instant-epp/src/request.rs

94 lines
2.9 KiB
Rust
Raw Normal View History

//! Types for EPP requests
use serde::{de::DeserializeOwned, ser::SerializeStruct, ser::Serializer, Serialize};
use std::fmt::Debug;
use crate::{
common::{StringValue, EPP_XMLNS},
response::{Response, ResponseDocument, ResponseStatus},
xml::EppXml,
2021-12-22 09:55:48 +00:00
Error,
};
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 {
fn deserialize_response(
epp_xml: &str,
2021-12-22 09:55:48 +00:00
) -> Result<Response<Self::Response, Ext::Response>, Error> {
let rsp =
<ResponseDocument<Self::Response, Ext::Response> as EppXml>::deserialize(epp_xml)?;
2022-01-23 22:24:27 +00:00
match rsp.data.result.code.is_success() {
true => Ok(rsp.data),
false => Err(crate::error::Error::Command(ResponseStatus {
result: rsp.data.result,
tr_ids: rsp.data.tr_ids,
})),
}
}
}
pub trait Command: Serialize + Debug {
type Response: DeserializeOwned + Debug;
const COMMAND: &'static str;
}
pub trait Extension: Serialize + Debug {
type Response: DeserializeOwned + Debug;
}
#[derive(Debug, PartialEq)]
2021-07-26 19:27:18 +00:00
/// Type corresponding to the &lt;command&gt; tag in an EPP XML request
/// with an &lt;extension&gt; tag
struct CommandWrapper<'a, D, E> {
pub command: &'static str,
2021-07-26 19:27:18 +00:00
/// The instance that will be used to populate the &lt;command&gt; tag
pub data: &'a D,
/// The client TRID
pub extension: Option<&'a E>,
pub client_tr_id: StringValue<'a>,
2021-07-25 14:34:01 +00:00
}
impl<'a, D: Serialize, E: Serialize> Serialize for CommandWrapper<'a, D, E> {
/// Serializes the generic type T to the proper XML tag (set by the `#[element_name(name = <tagname>)]` attribute) for the request
2021-07-25 14:34:01 +00:00
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("command", 3)?;
state.serialize_field(self.command, self.data)?;
state.serialize_field("extension", &self.extension)?;
2021-07-25 14:34:01 +00:00
state.serialize_field("clTRID", &self.client_tr_id)?;
state.end()
}
}
#[derive(Debug, PartialEq, Serialize)]
#[serde(rename = "epp")]
pub(crate) struct CommandDocument<'a, Cmd, Ext> {
xmlns: &'static str,
command: CommandWrapper<'a, Cmd, Ext>,
}
impl<'a, Cmd, Ext> CommandDocument<'a, Cmd, Ext> {
pub(crate) fn new(data: &'a Cmd, extension: Option<&'a Ext>, client_tr_id: &'a str) -> Self
where
Cmd: Transaction<Ext>,
Ext: Extension,
{
Self {
xmlns: EPP_XMLNS,
command: CommandWrapper {
command: Cmd::COMMAND,
data,
extension,
client_tr_id: client_tr_id.into(),
},
}
}
}
impl<'a, D: Serialize, E: Serialize> EppXml for CommandDocument<'a, D, E> {}