use chrono::NaiveDate; use serde::{Deserialize, Serialize}; use std::string::ToString; /// A specification to build invoice IDs with #[derive(Serialize, Deserialize)] pub enum InvoiceId { YearMonthId(u16, u8, usize), } impl ToString for InvoiceId { fn to_string(&self) -> String { match self { Self::YearMonthId(yr, mo, id) => format!("#{}-{:02}-{:04}", yr, mo, id), } } } /// An invoice for a specific project #[derive(Serialize, Deserialize)] pub struct Invoice { id: InvoiceId, client: String, project: String, date: NaiveDate, amount: usize, currency: String, vat: u8, } #[test] fn invoice_id_fmt() { let inv_id = InvoiceId::YearMonthId(2020, 06, 0055); assert_eq!(inv_id.to_string(), "#2020-06-0055".to_string()); }