diff --git a/src/domain.rs b/src/domain.rs index 58f6b6c..b392247 100644 --- a/src/domain.rs +++ b/src/domain.rs @@ -3,6 +3,7 @@ use std::borrow::Cow; use serde::{Deserialize, Serialize}; use crate::common::{HostAddr, StringValue}; +use crate::Error; pub mod check; pub use check::DomainCheck; @@ -75,27 +76,31 @@ pub struct DomainContact<'a> { } /// The <period> type for registration, renewal or transfer on domain transactions -#[derive(Serialize, Debug)] +#[derive(Clone, Copy, Debug, Serialize)] pub struct Period { /// The interval (usually 'y' indicating years) - unit: String, + unit: char, /// The length of the registration, renewal or transfer period (usually in years) #[serde(rename = "$value")] - length: u16, + length: u8, } impl Period { - /// Creates a new period in years - pub fn new(length: u16) -> Period { - Period { - unit: "y".to_string(), - length, - } + pub fn years(length: u8) -> Result { + Self::new(length, 'y') } - /// Sets the period unit ('y' for years, most commonly) - pub fn set_unit(&mut self, unit: &str) { - self.unit = unit.to_string(); + pub fn months(length: u8) -> Result { + Self::new(length, 'm') + } + + fn new(length: u8, unit: char) -> Result { + match length { + 1..=99 => Ok(Period { length, unit }), + 0 | 100.. => Err(Error::Other( + "Period length must be greater than 0 and less than 100".into(), + )), + } } } diff --git a/src/domain/create.rs b/src/domain/create.rs index 5e12fb7..6a7b14f 100644 --- a/src/domain/create.rs +++ b/src/domain/create.rs @@ -55,7 +55,7 @@ pub struct DomainCreate<'a> { impl<'a> DomainCreate<'a> { pub fn new( name: &'a str, - period: u16, + period: Period, ns: Option>, registrant_id: Option<&'a str>, auth_password: &'a str, @@ -65,7 +65,7 @@ impl<'a> DomainCreate<'a> { domain: DomainCreateRequestData { xmlns: XMLNS, name: name.into(), - period: Period::new(period), + period, ns, registrant: registrant_id.map(|id| id.into()), auth_info: DomainAuthInfo::new(auth_password), @@ -103,7 +103,7 @@ pub struct DomainCreateResponse { #[cfg(test)] mod tests { - use super::{DomainContact, DomainCreate, HostList}; + use super::{DomainContact, DomainCreate, HostList, Period}; use crate::common::{HostAddr, NoExtension}; use crate::domain::{HostAttr, HostAttrList, HostObjList}; use crate::request::Transaction; @@ -131,7 +131,7 @@ mod tests { let object = DomainCreate::new( "eppdev-1.com", - 1, + Period::years(1).unwrap(), None, Some("eppdev-contact-3"), "epP4uthd#v", @@ -167,7 +167,7 @@ mod tests { let hosts = &["ns1.test.com".into(), "ns2.test.com".into()]; let object = DomainCreate::new( "eppdev-1.com", - 1, + Period::years(1).unwrap(), Some(HostList::HostObjList(HostObjList { hosts })), Some("eppdev-contact-3"), "epP4uthd#v", @@ -216,7 +216,7 @@ mod tests { let object = DomainCreate::new( "eppdev-2.com", - 1, + Period::years(1).unwrap(), Some(HostList::HostAttrList(HostAttrList { hosts })), Some("eppdev-contact-3"), "epP4uthd#v", diff --git a/src/domain/renew.rs b/src/domain/renew.rs index 6177e82..0c6c280 100644 --- a/src/domain/renew.rs +++ b/src/domain/renew.rs @@ -14,14 +14,14 @@ impl<'a> Command for DomainRenew<'a> { } impl<'a> DomainRenew<'a> { - pub fn new(name: &'a str, current_expiry_date: NaiveDate, years: u16) -> Self { + pub fn new(name: &'a str, current_expiry_date: NaiveDate, period: Period) -> Self { let exp_date_str = current_expiry_date.format("%Y-%m-%d").to_string().into(); Self { domain: DomainRenewRequestData { xmlns: XMLNS, name: name.into(), current_expiry_date: exp_date_str, - period: Period::new(years), + period, }, } } @@ -76,7 +76,7 @@ pub struct DomainRenewResponse { #[cfg(test)] mod tests { - use super::DomainRenew; + use super::{DomainRenew, Period}; use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; @@ -88,7 +88,7 @@ mod tests { let xml = get_xml("request/domain/renew.xml").unwrap(); let exp_date = NaiveDate::from_ymd(2022, 7, 23); - let object = DomainRenew::new("eppdev.com", exp_date, 1); + let object = DomainRenew::new("eppdev.com", exp_date, Period::years(1).unwrap()); let serialized = >::serialize_request(&object, None, CLTRID) diff --git a/src/domain/transfer.rs b/src/domain/transfer.rs index d248357..fea9e30 100644 --- a/src/domain/transfer.rs +++ b/src/domain/transfer.rs @@ -13,11 +13,11 @@ impl<'a> Command for DomainTransfer<'a> { } impl<'a> DomainTransfer<'a> { - pub fn new(name: &'a str, years: Option, auth_password: &'a str) -> Self { + pub fn new(name: &'a str, period: Option, auth_password: &'a str) -> Self { Self::build( "request", name, - years.map(Period::new), + period, Some(DomainAuthInfo::new(auth_password)), ) } @@ -131,7 +131,7 @@ pub struct DomainTransferResponse { #[cfg(test)] mod tests { - use super::DomainTransfer; + use super::{DomainTransfer, Period}; use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; @@ -141,7 +141,8 @@ mod tests { fn request_command() { let xml = get_xml("request/domain/transfer_request.xml").unwrap(); - let object = DomainTransfer::new("testing.com", Some(1), "epP4uthd#v"); + let object = + DomainTransfer::new("testing.com", Some(Period::years(1).unwrap()), "epP4uthd#v"); let serialized = >::serialize_request(&object, None, CLTRID)