unic_char_property/
range_types.rs

1// Copyright 2017 The UNIC Project Developers.
2//
3// See the COPYRIGHT file at the top-level directory of this distribution.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Character Property Range types.
12//!
13//! NOTE: At the moment, it is not possible to define a marker for all character property range
14//! types and enforce their implementation from `CharProperty`.  We need to fix this whenever the
15//! compiler becomes able to do to so.
16
17use super::property::CharProperty;
18
19// == Enumerated/Catalog Types ==
20
21/// A Character Property with enumerated values.
22///
23/// This is similar to types *Enumeration* and *Catalog*, as defined in UAX#44.
24///
25/// Usage Note: If the property is of type *Catalog*, it's recommended to (in some way) mark the
26/// type as *non-exhaustive*, so that adding new variants to the `enum` type won't result in API
27/// breakage.
28pub trait EnumeratedCharProperty: Sized + CharProperty {
29    /// Exhaustive list of all property values.
30    fn all_values() -> &'static [Self];
31
32    /// The *abbreviated name* of the property value.
33    fn abbr_name(&self) -> &'static str;
34
35    /// The *long name* of the property value.
36    fn long_name(&self) -> &'static str;
37
38    /// The *human-readable name* of the property value.
39    fn human_name(&self) -> &'static str;
40}
41
42// == Binary Types ==
43
44/// A Character Property with binary values.
45///
46/// Examples: `Alphabetic`, `Bidi_Mirrored`, `White_Space`
47pub trait BinaryCharProperty: CharProperty {
48    /// The boolean value of the property value.
49    fn as_bool(&self) -> bool;
50
51    /// The *abbreviated name* of the property value.
52    fn abbr_name(&self) -> &'static str {
53        if self.as_bool() {
54            "Y"
55        } else {
56            "N"
57        }
58    }
59
60    /// The *long name* of the property value.
61    fn long_name(&self) -> &'static str {
62        if self.as_bool() {
63            "Yes"
64        } else {
65            "No"
66        }
67    }
68
69    /// The *human-readable name* of the property value.
70    fn human_name(&self) -> &'static str {
71        if self.as_bool() {
72            "Yes"
73        } else {
74            "No"
75        }
76    }
77}
78
79// == Numeric Types ==
80
81/// Marker for numeric types accepted by `NumericCharProperty`.
82pub trait NumericCharPropertyValue {}
83
84impl NumericCharPropertyValue for u8 {}
85
86/// A Character Property with numeric values.
87///
88/// Examples: `Numeric_Value`, `Canonical_Combining_Class`
89pub trait NumericCharProperty<NumericValue: NumericCharPropertyValue>: CharProperty {
90    /// The numeric value for the property value.
91    fn number(&self) -> NumericValue;
92}
93
94// == Custom Types ==
95
96/// A Character Property with custom values.
97///
98/// Custom values means any non-enumerated, non-numeric value.
99///
100/// Examples: `Age` property that returns a `UnicodeVersion` value.
101pub trait CustomCharProperty<Value>: CharProperty {
102    /// The actual (inner) value for the property value.
103    fn actual(&self) -> Value;
104}