|
8 | 8 | // option. This file may not be copied, modified, or distributed |
9 | 9 | // except according to those terms. |
10 | 10 |
|
11 | | -use unic_utils::CharDataTable; |
| 11 | + |
| 12 | +use std::fmt; |
| 13 | + |
| 14 | +use unic_utils::{CharDataTable, CharProperty, EnumeratedCharProperty}; |
| 15 | + |
12 | 16 |
|
13 | 17 | /// Represents the Unicode Character |
14 | 18 | /// [*General_Category*](http://unicode.org/reports/tr44/#General_Category) property. |
15 | 19 | /// |
16 | 20 | /// This is a useful breakdown into various character types which can be used as a default |
17 | 21 | /// categorization in implementations. For the property values, see |
18 | 22 | /// [*General_Category Values*](http://unicode.org/reports/tr44/#General_Category_Values). |
19 | | -#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 23 | +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
20 | 24 | pub enum GeneralCategory { |
21 | 25 | /// An uppercase letter (Short form: `Lu`) |
22 | 26 | UppercaseLetter, |
@@ -80,6 +84,21 @@ pub enum GeneralCategory { |
80 | 84 | Unassigned, |
81 | 85 | } |
82 | 86 |
|
| 87 | + |
| 88 | +impl CharProperty for GeneralCategory { |
| 89 | + fn of(ch: char) -> Self { |
| 90 | + Self::of(ch) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +impl EnumeratedCharProperty for GeneralCategory { |
| 96 | + fn all_values() -> &'static [Self] { |
| 97 | + Self::all_values() |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | + |
83 | 102 | pub mod abbr_names { |
84 | 103 | pub use super::GeneralCategory::UppercaseLetter as Lu; |
85 | 104 | pub use super::GeneralCategory::LowercaseLetter as Ll; |
@@ -125,8 +144,6 @@ impl GeneralCategory { |
125 | 144 | } |
126 | 145 |
|
127 | 146 | /// Exhaustive list of all `GeneralCategory` property values. |
128 | | - /// |
129 | | - /// Reference: <http://unicode.org/reports/tr44/#General_Category_Values> |
130 | 147 | pub fn all_values() -> &'static [GeneralCategory] { |
131 | 148 | use GeneralCategory::*; |
132 | 149 | const ALL_VALUES: &[GeneralCategory] = &[ |
@@ -163,8 +180,16 @@ impl GeneralCategory { |
163 | 180 | ]; |
164 | 181 | ALL_VALUES |
165 | 182 | } |
| 183 | + |
| 184 | + /// Human-readable description of the property value. |
| 185 | + // TODO: Needs to be improved by returning long-name with underscores replaced by space. |
| 186 | + #[inline] |
| 187 | + pub fn display(&self) -> String { |
| 188 | + format!("{:?}", self).to_owned() |
| 189 | + } |
166 | 190 | } |
167 | 191 |
|
| 192 | + |
168 | 193 | impl GeneralCategory { |
169 | 194 | /// `Lu` | `Ll` | `Lt` (Short form: `LC`) |
170 | 195 | pub fn is_cased_letter(&self) -> bool { |
@@ -207,6 +232,21 @@ impl GeneralCategory { |
207 | 232 | } |
208 | 233 | } |
209 | 234 |
|
| 235 | + |
| 236 | +impl Default for GeneralCategory { |
| 237 | + fn default() -> Self { |
| 238 | + GeneralCategory::Unassigned |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | + |
| 243 | +impl fmt::Display for GeneralCategory { |
| 244 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 245 | + write!(f, "{}", self.display()) |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | + |
210 | 250 | #[cfg(test)] |
211 | 251 | mod tests { |
212 | 252 | use super::GeneralCategory as GC; |
@@ -305,4 +345,11 @@ mod tests { |
305 | 345 | assert_eq!(GC::of(c), GC::Unassigned); |
306 | 346 | } |
307 | 347 | } |
| 348 | + |
| 349 | + #[test] |
| 350 | + fn test_display() { |
| 351 | + //assert_eq!(format!("{}", GC::UppercaseLetter), "Uppercase Letter"); |
| 352 | + assert_eq!(format!("{}", GC::UppercaseLetter), "UppercaseLetter"); |
| 353 | + assert_eq!(format!("{}", GC::Unassigned), "Unassigned"); |
| 354 | + } |
308 | 355 | } |
0 commit comments