use crate::{AddressErrorKind, Io, Nom, Parse, PartialAddress};
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
)]
#[serde(rename_all = "PascalCase")]
pub struct FireInspectionRaw {
name: String,
address: String,
class: Option<String>,
subclass: Option<String>,
}
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Deserialize,
serde::Serialize,
derive_more::Deref,
derive_more::DerefMut,
)]
pub struct FireInspectionsRaw(Vec<FireInspectionRaw>);
impl FireInspectionsRaw {
#[tracing::instrument(skip_all)]
pub fn from_csv<P: AsRef<std::path::Path>>(path: P) -> Result<Self, Io> {
let records = crate::from_csv(path)?;
Ok(FireInspectionsRaw(records))
}
}
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Serialize,
serde::Deserialize,
derive_getters::Getters,
derive_setters::Setters,
)]
#[setters(prefix = "with_", strip_option, borrow_self)]
pub struct FireInspection {
#[setters(doc = "Sets the value of the `name` field representing the business name.")]
name: String,
#[setters(doc = "Sets the value of the `address` field representing the business address.")]
address: PartialAddress,
class: Option<String>,
subclass: Option<String>,
}
impl TryFrom<FireInspectionRaw> for FireInspection {
type Error = Nom;
fn try_from(raw: FireInspectionRaw) -> Result<Self, Self::Error> {
match Parse::address(&raw.address) {
Ok((_, address)) => {
let mut upper_address = address.clone();
if let Some(identifier) = address.subaddress_identifier() {
upper_address.set_subaddress_identifier(&identifier.to_uppercase())
};
Ok(FireInspection {
name: raw.name,
address: upper_address,
class: raw.class,
subclass: raw.subclass,
})
}
Err(source) => Err(Nom::new(
raw.address.clone(),
source,
line!(),
file!().to_string(),
)),
}
}
}
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Deserialize,
serde::Serialize,
derive_more::Deref,
derive_more::DerefMut,
)]
pub struct FireInspections(Vec<FireInspection>);
impl FireInspections {
#[tracing::instrument(skip_all)]
pub fn from_csv<P: AsRef<std::path::Path>>(path: P) -> Result<Self, AddressErrorKind> {
let raw = FireInspectionsRaw::from_csv(path)?;
let mut records = Vec::new();
for record in raw.iter() {
records.push(FireInspection::try_from(record.clone())?);
}
Ok(FireInspections(records))
}
}