1use instant_xml::{Error, FromXml, ToXml};
7
8use crate::{
9 poll::Poll,
10 request::{Extension, Transaction},
11};
12
13pub const XMLNS: &str = "urn:ietf:params:xml:ns:changePoll-1.0";
14
15#[derive(Debug, ToXml)]
17struct ChangePollExtension;
18
19impl Transaction<ChangePollExtension> for Poll {}
20
21impl Extension for ChangePollExtension {
22 type Response = ChangePoll;
23}
24
25#[derive(Debug, FromXml)]
29#[xml(rename = "changeData", ns(XMLNS))]
30pub struct ChangePoll {
31 pub operation: Operation,
33 pub date: String,
35 #[xml(rename = "svTRID")]
37 pub server_tr_id: String,
38 pub who: String,
40 pub case_id: Option<CaseIdentifier>,
42 pub reason: Option<Reason>,
44 #[xml(attribute)]
46 state: Option<State>,
49}
50
51impl ChangePoll {
52 pub fn state(&self) -> State {
54 self.state.unwrap_or_default()
55 }
56}
57
58#[derive(Debug, FromXml)]
62#[xml(rename = "operation", ns(XMLNS))]
63pub struct Operation {
64 #[xml(attribute, rename = "op")]
66 op: Option<String>,
67 #[xml(direct)]
69 kind: OperationType,
70}
71
72impl Operation {
73 pub fn kind(&self) -> Result<OperationKind<'_>, Error> {
74 Ok(match self.kind {
75 OperationType::Create => OperationKind::Create,
76 OperationType::Delete => OperationKind::Delete,
77 OperationType::Renew => OperationKind::Renew,
78 OperationType::Transfer => OperationKind::Transfer,
79 OperationType::Update => OperationKind::Update,
80 OperationType::Restore => OperationKind::Restore,
81 OperationType::AutoRenew => OperationKind::AutoRenew,
82 OperationType::AutoDelete => OperationKind::AutoDelete,
83 OperationType::AutoPurge => OperationKind::AutoPurge,
84 OperationType::Custom => match self.op.as_deref() {
85 Some(op) => OperationKind::Custom(op),
86 None => {
87 return Err(Error::Other(
88 "invariant error: Missing op attribute for custom operation".to_string(),
89 ))
90 }
91 },
92 })
93 }
94}
95
96#[derive(Debug, Copy, Clone, PartialEq, Eq)]
98pub enum OperationKind<'a> {
99 Create,
100 Delete,
101 Renew,
102 Transfer,
103 Update,
104 Restore,
105 AutoRenew,
106 AutoDelete,
107 AutoPurge,
108 Custom(&'a str),
109}
110
111#[derive(Debug, Copy, Clone, FromXml)]
114#[xml(scalar, rename_all = "camelCase", ns(XMLNS))]
115enum OperationType {
116 Create,
117 Delete,
118 Renew,
119 Transfer,
120 Update,
121 Restore,
122 AutoRenew,
123 AutoDelete,
124 AutoPurge,
125 Custom,
126}
127
128#[derive(Debug, FromXml)]
132#[xml(rename = "caseId", ns(XMLNS))]
133pub struct CaseIdentifier {
134 #[xml(attribute, rename = "type")]
135 id_type: CaseIdentifierType,
136 #[xml(attribute)]
137 name: Option<String>,
138 #[xml(direct)]
139 pub id: String,
140}
141
142impl CaseIdentifier {
143 pub fn kind(&self) -> Result<CaseIdentifierKind<'_>, Error> {
144 Ok(match self.id_type {
145 CaseIdentifierType::Udrp => CaseIdentifierKind::Udrp,
146 CaseIdentifierType::Urs => CaseIdentifierKind::Urs,
147 CaseIdentifierType::Custom => match self.name.as_deref() {
148 Some(name) => CaseIdentifierKind::Custom(name),
149 None => {
150 return Err(Error::Other(
151 "invariant error: Missing name attribute for custom case identifier"
152 .to_string(),
153 ))
154 }
155 },
156 })
157 }
158}
159
160#[derive(Debug, Copy, Clone, PartialEq, Eq)]
161pub enum CaseIdentifierKind<'a> {
162 Udrp,
163 Urs,
164 Custom(&'a str),
165}
166
167#[derive(Debug, Copy, Clone, FromXml)]
170#[xml(scalar, rename_all = "camelCase")]
171enum CaseIdentifierType {
172 Udrp,
173 Urs,
174 Custom,
175}
176
177#[derive(Debug, Eq, FromXml, PartialEq)]
188#[xml(rename = "reason", ns(XMLNS))]
189pub struct Reason {
190 #[xml(attribute)]
192 pub lang: Option<String>,
193 #[xml(direct)]
194 pub inner: String,
195}
196
197#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, FromXml)]
199#[xml(scalar, rename_all = "camelCase")]
200pub enum State {
201 Before,
202 #[default]
203 After,
204}
205
206#[cfg(test)]
207mod tests {
208 use super::*;
209 use crate::poll::Poll;
210 use crate::response::ResultCode;
211 use crate::tests::{response_from_file_with_ext, CLTRID, SVTRID};
212
213 #[test]
214 fn urs_lock_before() {
215 let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
216 "response/extensions/change_poll/urs_lock_before.xml",
217 );
218
219 assert_eq!(
220 object.result.code,
221 ResultCode::CommandCompletedSuccessfullyAckToDequeue
222 );
223 assert_eq!(
224 object.result.message,
225 "Command completed successfully; ack to dequeue"
226 );
227
228 assert_eq!(object.extension().unwrap().state.unwrap(), State::Before);
229 assert_eq!(
230 object.extension().unwrap().operation.kind().unwrap(),
231 OperationKind::Update
232 );
233 assert_eq!(object.extension().unwrap().date, "2013-10-22T14:25:57.0Z");
234 assert_eq!(object.extension().unwrap().server_tr_id, "12345-XYZ");
235 assert_eq!(object.extension().unwrap().who, "URS Admin");
236 assert_eq!(
237 object
238 .extension()
239 .unwrap()
240 .case_id
241 .as_ref()
242 .unwrap()
243 .kind()
244 .unwrap(),
245 CaseIdentifierKind::Urs
246 );
247 assert_eq!(
248 object.extension().unwrap().reason.as_ref().unwrap().inner,
249 "URS Lock"
250 );
251
252 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
253 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
254 }
255
256 #[test]
257 fn urs_lock_after() {
258 let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
259 "response/extensions/change_poll/urs_lock_after.xml",
260 );
261
262 assert_eq!(
263 object.result.code,
264 ResultCode::CommandCompletedSuccessfullyAckToDequeue
265 );
266 assert_eq!(
267 object.result.message,
268 "Command completed successfully; ack to dequeue"
269 );
270 assert_eq!(object.extension().unwrap().state.unwrap(), State::After);
271
272 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
273 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
274 }
275
276 #[test]
277 fn custom_sync_after() {
278 let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
279 "response/extensions/change_poll/custom_sync_after.xml",
280 );
281
282 assert_eq!(
283 object.result.code,
284 ResultCode::CommandCompletedSuccessfullyAckToDequeue
285 );
286 assert_eq!(
287 object.result.message,
288 "Command completed successfully; ack to dequeue"
289 );
290
291 assert_eq!(
292 object.extension().unwrap().operation.kind().unwrap(),
293 OperationKind::Custom("sync")
294 );
295 assert_eq!(object.extension().unwrap().who, "CSR");
296 assert_eq!(
297 object.extension().unwrap().reason.as_ref().unwrap().inner,
298 "Customer sync request"
299 );
300
301 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
302 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
303 }
304
305 #[test]
306 fn delete_before() {
307 let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
308 "response/extensions/change_poll/delete_before.xml",
309 );
310
311 assert_eq!(
312 object.result.code,
313 ResultCode::CommandCompletedSuccessfullyAckToDequeue
314 );
315 assert_eq!(
316 object.result.message,
317 "Command completed successfully; ack to dequeue"
318 );
319
320 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
321 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
322 }
323
324 #[test]
325 fn autopurge_before() {
326 let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
327 "response/extensions/change_poll/autopurge_before.xml",
328 );
329
330 assert_eq!(
331 object.result.code,
332 ResultCode::CommandCompletedSuccessfullyAckToDequeue
333 );
334 assert_eq!(
335 object.result.message,
336 "Command completed successfully; ack to dequeue"
337 );
338
339 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
340 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
341 }
342
343 #[test]
344 fn update_after() {
345 let object = response_from_file_with_ext::<Poll, ChangePollExtension>(
346 "response/extensions/change_poll/update_after.xml",
347 );
348
349 assert_eq!(
350 object.result.code,
351 ResultCode::CommandCompletedSuccessfullyAckToDequeue
352 );
353 assert_eq!(
354 object.result.message,
355 "Command completed successfully; ack to dequeue"
356 );
357
358 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
359 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
360 }
361}