From 0c36d8add35eb74baf7554cbfdaa9e862410b8f9 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 23 Jan 2022 23:24:27 +0100 Subject: [PATCH] Use an enum for result codes --- src/contact/check.rs | 3 +- src/contact/create.rs | 3 +- src/contact/delete.rs | 3 +- src/contact/info.rs | 3 +- src/contact/update.rs | 3 +- src/domain/check.rs | 3 +- src/domain/create.rs | 3 +- src/domain/delete.rs | 3 +- src/domain/info.rs | 3 +- src/domain/renew.rs | 3 +- src/domain/transfer.rs | 14 ++-- src/domain/update.rs | 3 +- src/extensions/rgp/request.rs | 3 +- src/host/check.rs | 3 +- src/host/create.rs | 3 +- src/host/delete.rs | 3 +- src/host/info.rs | 3 +- src/host/update.rs | 3 +- src/login.rs | 3 +- src/logout.rs | 6 +- src/message/ack.rs | 3 +- src/message/poll.rs | 21 ++++-- src/request.rs | 6 +- src/response.rs | 130 ++++++++++++++++++++++++++++++++-- tests/basic.rs | 3 +- 25 files changed, 200 insertions(+), 37 deletions(-) diff --git a/src/contact/check.rs b/src/contact/check.rs index b66b809..968bc0b 100644 --- a/src/contact/check.rs +++ b/src/contact/check.rs @@ -89,6 +89,7 @@ mod tests { use super::ContactCheck; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -110,7 +111,7 @@ mod tests { let results = object.res_data().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!( results.check_data.contact_list[0].contact.id, diff --git a/src/contact/create.rs b/src/contact/create.rs index 7057b5e..b794069 100644 --- a/src/contact/create.rs +++ b/src/contact/create.rs @@ -101,6 +101,7 @@ mod tests { use crate::common::NoExtension; use crate::contact::Address; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -140,7 +141,7 @@ mod tests { let results = object.res_data().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(results.create_data.id, "eppdev-contact-4".into()); assert_eq!( diff --git a/src/contact/delete.rs b/src/contact/delete.rs index edb4988..1f01e21 100644 --- a/src/contact/delete.rs +++ b/src/contact/delete.rs @@ -47,6 +47,7 @@ mod tests { use super::ContactDelete; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -69,7 +70,7 @@ mod tests { >::deserialize_response(xml.as_str()) .unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); diff --git a/src/contact/info.rs b/src/contact/info.rs index 1b7fd3b..2b90abc 100644 --- a/src/contact/info.rs +++ b/src/contact/info.rs @@ -105,6 +105,7 @@ mod tests { use super::ContactInfo; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -132,7 +133,7 @@ mod tests { let fax_ext = fax.extension.as_ref().unwrap(); let auth_info = result.info_data.auth_info.as_ref().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(result.info_data.id, "eppdev-contact-3".into()); assert_eq!(result.info_data.roid, "UNDEF-ROID".into()); diff --git a/src/contact/update.rs b/src/contact/update.rs index dd95d91..4d860e6 100644 --- a/src/contact/update.rs +++ b/src/contact/update.rs @@ -111,6 +111,7 @@ mod tests { use crate::common::{NoExtension, ObjectStatus}; use crate::contact::Address; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -148,7 +149,7 @@ mod tests { >::deserialize_response(xml.as_str()) .unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); diff --git a/src/domain/check.rs b/src/domain/check.rs index eed6ea0..7416b61 100644 --- a/src/domain/check.rs +++ b/src/domain/check.rs @@ -88,6 +88,7 @@ mod tests { use super::DomainCheck; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -111,7 +112,7 @@ mod tests { let result = object.res_data().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!( result.check_data.domain_list[0].domain.name, diff --git a/src/domain/create.rs b/src/domain/create.rs index 251d60a..5e12fb7 100644 --- a/src/domain/create.rs +++ b/src/domain/create.rs @@ -107,6 +107,7 @@ mod tests { use crate::common::{HostAddr, NoExtension}; use crate::domain::{HostAttr, HostAttrList, HostObjList}; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -237,7 +238,7 @@ mod tests { let result = object.res_data().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(result.create_data.name, "eppdev-2.com".into()); assert_eq!( diff --git a/src/domain/delete.rs b/src/domain/delete.rs index a4ea711..4c5a31a 100644 --- a/src/domain/delete.rs +++ b/src/domain/delete.rs @@ -47,6 +47,7 @@ mod tests { use super::DomainDelete; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -68,7 +69,7 @@ mod tests { let object = >::deserialize_response(xml.as_str()).unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); diff --git a/src/domain/info.rs b/src/domain/info.rs index d68c46d..f2ce4d5 100644 --- a/src/domain/info.rs +++ b/src/domain/info.rs @@ -133,6 +133,7 @@ mod tests { use super::DomainInfo; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -163,7 +164,7 @@ mod tests { let registrant = result.info_data.registrant.as_ref().unwrap(); let contacts = result.info_data.contacts.as_ref().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(result.info_data.name, "eppdev-1.com".into()); assert_eq!(result.info_data.roid, "125899511_DOMAIN_COM-VRSN".into()); diff --git a/src/domain/renew.rs b/src/domain/renew.rs index 8617490..6177e82 100644 --- a/src/domain/renew.rs +++ b/src/domain/renew.rs @@ -79,6 +79,7 @@ mod tests { use super::DomainRenew; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; use chrono::NaiveDate; @@ -104,7 +105,7 @@ mod tests { let result = object.res_data().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(result.renew_data.name, "eppdev-1.com".into()); assert_eq!( diff --git a/src/domain/transfer.rs b/src/domain/transfer.rs index b97ad38..d248357 100644 --- a/src/domain/transfer.rs +++ b/src/domain/transfer.rs @@ -134,6 +134,7 @@ mod tests { use super::DomainTransfer; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -210,7 +211,10 @@ mod tests { let result = object.res_data().unwrap(); - assert_eq!(object.result.code, 1001); + assert_eq!( + object.result.code, + ResultCode::CommandCompletedSuccessfullyActionPending + ); assert_eq!( object.result.message, "Command completed successfully; action pending".into() @@ -239,7 +243,7 @@ mod tests { >::deserialize_response(xml.as_str()) .unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); @@ -252,7 +256,7 @@ mod tests { >::deserialize_response(xml.as_str()) .unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); @@ -265,7 +269,7 @@ mod tests { >::deserialize_response(xml.as_str()) .unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); @@ -280,7 +284,7 @@ mod tests { let result = object.res_data().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(result.transfer_data.name, "eppdev-transfer.com".into()); assert_eq!(result.transfer_data.transfer_status, "pending".into()); diff --git a/src/domain/update.rs b/src/domain/update.rs index a2c9c3d..54f8371 100644 --- a/src/domain/update.rs +++ b/src/domain/update.rs @@ -104,6 +104,7 @@ mod tests { use super::{DomainAddRemove, DomainAuthInfo, DomainChangeInfo, DomainContact, DomainUpdate}; use crate::common::{NoExtension, ObjectStatus}; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -155,7 +156,7 @@ mod tests { let object = >::deserialize_response(xml.as_str()).unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); diff --git a/src/extensions/rgp/request.rs b/src/extensions/rgp/request.rs index a6ed00d..cc3ba78 100644 --- a/src/extensions/rgp/request.rs +++ b/src/extensions/rgp/request.rs @@ -71,6 +71,7 @@ mod tests { use crate::domain::info::DomainInfo; use crate::domain::update::{DomainChangeInfo, DomainUpdate}; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -112,7 +113,7 @@ mod tests { let ext = object.extension.unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(ext.data.rgp_status[0].status, "pendingRestore".to_string()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); diff --git a/src/host/check.rs b/src/host/check.rs index ba171ce..aed72e8 100644 --- a/src/host/check.rs +++ b/src/host/check.rs @@ -92,6 +92,7 @@ mod tests { use super::HostCheck; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -115,7 +116,7 @@ mod tests { let result = object.res_data().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!( result.check_data.host_list[0].host.name, diff --git a/src/host/create.rs b/src/host/create.rs index f609d69..616afc2 100644 --- a/src/host/create.rs +++ b/src/host/create.rs @@ -73,6 +73,7 @@ mod tests { use super::HostCreate; use crate::common::{HostAddr, NoExtension}; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -101,7 +102,7 @@ mod tests { let result = object.res_data().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(result.create_data.name, "host2.eppdev-1.com".into()); assert_eq!( diff --git a/src/host/delete.rs b/src/host/delete.rs index 7849c0c..f4d006c 100644 --- a/src/host/delete.rs +++ b/src/host/delete.rs @@ -47,6 +47,7 @@ mod tests { use super::HostDelete; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -68,7 +69,7 @@ mod tests { let object = >::deserialize_response(xml.as_str()).unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); diff --git a/src/host/info.rs b/src/host/info.rs index 53b82cc..f0bc9c0 100644 --- a/src/host/info.rs +++ b/src/host/info.rs @@ -92,6 +92,7 @@ mod tests { use super::HostInfo; use crate::common::NoExtension; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -115,7 +116,7 @@ mod tests { let result = object.res_data().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(result.info_data.name, "host2.eppdev-1.com".into()); assert_eq!(result.info_data.roid, "UNDEF-ROID".into()); diff --git a/src/host/update.rs b/src/host/update.rs index d2e71bd..b89821d 100644 --- a/src/host/update.rs +++ b/src/host/update.rs @@ -93,6 +93,7 @@ mod tests { use super::{HostAddRemove, HostChangeInfo, HostUpdate}; use crate::common::{HostAddr, NoExtension, ObjectStatus}; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -136,7 +137,7 @@ mod tests { let object = >::deserialize_response(xml.as_str()).unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); diff --git a/src/login.rs b/src/login.rs index 9899c37..fd755f5 100644 --- a/src/login.rs +++ b/src/login.rs @@ -68,6 +68,7 @@ impl<'a> Command for Login<'a> { mod tests { use super::Login; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -86,7 +87,7 @@ mod tests { let xml = get_xml("response/login.xml").unwrap(); let object = Login::deserialize_response(xml.as_str()).unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); diff --git a/src/logout.rs b/src/logout.rs index b5833a8..a621e21 100644 --- a/src/logout.rs +++ b/src/logout.rs @@ -22,6 +22,7 @@ pub struct Logout; mod tests { use super::Logout; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SVTRID}; #[test] @@ -38,7 +39,10 @@ mod tests { let xml = get_xml("response/logout.xml").unwrap(); let object = Logout::deserialize_response(xml.as_str()).unwrap(); - assert_eq!(object.result.code, 1500); + assert_eq!( + object.result.code, + ResultCode::CommandCompletedSuccessfullyEndingSession + ); assert_eq!( object.result.message, "Command completed successfully; ending session".into() diff --git a/src/message/ack.rs b/src/message/ack.rs index 90d1c10..465bceb 100644 --- a/src/message/ack.rs +++ b/src/message/ack.rs @@ -35,6 +35,7 @@ impl<'a> MessageAck<'a> { mod tests { use super::MessageAck; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] @@ -55,7 +56,7 @@ mod tests { let msg = object.message_queue().unwrap(); - assert_eq!(object.result.code, 1000); + assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(msg.count, 4); assert_eq!(msg.id, "12345".to_string()); diff --git a/src/message/poll.rs b/src/message/poll.rs index bf6e8ec..676f5e1 100644 --- a/src/message/poll.rs +++ b/src/message/poll.rs @@ -53,6 +53,7 @@ mod tests { use super::MessagePoll; use crate::message::poll::MessageData; use crate::request::Transaction; + use crate::response::ResultCode; use crate::tests::{get_xml, CLTRID, SVTRID}; #[test] @@ -74,7 +75,10 @@ mod tests { let result = object.res_data().unwrap(); let msg = object.message_queue().unwrap(); - assert_eq!(object.result.code, 1301); + assert_eq!( + object.result.code, + ResultCode::CommandCompletedSuccessfullyAckToDequeue + ); assert_eq!( object.result.message, "Command completed successfully; ack to dequeue".into() @@ -117,7 +121,10 @@ mod tests { let result = object.res_data().unwrap(); let msg = object.message_queue().unwrap(); - assert_eq!(object.result.code, 1301); + assert_eq!( + object.result.code, + ResultCode::CommandCompletedSuccessfullyAckToDequeue + ); assert_eq!( object.result.message, "Command completed successfully; ack to dequeue".into() @@ -156,7 +163,10 @@ mod tests { let msg = object.message_queue().unwrap(); - assert_eq!(object.result.code, 1301); + assert_eq!( + object.result.code, + ResultCode::CommandCompletedSuccessfullyAckToDequeue + ); assert_eq!( object.result.message, "Command completed successfully; ack to dequeue".into() @@ -182,7 +192,10 @@ mod tests { let xml = get_xml("response/message/poll_empty_queue.xml").unwrap(); let object = MessagePoll::deserialize_response(xml.as_str()).unwrap(); - assert_eq!(object.result.code, 1300); + assert_eq!( + object.result.code, + ResultCode::CommandCompletedSuccessfullyNoMessages + ); assert_eq!( object.result.message, "Command completed successfully; no messages".into() diff --git a/src/request.rs b/src/request.rs index 77b5fa3..413903f 100644 --- a/src/request.rs +++ b/src/request.rs @@ -33,9 +33,9 @@ pub trait Transaction: Command + Sized { ) -> Result, Error> { let rsp = as EppXml>::deserialize(epp_xml)?; - match rsp.data.result.code { - 0..=2000 => Ok(rsp.data), - _ => Err(crate::error::Error::Command(ResponseStatus { + 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, })), diff --git a/src/response.rs b/src/response.rs index 561c9d1..4ddb01d 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,7 +1,7 @@ //! Types for EPP responses use serde::{de::DeserializeOwned, Deserialize}; -use std::fmt::Debug; +use std::fmt::{self, Debug}; use crate::common::StringValue; use crate::xml::EppXml; @@ -33,7 +33,7 @@ pub struct ExtValue { #[derive(Deserialize, Debug, PartialEq)] pub struct EppResult { /// The result code - pub code: u16, + pub code: ResultCode, /// The result message #[serde(rename = "msg")] pub message: StringValue<'static>, @@ -42,6 +42,128 @@ pub struct EppResult { pub ext_value: Option, } +/// Response codes as enumerated in section 3 of RFC 5730 +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum ResultCode { + CommandCompletedSuccessfully = 1000, + CommandCompletedSuccessfullyActionPending = 1001, + CommandCompletedSuccessfullyNoMessages = 1300, + CommandCompletedSuccessfullyAckToDequeue = 1301, + CommandCompletedSuccessfullyEndingSession = 1500, + UnknownCommand = 2000, + CommandSyntaxError = 2001, + CommandUseError = 2002, + RequiredParameterMissing = 2003, + ParameterValueRangeError = 2004, + ParameterValueSyntaxError = 2005, + UnimplementedProtocolVersion = 2100, + UnimplementedCommand = 2101, + UnimplementedOption = 2102, + UnimplementedExtension = 2103, + BillingFailure = 2104, + ObjectIsNotEligibleForRenewal = 2105, + ObjectIsNotEligibleForTransfer = 2106, + AuthenticationError = 2200, + AuthorizationError = 2201, + InvalidAuthorizationInformation = 2202, + ObjectPendingTransfer = 2300, + ObjectNotPendingTransfer = 2301, + ObjectExists = 2302, + ObjectDoesNotExist = 2303, + ObjectStatusProhibitsOperation = 2304, + ObjectAssociationProhibitsOperation = 2305, + ParameterValuePolicyError = 2306, + UnimplementedObjectService = 2307, + DataManagementPolicyViolation = 2308, + CommandFailed = 2400, + CommandFailedServerClosingConnection = 2500, + AuthenticationErrorServerClosingConnection = 2501, + SessionLimitExceededServerClosingConnection = 2502, +} + +impl ResultCode { + pub fn from_u16(code: u16) -> Option { + match code { + 1000 => Some(ResultCode::CommandCompletedSuccessfully), + 1001 => Some(ResultCode::CommandCompletedSuccessfullyActionPending), + 1300 => Some(ResultCode::CommandCompletedSuccessfullyNoMessages), + 1301 => Some(ResultCode::CommandCompletedSuccessfullyAckToDequeue), + 1500 => Some(ResultCode::CommandCompletedSuccessfullyEndingSession), + 2000 => Some(ResultCode::UnknownCommand), + 2001 => Some(ResultCode::CommandSyntaxError), + 2002 => Some(ResultCode::CommandUseError), + 2003 => Some(ResultCode::RequiredParameterMissing), + 2004 => Some(ResultCode::ParameterValueRangeError), + 2005 => Some(ResultCode::ParameterValueSyntaxError), + 2100 => Some(ResultCode::UnimplementedProtocolVersion), + 2101 => Some(ResultCode::UnimplementedCommand), + 2102 => Some(ResultCode::UnimplementedOption), + 2103 => Some(ResultCode::UnimplementedExtension), + 2104 => Some(ResultCode::BillingFailure), + 2105 => Some(ResultCode::ObjectIsNotEligibleForRenewal), + 2106 => Some(ResultCode::ObjectIsNotEligibleForTransfer), + 2200 => Some(ResultCode::AuthenticationError), + 2201 => Some(ResultCode::AuthorizationError), + 2202 => Some(ResultCode::InvalidAuthorizationInformation), + 2300 => Some(ResultCode::ObjectPendingTransfer), + 2301 => Some(ResultCode::ObjectNotPendingTransfer), + 2302 => Some(ResultCode::ObjectExists), + 2303 => Some(ResultCode::ObjectDoesNotExist), + 2304 => Some(ResultCode::ObjectStatusProhibitsOperation), + 2305 => Some(ResultCode::ObjectAssociationProhibitsOperation), + 2306 => Some(ResultCode::ParameterValuePolicyError), + 2307 => Some(ResultCode::UnimplementedObjectService), + 2308 => Some(ResultCode::DataManagementPolicyViolation), + 2400 => Some(ResultCode::CommandFailed), + 2500 => Some(ResultCode::CommandFailedServerClosingConnection), + 2501 => Some(ResultCode::AuthenticationErrorServerClosingConnection), + 2502 => Some(ResultCode::SessionLimitExceededServerClosingConnection), + _ => None, + } + } + + pub fn is_success(&self) -> bool { + use ResultCode::*; + matches!( + self, + CommandCompletedSuccessfully + | CommandCompletedSuccessfullyActionPending + | CommandCompletedSuccessfullyNoMessages + | CommandCompletedSuccessfullyAckToDequeue + | CommandCompletedSuccessfullyEndingSession + ) + } +} + +impl<'de> Deserialize<'de> for ResultCode { + fn deserialize(deserializer: D) -> Result + where + D: serde::de::Deserializer<'de>, + { + deserializer.deserialize_u16(ResultCodeVisitor) + } +} + +struct ResultCodeVisitor; + +impl<'de> serde::de::Visitor<'de> for ResultCodeVisitor { + type Value = ResultCode; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("a valid EPP result code") + } + + fn visit_u16(self, v: u16) -> Result + where + E: serde::de::Error, + { + use serde::de::Unexpected; + ResultCode::from_u16(v).ok_or_else(|| { + E::invalid_value(Unexpected::Unsigned(v as u64), &"unexpected result code") + }) + } +} + /// Type corresponding to the tag in an EPP response XML #[derive(Deserialize, Debug, PartialEq)] pub struct ResponseTRID { @@ -135,7 +257,7 @@ impl Response { #[cfg(test)] mod tests { - use super::ResultDocument; + use super::{ResultCode, ResultDocument}; use crate::tests::{get_xml, CLTRID, SVTRID}; use crate::xml::EppXml; @@ -144,7 +266,7 @@ mod tests { let xml = get_xml("response/error.xml").unwrap(); let object = ResultDocument::deserialize(xml.as_str()).unwrap(); - assert_eq!(object.data.result.code, 2303); + assert_eq!(object.data.result.code, ResultCode::ObjectDoesNotExist); assert_eq!(object.data.result.message, "Object does not exist".into()); assert_eq!( object.data.result.ext_value.unwrap().reason, diff --git a/tests/basic.rs b/tests/basic.rs index 87c701e..9a9621b 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -7,6 +7,7 @@ use tokio_test::io::Builder; use epp_client::domain::check::DomainCheck; use epp_client::login::Login; +use epp_client::response::ResultCode; use epp_client::EppClient; const CLTRID: &str = "cltrid:1626454866"; @@ -103,7 +104,7 @@ async fn client() { .transact(&DomainCheck::new(vec!["eppdev.com", "eppdev.net"]), CLTRID) .await .unwrap(); - assert_eq!(rsp.result.code, 1000); + assert_eq!(rsp.result.code, ResultCode::CommandCompletedSuccessfully); let result = rsp.res_data().unwrap(); assert_eq!(