diff --git a/src/mixed_script.rs b/src/mixed_script.rs index a1c6df1..bffc102 100644 --- a/src/mixed_script.rs +++ b/src/mixed_script.rs @@ -105,6 +105,40 @@ impl Debug for AugmentedScriptSet { } } +impl fmt::Display for AugmentedScriptSet { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.is_empty() { + write!(f, "Empty")?; + } else if self.is_all() { + write!(f, "All")?; + } else { + let mut first_entry = true; + let hanb = if self.hanb { + Some("Han with Bopomofo") + } else { + None + }; + let jpan = if self.jpan { Some("Japanese") } else { None }; + let kore = if self.kore { Some("Korean") } else { None }; + for writing_system in None + .into_iter() + .chain(hanb) + .chain(jpan) + .chain(kore) + .chain(self.base.iter().map(Script::full_name)) + { + if !first_entry { + write!(f, ", ")?; + } else { + first_entry = false; + } + write!(f, "{}", writing_system)?; + } + } + Ok(()) + } +} + impl AugmentedScriptSet { /// Intersect this set with another pub fn intersect_with(&mut self, other: Self) { diff --git a/src/tests.rs b/src/tests.rs index 6409113..903f385 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -79,7 +79,7 @@ fn test_potential_mixed_script_detection() { } #[test] -fn test_augmented_script_set() { +fn test_augmented_script_set_fmt_debug() { use crate::mixed_script::AugmentedScriptSet; let augmented_script_sets = vec![ AugmentedScriptSet::default(), @@ -114,3 +114,40 @@ fn test_augmented_script_set() { assert_eq!(format!("{:?}", ss), output); } } + +#[test] +fn test_augmented_script_set_fmt_display() { + use crate::mixed_script::AugmentedScriptSet; + let augmented_script_sets = vec![ + AugmentedScriptSet::default(), + AugmentedScriptSet::from('0'), + AugmentedScriptSet::from('a'), + AugmentedScriptSet::from('μ'), + AugmentedScriptSet::from('汉'), + AugmentedScriptSet::from('ひ'), + AugmentedScriptSet::from('カ'), + AugmentedScriptSet::from('한'), + AugmentedScriptSet::from("汉ひ"), + AugmentedScriptSet::from("汉a"), + AugmentedScriptSet::from("汉μ"), + AugmentedScriptSet::from("〆切"), + ]; + let debug_output = vec![ + "All", + "All", + "Latin", + "Greek", + "Han with Bopomofo, Japanese, Korean, Han", + "Japanese, Hiragana", + "Japanese, Katakana", + "Korean, Hangul", + "Japanese", + "Empty", + "Empty", + "Han with Bopomofo, Japanese, Korean, Han", + ]; + + for (ss, output) in augmented_script_sets.into_iter().zip(debug_output) { + assert_eq!(format!("{}", ss), output); + } +}