1crate::ix!();
2
3#[derive(thiserror::Error,Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
5pub enum PostalCodeConstructionError {
6 UnsupportedCountry {
8 attempted_country: Country,
10 },
11 InvalidFormat {
13 attempted_code: String,
15 attempted_country: Option<Country>,
17 },
18 RegexInitializationError {
20 country: Country,
22 },
23}
24
25impl fmt::Display for PostalCodeConstructionError {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 match self {
28 PostalCodeConstructionError::UnsupportedCountry { attempted_country } => {
29 write!(f, "Unsupported country: {}", attempted_country)
30 }
31 PostalCodeConstructionError::InvalidFormat { attempted_code, attempted_country } => {
32 match attempted_country {
33 Some(country) => write!(
34 f,
35 "Invalid postal code format '{}' for country '{}'",
36 attempted_code, country
37 ),
38 None => write!(
39 f,
40 "Invalid postal code format '{}', country unspecified",
41 attempted_code
42 ),
43 }
44 }
45 PostalCodeConstructionError::RegexInitializationError { country } => {
46 write!(f, "Regex initialization error for country '{}'", country)
47 }
48 }
49 }
50}
51
52impl From<derive_builder::UninitializedFieldError> for PostalCodeConstructionError {
53 fn from(_e: derive_builder::UninitializedFieldError) -> Self {
54 PostalCodeConstructionError::InvalidFormat {
56 attempted_code: "<unset>".to_string(),
57 attempted_country: None,
58 }
59 }
60}