From da19f750dc065a786a660564b9a91617312141b9 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 23 Jun 2020 14:40:35 -0700 Subject: [PATCH 1/2] Add Display impl for AugmentedScriptSet --- src/mixed_script.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/mixed_script.rs b/src/mixed_script.rs index a1c6df1..d1e17a5 100644 --- a/src/mixed_script.rs +++ b/src/mixed_script.rs @@ -105,6 +105,36 @@ 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("Krorean") } 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) { From c2f0cf6e87d7638e6c695cea5d9a25908cf0ad57 Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Wed, 24 Jun 2020 08:31:04 +0800 Subject: [PATCH 2/2] Fix typo and add a new test --- src/mixed_script.rs | 8 ++++++-- src/tests.rs | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/mixed_script.rs b/src/mixed_script.rs index d1e17a5..bffc102 100644 --- a/src/mixed_script.rs +++ b/src/mixed_script.rs @@ -113,9 +113,13 @@ impl fmt::Display for AugmentedScriptSet { write!(f, "All")?; } else { let mut first_entry = true; - let hanb = if self.hanb { Some("Han with Bopomofo") } else { None }; + 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("Krorean") } else { None }; + let kore = if self.kore { Some("Korean") } else { None }; for writing_system in None .into_iter() .chain(hanb) 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); + } +}