Skip to content

Commit

Permalink
[char_property] Add NumericCharProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
behnam committed Aug 9, 2017
1 parent 6888740 commit 0a3dd76
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
24 changes: 16 additions & 8 deletions unic/ucd/normal/src/canonical_combining_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use std::fmt;

use unic_utils::{CharDataTable, CharProperty};
use unic_utils::{CharDataTable, CharProperty, NumericCharProperty};


/// Represents *Canonical_Combining_Class* property of a Unicode character.
Expand All @@ -27,6 +27,21 @@ use unic_utils::{CharDataTable, CharProperty};
pub struct CanonicalCombiningClass(u8);


impl CharProperty for CanonicalCombiningClass {
fn of(ch: char) -> Self {
Self::of(ch)
}
}


impl NumericCharProperty<u8> for CanonicalCombiningClass {
/// Get numeric value for character property value
fn number(&self) -> u8 {
self.number()
}
}


// TODO: Once we fully adopt 1.20 change these to associated consts on CanonicalCombiningClass
/// *Canonical_Combining_Class*es by their name
#[allow(non_upper_case_globals)]
Expand Down Expand Up @@ -84,13 +99,6 @@ pub mod values {
}


impl CharProperty for CanonicalCombiningClass {
fn of(ch: char) -> Self {
Self::of(ch)
}
}


const CANONICAL_COMBINING_CLASS_VALUES: &'static [(char, char, CanonicalCombiningClass)] =
include!("tables/canonical_combining_class_values.rsv");

Expand Down
2 changes: 1 addition & 1 deletion unic/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
[![Crates.io](https://img.shields.io/crates/v/unic-utils.svg)](https://crates.io/crates/unic-utils)
[![Documentation](https://docs.rs/unic-utils/badge.svg)](https://docs.rs/unic-utils/)

This UNIC component provides utility libraries that do not depend on Unicode data.
This UNIC component provides utility libraries that do not depend on any data.
47 changes: 40 additions & 7 deletions unic/utils/src/char_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,71 @@
// except according to those terms.


//! TBD.
//! Taxonomy and Contracts for Character Property types.
//!
//! See also the list of types of character properties defined in the UCD:
//! <http://unicode.org/reports/tr44/#About_Property_Table>, in [Unicode® Standard Annex #44 —
//! Unicode Character Database](http://unicode.org/reports/tr44/#About_Property_Table)


use std::fmt::{Debug, Display};
use std::hash::Hash;


/// TBD.
/// A Character Property defined on all characters.
///
/// Examples: *Age*, *Name*, *General_Category*, *Bidi_Class*
pub trait CharProperty
where
Self: Copy + Debug + Default + Display + Eq + Hash,
{
/// TBD
/// Find the character property value.
fn of(ch: char) -> Self;
}


/// TBD.
/// A Character Property defined for some characters.
///
/// Examples: *Decomposition_Type*, *Numeric_Type*
pub trait OptionCharProperty
where
Self: Copy + Debug + Display + Eq + Hash,
{
/// TBD
/// Find the character property value, or None.
fn of(ch: char) -> Option<Self>;
}


/// TBD.
/// A Character Property with enumerated values.
///
/// This is similar to types *Catalog* and *Enumeration*, as defined in UAX#44.
///
/// Usage Note: If the property is of type *Catalog* (as defined by Unicode), 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
where
Self: Copy + Debug + Display + Eq + Hash,
{
/// TBD
/// Exhaustive list of all property values.
fn all_values() -> &'static [Self];
}


/// 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<Value>
where
Self: Copy + Debug + Default + Display + Eq + Hash,
Value: NumericCharPropertyValue,
{
/// Get numeric value for character property value
fn number(&self) -> Value;
}
9 changes: 7 additions & 2 deletions unic/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ pub const PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
pub const PKG_DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");


pub mod char_property;
pub mod codepoints;
pub mod tables;
pub mod char_property;


pub use char_property::{CharProperty, EnumeratedCharProperty, OptionCharProperty};
pub use char_property::{
CharProperty,
EnumeratedCharProperty,
NumericCharProperty,
OptionCharProperty,
};
pub use codepoints::iter_all_chars;
pub use tables::CharDataTable;

0 comments on commit 0a3dd76

Please sign in to comment.