use instant_xml::{Error, FromXml, ToXml};
use crate::{
poll::Poll,
request::{Extension, Transaction},
};
pub const XMLNS: &str = "urn:ietf:params:xml:ns:changePoll-1.0";
#[derive(Debug, ToXml)]
struct ChangePollExtension;
impl Transaction<ChangePollExtension> for Poll {}
impl Extension for ChangePollExtension {
type Response = ChangePoll;
}
#[derive(Debug, FromXml)]
#[xml(rename = "changeData", ns(XMLNS))]
pub struct ChangePoll {
pub operation: Operation,
pub date: String,
#[xml(rename = "svTRID")]
pub server_tr_id: String,
pub who: String,
pub case_id: Option<CaseIdentifier>,
pub reason: Option<Reason>,
#[xml(attribute)]
state: Option<State>,
}
impl ChangePoll {
pub fn state(&self) -> State {
self.state.unwrap_or_default()
}
}
#[derive(Debug, FromXml)]
#[xml(rename = "operation", ns(XMLNS))]
pub struct Operation {
#[xml(attribute, rename = "op")]
op: Option<String>,
#[xml(direct)]
kind: OperationType,
}
impl Operation {
pub fn kind(&self) -> Result<OperationKind<'_>, Error> {
Ok(match self.kind {
OperationType::Create => OperationKind::Create,
OperationType::Delete => OperationKind::Delete,
OperationType::Renew => OperationKind::Renew,
OperationType::Transfer => OperationKind::Transfer,
OperationType::Update => OperationKind::Update,
OperationType::Restore => OperationKind::Restore,
OperationType::AutoRenew => OperationKind::AutoRenew,
OperationType::AutoDelete => OperationKind::AutoDelete,
OperationType::AutoPurge => OperationKind::AutoPurge,
OperationType::Custom => match self.op.as_deref() {
Some(op) => OperationKind::Custom(op),
None => {
return Err(Error::Other(
"invariant error: Missing op attribute for custom operation".to_string(),
))
}
},
})
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum OperationKind<'a> {
Create,
Delete,
Renew,
Transfer,
Update,
Restore,
AutoRenew,
AutoDelete,
AutoPurge,
Custom(&'a str),
}
#[derive(Debug, Copy, Clone, FromXml)]
#[xml(scalar, rename_all = "camelCase", ns(XMLNS))]
enum OperationType {
Create,
Delete,
Renew,
Transfer,
Update,
Restore,
AutoRenew,
AutoDelete,
AutoPurge,
Custom,
}
#[derive(Debug, FromXml)]
#[xml(rename = "caseId", ns(XMLNS))]
pub struct CaseIdentifier {
#[xml(attribute, rename = "type")]
id_type: CaseIdentifierType,
#[xml(attribute)]
name: Option<String>,
#[xml(direct)]
pub id: String,
}
impl CaseIdentifier {
pub fn kind(&self) -> Result<CaseIdentifierKind<'_>, Error> {
Ok(match self.id_type {
CaseIdentifierType::Udrp => CaseIdentifierKind::Udrp,
CaseIdentifierType::Urs => CaseIdentifierKind::Urs,
CaseIdentifierType::Custom => match self.name.as_deref() {
Some(name) => CaseIdentifierKind::Custom(name),
None => {
return Err(Error::Other(
"invariant error: Missing name attribute for custom case identifier"
.to_string(),
))
}
},
})
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum CaseIdentifierKind<'a> {
Udrp,
Urs,
Custom(&'a str),
}
#[derive(Debug, Copy, Clone, FromXml)]
#[xml(scalar, rename_all = "camelCase")]
enum CaseIdentifierType {
Udrp,
Urs,
Custom,
}
#[derive(Debug, Eq, FromXml, PartialEq)]
#[xml(rename = "reason", ns(XMLNS))]
pub struct Reason {
#[xml(attribute)]
pub lang: Option<String>,
#[xml(direct)]
pub inner: String,
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, FromXml)]
#[xml(scalar, rename_all = "camelCase")]
pub enum State {
Before,
#[default]
After,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::poll::Poll;
use crate::response::ResultCode;
use crate::tests::{response_from_file_with_ext, CLTRID, SVTRID};
#[test]
fn urs_lock_before() {
let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
"response/extensions/change_poll/urs_lock_before.xml",
);
assert_eq!(
object.result.code,
ResultCode::CommandCompletedSuccessfullyAckToDequeue
);
assert_eq!(
object.result.message,
"Command completed successfully; ack to dequeue"
);
assert_eq!(object.extension().unwrap().state.unwrap(), State::Before);
assert_eq!(
object.extension().unwrap().operation.kind().unwrap(),
OperationKind::Update
);
assert_eq!(object.extension().unwrap().date, "2013-10-22T14:25:57.0Z");
assert_eq!(object.extension().unwrap().server_tr_id, "12345-XYZ");
assert_eq!(object.extension().unwrap().who, "URS Admin");
assert_eq!(
object
.extension()
.unwrap()
.case_id
.as_ref()
.unwrap()
.kind()
.unwrap(),
CaseIdentifierKind::Urs
);
assert_eq!(
object.extension().unwrap().reason.as_ref().unwrap().inner,
"URS Lock"
);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
assert_eq!(object.tr_ids.server_tr_id, SVTRID);
}
#[test]
fn urs_lock_after() {
let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
"response/extensions/change_poll/urs_lock_after.xml",
);
assert_eq!(
object.result.code,
ResultCode::CommandCompletedSuccessfullyAckToDequeue
);
assert_eq!(
object.result.message,
"Command completed successfully; ack to dequeue"
);
assert_eq!(object.extension().unwrap().state.unwrap(), State::After);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
assert_eq!(object.tr_ids.server_tr_id, SVTRID);
}
#[test]
fn custom_sync_after() {
let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
"response/extensions/change_poll/custom_sync_after.xml",
);
assert_eq!(
object.result.code,
ResultCode::CommandCompletedSuccessfullyAckToDequeue
);
assert_eq!(
object.result.message,
"Command completed successfully; ack to dequeue"
);
assert_eq!(
object.extension().unwrap().operation.kind().unwrap(),
OperationKind::Custom("sync")
);
assert_eq!(object.extension().unwrap().who, "CSR");
assert_eq!(
object.extension().unwrap().reason.as_ref().unwrap().inner,
"Customer sync request"
);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
assert_eq!(object.tr_ids.server_tr_id, SVTRID);
}
#[test]
fn delete_before() {
let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
"response/extensions/change_poll/delete_before.xml",
);
assert_eq!(
object.result.code,
ResultCode::CommandCompletedSuccessfullyAckToDequeue
);
assert_eq!(
object.result.message,
"Command completed successfully; ack to dequeue"
);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
assert_eq!(object.tr_ids.server_tr_id, SVTRID);
}
#[test]
fn autopurge_before() {
let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
"response/extensions/change_poll/autopurge_before.xml",
);
assert_eq!(
object.result.code,
ResultCode::CommandCompletedSuccessfullyAckToDequeue
);
assert_eq!(
object.result.message,
"Command completed successfully; ack to dequeue"
);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
assert_eq!(object.tr_ids.server_tr_id, SVTRID);
}
#[test]
fn update_after() {
let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
"response/extensions/change_poll/update_after.xml",
);
assert_eq!(
object.result.code,
ResultCode::CommandCompletedSuccessfullyAckToDequeue
);
assert_eq!(
object.result.message,
"Command completed successfully; ack to dequeue"
);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
assert_eq!(object.tr_ids.server_tr_id, SVTRID);
}
}