Skip to content

Commit 233de0a

Browse files
authored
Merge pull request #21 from unicode-rs/fmt
Add Display impl for AugmentedScriptSet
2 parents 7ea5530 + c2f0cf6 commit 233de0a

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/mixed_script.rs

+34
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,40 @@ impl Debug for AugmentedScriptSet {
105105
}
106106
}
107107

108+
impl fmt::Display for AugmentedScriptSet {
109+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110+
if self.is_empty() {
111+
write!(f, "Empty")?;
112+
} else if self.is_all() {
113+
write!(f, "All")?;
114+
} else {
115+
let mut first_entry = true;
116+
let hanb = if self.hanb {
117+
Some("Han with Bopomofo")
118+
} else {
119+
None
120+
};
121+
let jpan = if self.jpan { Some("Japanese") } else { None };
122+
let kore = if self.kore { Some("Korean") } else { None };
123+
for writing_system in None
124+
.into_iter()
125+
.chain(hanb)
126+
.chain(jpan)
127+
.chain(kore)
128+
.chain(self.base.iter().map(Script::full_name))
129+
{
130+
if !first_entry {
131+
write!(f, ", ")?;
132+
} else {
133+
first_entry = false;
134+
}
135+
write!(f, "{}", writing_system)?;
136+
}
137+
}
138+
Ok(())
139+
}
140+
}
141+
108142
impl AugmentedScriptSet {
109143
/// Intersect this set with another
110144
pub fn intersect_with(&mut self, other: Self) {

src/tests.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn test_potential_mixed_script_detection() {
7979
}
8080

8181
#[test]
82-
fn test_augmented_script_set() {
82+
fn test_augmented_script_set_fmt_debug() {
8383
use crate::mixed_script::AugmentedScriptSet;
8484
let augmented_script_sets = vec![
8585
AugmentedScriptSet::default(),
@@ -114,3 +114,40 @@ fn test_augmented_script_set() {
114114
assert_eq!(format!("{:?}", ss), output);
115115
}
116116
}
117+
118+
#[test]
119+
fn test_augmented_script_set_fmt_display() {
120+
use crate::mixed_script::AugmentedScriptSet;
121+
let augmented_script_sets = vec![
122+
AugmentedScriptSet::default(),
123+
AugmentedScriptSet::from('0'),
124+
AugmentedScriptSet::from('a'),
125+
AugmentedScriptSet::from('μ'),
126+
AugmentedScriptSet::from('汉'),
127+
AugmentedScriptSet::from('ひ'),
128+
AugmentedScriptSet::from('カ'),
129+
AugmentedScriptSet::from('한'),
130+
AugmentedScriptSet::from("汉ひ"),
131+
AugmentedScriptSet::from("汉a"),
132+
AugmentedScriptSet::from("汉μ"),
133+
AugmentedScriptSet::from("〆切"),
134+
];
135+
let debug_output = vec![
136+
"All",
137+
"All",
138+
"Latin",
139+
"Greek",
140+
"Han with Bopomofo, Japanese, Korean, Han",
141+
"Japanese, Hiragana",
142+
"Japanese, Katakana",
143+
"Korean, Hangul",
144+
"Japanese",
145+
"Empty",
146+
"Empty",
147+
"Han with Bopomofo, Japanese, Korean, Han",
148+
];
149+
150+
for (ss, output) in augmented_script_sets.into_iter().zip(debug_output) {
151+
assert_eq!(format!("{}", ss), output);
152+
}
153+
}

0 commit comments

Comments
 (0)