Skip to content

Commit

Permalink
Merge pull request #21 from unicode-rs/fmt
Browse files Browse the repository at this point in the history
Add Display impl for AugmentedScriptSet
  • Loading branch information
Manishearth authored Jun 24, 2020
2 parents 7ea5530 + c2f0cf6 commit 233de0a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/mixed_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
39 changes: 38 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 233de0a

Please sign in to comment.