feat!: turn `Period` into an enum whose variants represent the unit
This commit is contained in:
parent
4973a4fe1d
commit
cd556f2481
|
@ -150,29 +150,19 @@ pub struct DomainContact<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The `<period>` type for registration, renewal or transfer on domain transactions
|
/// The `<period>` type for registration, renewal or transfer on domain transactions
|
||||||
#[derive(Clone, Copy, Debug, ToXml)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
#[xml(rename = "period", ns(XMLNS))]
|
pub enum Period {
|
||||||
pub struct Period {
|
Years(PeriodLength),
|
||||||
/// The interval (usually 'y' indicating years)
|
Months(PeriodLength),
|
||||||
#[xml(attribute)]
|
|
||||||
unit: char,
|
|
||||||
/// The length of the registration, renewal or transfer period (usually in years)
|
|
||||||
#[xml(direct)]
|
|
||||||
length: u8,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Period {
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub fn years(length: u8) -> Result<Self, Error> {
|
pub struct PeriodLength(u8);
|
||||||
Self::new(length, 'y')
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn months(length: u8) -> Result<Self, Error> {
|
impl PeriodLength {
|
||||||
Self::new(length, 'm')
|
pub fn new(length: u8) -> Result<Self, Error> {
|
||||||
}
|
|
||||||
|
|
||||||
fn new(length: u8, unit: char) -> Result<Self, Error> {
|
|
||||||
match length {
|
match length {
|
||||||
1..=99 => Ok(Self { length, unit }),
|
1..=99 => Ok(Self(length)),
|
||||||
0 | 100.. => Err(Error::Other(
|
0 | 100.. => Err(Error::Other(
|
||||||
"Period length must be greater than 0 and less than 100".into(),
|
"Period length must be greater than 0 and less than 100".into(),
|
||||||
)),
|
)),
|
||||||
|
@ -180,30 +170,46 @@ impl Period {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const ONE_YEAR: Period = Period {
|
impl Period {
|
||||||
unit: 'y',
|
pub fn years(length: u8) -> Result<Self, Error> {
|
||||||
length: 1,
|
PeriodLength::new(length).map(Self::Years)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn months(length: u8) -> Result<Self, Error> {
|
||||||
|
PeriodLength::new(length).map(Self::Months)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToXml for Period {
|
||||||
|
fn serialize<W: fmt::Write + ?Sized>(
|
||||||
|
&self,
|
||||||
|
_: Option<instant_xml::Id<'_>>,
|
||||||
|
serializer: &mut Serializer<W>,
|
||||||
|
) -> Result<(), instant_xml::Error> {
|
||||||
|
const ELEMENT: &str = "period";
|
||||||
|
|
||||||
|
let (unit, length) = match self {
|
||||||
|
Self::Years(length) => ('y', length.0),
|
||||||
|
Self::Months(length) => ('m', length.0),
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const TWO_YEARS: Period = Period {
|
serializer.write_start(ELEMENT, XMLNS)?;
|
||||||
unit: 'y',
|
serializer.write_attr("unit", XMLNS, &unit)?;
|
||||||
length: 2,
|
serializer.end_start()?;
|
||||||
};
|
serializer.write_str(&length)?;
|
||||||
|
serializer.write_close(None, ELEMENT)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub const THREE_YEARS: Period = Period {
|
pub const ONE_YEAR: Period = Period::Years(PeriodLength(1));
|
||||||
unit: 'y',
|
|
||||||
length: 3,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const ONE_MONTH: Period = Period {
|
pub const TWO_YEARS: Period = Period::Years(PeriodLength(2));
|
||||||
unit: 'm',
|
|
||||||
length: 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const SIX_MONTHS: Period = Period {
|
pub const THREE_YEARS: Period = Period::Years(PeriodLength(3));
|
||||||
unit: 'm',
|
|
||||||
length: 6,
|
pub const ONE_MONTH: Period = Period::Months(PeriodLength(1));
|
||||||
};
|
|
||||||
|
pub const SIX_MONTHS: Period = Period::Months(PeriodLength(6));
|
||||||
|
|
||||||
/// The `<authInfo>` tag for domain and contact transactions
|
/// The `<authInfo>` tag for domain and contact transactions
|
||||||
#[derive(Clone, Debug, FromXml, ToXml)]
|
#[derive(Clone, Debug, FromXml, ToXml)]
|
||||||
|
|
Loading…
Reference in New Issue