use crate::prelude::*;
#[derive(
Clone, Debug, Default, Display, Serialize, Deserialize, PartialEq, Eq, Hash, From, Deref,
)]
#[serde(transparent)]
pub struct InvoiceNumber(u16);
impl std::str::FromStr for InvoiceNumber {
type Err = crate::prelude::Error;
fn from_str(s: &str) -> Result<Self> {
s.parse::<u16>()
.map(InvoiceNumber)
.map_err(|_| Error::InvalidInvoiceNumberString {
invalid_string: s.to_owned(),
})
}
}
impl HasSample for InvoiceNumber {
fn sample() -> Self {
Self::from(9876)
}
fn sample_other() -> Self {
Self::from(1234)
}
}
#[cfg(test)]
mod tests {
use super::*;
type Sut = InvoiceNumber;
#[test]
fn equality() {
assert_eq!(Sut::sample(), Sut::sample());
assert_eq!(Sut::sample_other(), Sut::sample_other());
}
#[test]
fn inequality() {
assert_ne!(Sut::sample(), Sut::sample_other());
}
#[test]
fn test_invoice_number_sample() {
let sample = Sut::sample();
assert_eq!(*sample, 9876);
}
#[test]
fn test_invoice_number_default_is_zero() {
let default = Sut::default();
assert_eq!(*default, 0);
}
#[test]
fn test_from_str_valid() {
let invoice_number = Sut::from_str("1234").unwrap();
assert_eq!(*invoice_number, 1234);
}
#[test]
fn test_from_str_invalid() {
let result = Sut::from_str("invalid");
assert!(
result.is_err(),
"Expected error for invalid string, got: {:?}",
result
);
}
}