//! Form fields as posted: everything arrives as text and is trimmed here. use std::collections::{BTreeMap, HashMap}; pub type Fields = HashMap; /// Field name to message; templates read `errors.`. pub type Errors = BTreeMap; /// A field's value, trimmed; empty when absent. pub fn get(f: &Fields, key: &str) -> String { f.get(key).map(|s| s.trim().to_string()).unwrap_or_default() } /// Upper case without inner spaces: passport numbers, codes. pub fn compact_upper(s: &str) -> String { s.chars() .filter(|c| !c.is_whitespace()) .collect::() .to_uppercase() } pub fn err(errors: &mut Errors, field: &str, msg: impl Into) { errors .entry(field.to_string()) .or_insert_with(|| msg.into()); } #[cfg(test)] mod tests { use super::*; #[test] fn trims_and_compacts() { let mut f = Fields::new(); f.insert("a".into(), " x y ".into()); assert_eq!(get(&f, "a"), "x y"); assert_eq!(get(&f, "b"), ""); assert_eq!(compact_upper(" c01 x00t47 "), "C01X00T47"); let mut e = Errors::new(); err(&mut e, "a", "first"); err(&mut e, "a", "second"); assert_eq!(e["a"], "first"); } }