use crate::text::{Charset, TextSpec, Ws, digits, text_type};
const fn digits_spec(
name: &'static str,
min: usize,
max: usize,
check: fn(&str) -> bool,
expected: &'static str,
) -> TextSpec {
TextSpec { name, min, max, charset: Charset::Wbci, ws: Ws::Preserve, pattern: Some((check, expected)) }
}
const fn wbci(name: &'static str, min: usize, max: usize, ws: Ws) -> TextSpec {
TextSpec { name, min, max, charset: Charset::Wbci, ws, pattern: None }
}
text_type!(
Onkz,
digits_spec("Onkz", 2, 5, |s| digits(s, 2, 5) && !s.starts_with('0'), r"[1-9]\d{1,4}"),
plain
);
text_type!(
Rufnummer,
digits_spec("Rufnummer", 1, 14, |s| digits(s, 1, 14), r"\d{1,14}"),
redact
);
text_type!(
Durchwahlnummer,
digits_spec("Durchwahlnummer", 1, 8, |s| digits(s, 1, 8), r"\d{1,8}"),
redact
);
text_type!(
Abfragestelle,
digits_spec("Abfragestelle", 1, 6, |s| digits(s, 1, 6), r"\d{1,6}"),
plain
);
text_type!(
RufnummernBlock,
digits_spec("RufnummernBlock", 1, 6, |s| digits(s, 1, 6), r"\d{1,6}"),
plain
);
text_type!(
Postleitzahl,
digits_spec("Postleitzahl", 5, 5, |s| digits(s, 5, 5), r"\d{5}"),
plain
);
text_type!(
Hausnummer,
digits_spec("Hausnummer", 1, 4, |s| digits(s, 1, 4), r"\d{1,4}"),
redact
);
text_type!(
Vertragsnummer,
TextSpec { name: "Vertragsnummer", min: 1, max: 10, charset: Charset::Xml, ws: Ws::Preserve, pattern: None },
plain
);
text_type!(
Technologie,
TextSpec {
name: "Technologie",
min: 1,
max: 25,
charset: Charset::Xml,
ws: Ws::Preserve,
pattern: Some((|s| s.get(..3).is_some_and(|d| digits(d, 3, 3)) && s.get(3..4) == Some(" "), "[0-9]{3} .*")),
},
plain
);
text_type!(
Vorname,
wbci("Vorname", 1, 30, Ws::Collapse),
redact
);
text_type!(
Nachname,
wbci("Nachname", 1, 30, Ws::Collapse),
redact
);
text_type!(
Firmenname,
wbci("Firmenname", 1, 30, Ws::Collapse),
redact
);
text_type!(
Strassenname,
wbci("Strassenname", 1, 40, Ws::Collapse),
redact
);
text_type!(
Ortsname,
wbci("Ortsname", 1, 40, Ws::Collapse),
plain
);
text_type!(
HausnummernZusatz,
wbci("HausnummernZusatz", 1, 6, Ws::Collapse),
redact
);
text_type!(
Identifizierer,
wbci("Identifizierer", 1, 20, Ws::Preserve),
plain
);
text_type!(
Projektkenner,
wbci("Projektkenner", 1, 30, Ws::Preserve),
plain
);
text_type!(
Kopplungskenner,
wbci("Kopplungskenner", 1, 30, Ws::Preserve),
plain
);
text_type!(
Freitext,
TextSpec { name: "Freitext", min: 1, max: 255, charset: Charset::WbciMultiline, ws: Ws::Preserve, pattern: None },
redact
);
text_type!(
Meldungstext,
wbci("Meldungstext", 1, 255, Ws::Collapse),
plain
);
pub(crate) use carrier_core::code_enum;
impl Meldungstext {
pub(crate) fn dash() -> Self {
Self("-".to_owned())
}
}
code_enum!(
Anrede {
Herr = "1",
Frau = "2",
Keine = "9",
}
);
code_enum!(
AnredeFirma {
Firma = "4",
Keine = "9",
}
);
code_enum!(
Portierungszeitfenster {
Zf1 = "ZF1",
Zf2 = "ZF2",
Zf3 = "ZF3",
}
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn numbers() {
assert!("30".parse::<Onkz>().is_ok());
assert!("030".parse::<Onkz>().is_err());
assert!("3".parse::<Onkz>().is_err());
assert!("12345678901234".parse::<Rufnummer>().is_ok());
assert!("123456789012345".parse::<Rufnummer>().is_err());
assert!("12a".parse::<Rufnummer>().is_err());
assert!("10115".parse::<Postleitzahl>().is_ok());
assert!("1011".parse::<Postleitzahl>().is_err());
assert!("12345".parse::<Hausnummer>().is_err());
}
#[test]
fn technologie_needs_three_digits_and_a_space() {
assert!("001 TAL ISDN".parse::<Technologie>().is_ok());
assert!("01 TAL".parse::<Technologie>().is_err());
assert!("001TAL".parse::<Technologie>().is_err());
}
#[test]
fn personal_data_is_redacted_in_debug() {
let name = Nachname::new("Mustermann").unwrap();
assert_eq!(format!("{name:?}"), "Nachname(<redacted, 10 chars>)");
assert_eq!(name.to_string(), "Mustermann");
let nr = Rufnummer::new("1234567").unwrap();
assert!(!format!("{nr:?}").contains("1234567"));
assert_eq!(format!("{:?}", Ortsname::new("Berlin").unwrap()), "Ortsname(\"Berlin\")");
}
#[test]
fn enums_round_trip_their_codes() {
assert_eq!(Anrede::from_code("2"), Some(Anrede::Frau));
assert_eq!(AnredeFirma::Firma.as_str(), "4");
assert_eq!(Portierungszeitfenster::from_code("ZF2"), Some(Portierungszeitfenster::Zf2));
assert_eq!(Portierungszeitfenster::from_code("ZF4"), None);
}
}