Skip to content

Commit 1d0a740

Browse files
authored
Merge pull request #19 from crlf0710/impl_debug
Impl pretty-printing for AugmentedScriptSet
2 parents 7c89ffb + a5d5f09 commit 1d0a740

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ according to Unicode Technical Standard #39 rules.
1616
exclude = [ "target/*", "Cargo.lock" ]
1717

1818
[dependencies]
19-
unicode-script = { version = "0.4.0", default-features = false }
19+
unicode-script = { version = "0.5.1", default-features = false }
2020
unicode-normalization = { version = "0.1.12", default-features = false }
2121
std = { version = "1.0", package = "rustc-std-workspace-std", optional = true }
2222
core = { version = "1.0", package = "rustc-std-workspace-core", optional = true }

src/mixed_script.rs

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! [Mixed-script detection](https://www.unicode.org/reports/tr39/#Mixed_Script_Detection)
22
3+
use core::fmt::{self, Debug};
34
use unicode_script::{Script, ScriptExtension};
45

56
/// An Augmented script set, as defined by UTS 39
67
///
78
/// https://www.unicode.org/reports/tr39/#def-augmented-script-set
8-
#[derive(Copy, Clone, PartialEq, Debug, Hash, Eq)]
9+
#[derive(Copy, Clone, PartialEq, Hash, Eq)]
910
pub struct AugmentedScriptSet {
1011
/// The base ScriptExtension value
1112
pub base: ScriptExtension,
@@ -23,10 +24,7 @@ impl From<ScriptExtension> for AugmentedScriptSet {
2324
let mut jpan = false;
2425
let mut kore = false;
2526

26-
if ext == ScriptExtension::Single(Script::Common)
27-
|| ext == ScriptExtension::Single(Script::Inherited)
28-
|| ext.contains_script(Script::Han)
29-
{
27+
if ext.is_common() || ext.is_inherited() || ext.contains_script(Script::Han) {
3028
hanb = true;
3129
jpan = true;
3230
kore = true;
@@ -67,14 +65,46 @@ impl From<&'_ str> for AugmentedScriptSet {
6765
impl Default for AugmentedScriptSet {
6866
fn default() -> Self {
6967
AugmentedScriptSet {
70-
base: ScriptExtension::Single(Script::Common),
68+
base: Script::Common.into(),
7169
hanb: true,
7270
jpan: true,
7371
kore: true,
7472
}
7573
}
7674
}
7775

76+
impl Debug for AugmentedScriptSet {
77+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78+
if self.is_empty() {
79+
write!(f, "AugmentedScriptSet {{∅}}")?;
80+
} else if self.is_all() {
81+
write!(f, "AugmentedScriptSet {{ALL}}")?;
82+
} else {
83+
write!(f, "AugmentedScriptSet {{")?;
84+
let mut first_entry = true;
85+
let hanb = if self.hanb { Some("Hanb") } else { None };
86+
let jpan = if self.jpan { Some("Jpan") } else { None };
87+
let kore = if self.kore { Some("Kore") } else { None };
88+
for writing_system in None
89+
.into_iter()
90+
.chain(hanb)
91+
.chain(jpan)
92+
.chain(kore)
93+
.chain(self.base.iter().map(Script::short_name))
94+
{
95+
if !first_entry {
96+
write!(f, ", ")?;
97+
} else {
98+
first_entry = false;
99+
}
100+
write!(f, "{}", writing_system)?;
101+
}
102+
write!(f, "}}")?;
103+
}
104+
Ok(())
105+
}
106+
}
107+
78108
impl AugmentedScriptSet {
79109
/// Intersect this set with another
80110
pub fn intersect_with(&mut self, other: Self) {
@@ -91,8 +121,7 @@ impl AugmentedScriptSet {
91121

92122
/// Check if the set is "All" (Common or Inherited)
93123
pub fn is_all(&self) -> bool {
94-
self.base == ScriptExtension::Single(Script::Common)
95-
|| self.base == ScriptExtension::Single(Script::Inherited)
124+
self.base.is_common() || self.base.is_inherited()
96125
}
97126

98127
/// Construct an AugmentedScriptSet for a given character

src/restriction_level.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use crate::mixed_script::AugmentedScriptSet;
55
use crate::GeneralSecurityProfile;
6-
use unicode_script::{Script, ScriptExtension};
6+
use unicode_script::Script;
77

88
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
99
/// The [Restriction level](https://www.unicode.org/reports/tr39/#Restriction_Level_Detection)
@@ -64,7 +64,8 @@ impl RestrictionLevelDetection for &'_ str {
6464
return RestrictionLevel::SingleScript;
6565
} else if exclude_latin_set.kore || exclude_latin_set.hanb || exclude_latin_set.jpan {
6666
return RestrictionLevel::HighlyRestrictive;
67-
} else if let ScriptExtension::Single(script) = exclude_latin_set.base {
67+
} else if exclude_latin_set.base.len() == 1 {
68+
let script = exclude_latin_set.base.iter().next().unwrap();
6869
if script.is_recommended() && script != Script::Cyrillic && script != Script::Greek {
6970
return RestrictionLevel::ModeratelyRestrictive;
7071
}

src/tests.rs

+37
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,40 @@ fn test_potential_mixed_script_detection() {
7777
assert!(is_potential_mixed_script_confusable_char('A'));
7878
assert!(!is_potential_mixed_script_confusable_char('D'));
7979
}
80+
81+
#[test]
82+
fn test_augmented_script_set() {
83+
use crate::mixed_script::AugmentedScriptSet;
84+
let augmented_script_sets = vec![
85+
AugmentedScriptSet::default(),
86+
AugmentedScriptSet::from('0'),
87+
AugmentedScriptSet::from('a'),
88+
AugmentedScriptSet::from('μ'),
89+
AugmentedScriptSet::from('汉'),
90+
AugmentedScriptSet::from('ひ'),
91+
AugmentedScriptSet::from('カ'),
92+
AugmentedScriptSet::from('한'),
93+
AugmentedScriptSet::from("汉ひ"),
94+
AugmentedScriptSet::from("汉a"),
95+
AugmentedScriptSet::from("汉μ"),
96+
AugmentedScriptSet::from("〆切"),
97+
];
98+
let debug_output = vec![
99+
"AugmentedScriptSet {ALL}",
100+
"AugmentedScriptSet {ALL}",
101+
"AugmentedScriptSet {Latn}",
102+
"AugmentedScriptSet {Grek}",
103+
"AugmentedScriptSet {Hanb, Jpan, Kore, Hani}",
104+
"AugmentedScriptSet {Jpan, Hira}",
105+
"AugmentedScriptSet {Jpan, Kana}",
106+
"AugmentedScriptSet {Kore, Hang}",
107+
"AugmentedScriptSet {Jpan}",
108+
"AugmentedScriptSet {∅}",
109+
"AugmentedScriptSet {∅}",
110+
"AugmentedScriptSet {Hanb, Jpan, Kore, Hani}",
111+
];
112+
113+
for (ss, output) in augmented_script_sets.into_iter().zip(debug_output) {
114+
assert_eq!(format!("{:?}", ss), output);
115+
}
116+
}

0 commit comments

Comments
 (0)