Make the Period type align with the standard

This commit is contained in:
Dirkjan Ochtman 2022-01-27 23:34:13 +01:00 committed by masalachai
parent 5fb32978bd
commit 2639ec8c95
4 changed files with 32 additions and 26 deletions

View File

@ -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 &lt;period&gt; 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, Error> {
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, Error> {
Self::new(length, 'm')
}
fn new(length: u8, unit: char) -> Result<Self, Error> {
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(),
)),
}
}
}

View File

@ -55,7 +55,7 @@ pub struct DomainCreate<'a> {
impl<'a> DomainCreate<'a> {
pub fn new(
name: &'a str,
period: u16,
period: Period,
ns: Option<HostList<'a>>,
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",

View File

@ -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 =
<DomainRenew as Transaction<NoExtension>>::serialize_request(&object, None, CLTRID)

View File

@ -13,11 +13,11 @@ impl<'a> Command for DomainTransfer<'a> {
}
impl<'a> DomainTransfer<'a> {
pub fn new(name: &'a str, years: Option<u16>, auth_password: &'a str) -> Self {
pub fn new(name: &'a str, period: Option<Period>, 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 =
<DomainTransfer as Transaction<NoExtension>>::serialize_request(&object, None, CLTRID)