instant-epp/src/request.rs

75 lines
2.1 KiB
Rust
Raw Normal View History

//! Types for EPP requests
use std::fmt::Debug;
2022-11-21 19:34:25 +00:00
use instant_xml::{FromXmlOwned, ToXml};
use crate::common::EPP_XMLNS;
pub const EPP_VERSION: &str = "1.0";
pub const EPP_LANG: &str = "en";
/// Trait to set correct value for xml tags when tags are being generated from generic types
pub trait Transaction<Ext: Extension>: Command + Sized {}
2022-11-21 19:34:25 +00:00
pub trait Command: ToXml + Debug {
type Response: FromXmlOwned + Debug;
const COMMAND: &'static str;
}
2022-11-21 19:34:25 +00:00
pub trait Extension: ToXml + Debug {
type Response: FromXmlOwned + Debug;
}
#[derive(Debug, PartialEq)]
2023-03-02 13:27:26 +00:00
/// Type corresponding to the `<command>` tag in an EPP XML request
/// with an `<extension>` tag
2022-11-21 19:34:25 +00:00
pub(crate) struct CommandWrapper<'a, D, E> {
command: &'static str,
2023-03-02 13:27:26 +00:00
/// The instance that will be used to populate the `<command>` tag
data: &'a D,
/// The client TRID
extension: Option<&'a E>,
client_tr_id: String,
2021-07-25 14:34:01 +00:00
}
2022-11-21 19:34:25 +00:00
impl<'a, E: Extension, D: Transaction<E>> CommandWrapper<'a, D, E> {
pub(crate) fn new(data: &'a D, extension: Option<&'a E>, client_tr_id: &'a str) -> Self {
Self {
command: D::COMMAND,
data,
extension,
client_tr_id: client_tr_id.into(),
}
2021-07-25 14:34:01 +00:00
}
}
2022-11-21 19:34:25 +00:00
impl<'a, D: ToXml, E: ToXml> ToXml for CommandWrapper<'a, D, E> {
fn serialize<W: std::fmt::Write + ?Sized>(
&self,
_: Option<instant_xml::Id<'_>>,
serializer: &mut instant_xml::Serializer<W>,
) -> Result<(), instant_xml::Error> {
let prefix = serializer.write_start("command", EPP_XMLNS)?;
serializer.end_start()?;
self.data.serialize(None, serializer)?;
if let Some(extension) = self.extension {
Ext { inner: extension }.serialize(None, serializer)?;
}
2022-11-21 19:34:25 +00:00
let id_prefix = serializer.write_start("clTRID", EPP_XMLNS)?;
serializer.end_start()?;
serializer.write_str(&self.client_tr_id)?;
serializer.write_close(id_prefix, "clTRID")?;
serializer.write_close(prefix, "command")?;
Ok(())
}
}
2022-11-21 19:34:25 +00:00
#[derive(Debug, ToXml)]
#[xml(rename = "extension", ns(EPP_XMLNS))]
struct Ext<E> {
inner: E,
}