feat!: turn `Period` into an enum whose variants represent the unit

This commit is contained in:
gibbz00 2024-10-18 09:46:45 +02:00 committed by Dirkjan Ochtman
parent 4973a4fe1d
commit cd556f2481
1 changed files with 45 additions and 39 deletions

View File

@ -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 const TWO_YEARS: Period = Period { pub fn months(length: u8) -> Result<Self, Error> {
unit: 'y', PeriodLength::new(length).map(Self::Months)
length: 2, }
}; }
pub const THREE_YEARS: Period = Period { impl ToXml for Period {
unit: 'y', fn serialize<W: fmt::Write + ?Sized>(
length: 3, &self,
}; _: Option<instant_xml::Id<'_>>,
serializer: &mut Serializer<W>,
) -> Result<(), instant_xml::Error> {
const ELEMENT: &str = "period";
pub const ONE_MONTH: Period = Period { let (unit, length) = match self {
unit: 'm', Self::Years(length) => ('y', length.0),
length: 1, Self::Months(length) => ('m', length.0),
}; };
pub const SIX_MONTHS: Period = Period { serializer.write_start(ELEMENT, XMLNS)?;
unit: 'm', serializer.write_attr("unit", XMLNS, &unit)?;
length: 6, serializer.end_start()?;
}; serializer.write_str(&length)?;
serializer.write_close(None, ELEMENT)
}
}
pub const ONE_YEAR: Period = Period::Years(PeriodLength(1));
pub const TWO_YEARS: Period = Period::Years(PeriodLength(2));
pub const THREE_YEARS: Period = Period::Years(PeriodLength(3));
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)]