unic_char_property/
range_types.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2017 The UNIC Project Developers.
//
// See the COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Character Property Range types.
//!
//! NOTE: At the moment, it is not possible to define a marker for all character property range
//! types and enforce their implementation from `CharProperty`.  We need to fix this whenever the
//! compiler becomes able to do to so.

use super::property::CharProperty;

// == Enumerated/Catalog Types ==

/// A Character Property with enumerated values.
///
/// This is similar to types *Enumeration* and *Catalog*, as defined in UAX#44.
///
/// Usage Note: If the property is of type *Catalog*, it's recommended to (in some way) mark the
/// type as *non-exhaustive*, so that adding new variants to the `enum` type won't result in API
/// breakage.
pub trait EnumeratedCharProperty: Sized + CharProperty {
    /// Exhaustive list of all property values.
    fn all_values() -> &'static [Self];

    /// The *abbreviated name* of the property value.
    fn abbr_name(&self) -> &'static str;

    /// The *long name* of the property value.
    fn long_name(&self) -> &'static str;

    /// The *human-readable name* of the property value.
    fn human_name(&self) -> &'static str;
}

// == Binary Types ==

/// A Character Property with binary values.
///
/// Examples: `Alphabetic`, `Bidi_Mirrored`, `White_Space`
pub trait BinaryCharProperty: CharProperty {
    /// The boolean value of the property value.
    fn as_bool(&self) -> bool;

    /// The *abbreviated name* of the property value.
    fn abbr_name(&self) -> &'static str {
        if self.as_bool() {
            "Y"
        } else {
            "N"
        }
    }

    /// The *long name* of the property value.
    fn long_name(&self) -> &'static str {
        if self.as_bool() {
            "Yes"
        } else {
            "No"
        }
    }

    /// The *human-readable name* of the property value.
    fn human_name(&self) -> &'static str {
        if self.as_bool() {
            "Yes"
        } else {
            "No"
        }
    }
}

// == Numeric Types ==

/// Marker for numeric types accepted by `NumericCharProperty`.
pub trait NumericCharPropertyValue {}

impl NumericCharPropertyValue for u8 {}

/// A Character Property with numeric values.
///
/// Examples: `Numeric_Value`, `Canonical_Combining_Class`
pub trait NumericCharProperty<NumericValue: NumericCharPropertyValue>: CharProperty {
    /// The numeric value for the property value.
    fn number(&self) -> NumericValue;
}

// == Custom Types ==

/// A Character Property with custom values.
///
/// Custom values means any non-enumerated, non-numeric value.
///
/// Examples: `Age` property that returns a `UnicodeVersion` value.
pub trait CustomCharProperty<Value>: CharProperty {
    /// The actual (inner) value for the property value.
    fn actual(&self) -> Value;
}