crate::ix!();
#[derive(thiserror::Error,Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PostalCodeConstructionError {
UnsupportedCountry {
attempted_country: Country,
},
InvalidFormat {
attempted_code: String,
attempted_country: Option<Country>,
},
RegexInitializationError {
country: Country,
},
}
impl fmt::Display for PostalCodeConstructionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PostalCodeConstructionError::UnsupportedCountry { attempted_country } => {
write!(f, "Unsupported country: {}", attempted_country)
}
PostalCodeConstructionError::InvalidFormat { attempted_code, attempted_country } => {
match attempted_country {
Some(country) => write!(
f,
"Invalid postal code format '{}' for country '{}'",
attempted_code, country
),
None => write!(
f,
"Invalid postal code format '{}', country unspecified",
attempted_code
),
}
}
PostalCodeConstructionError::RegexInitializationError { country } => {
write!(f, "Regex initialization error for country '{}'", country)
}
}
}
}
impl From<derive_builder::UninitializedFieldError> for PostalCodeConstructionError {
fn from(_e: derive_builder::UninitializedFieldError) -> Self {
PostalCodeConstructionError::InvalidFormat {
attempted_code: "<unset>".to_string(),
attempted_country: None,
}
}
}