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 eabbe88 commit 03a8168
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 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)
}
}


impl CanonicalCombiningClass {
/// Find the character *Canonical_Combining_Class* property value.
pub fn of(ch: char) -> CanonicalCombiningClass {
Expand Down
3 changes: 1 addition & 2 deletions unic/ucd/normal/src/composition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ pub fn canonical_composition(c: char) -> Option<&'static ([(char, char)])> {
pub fn canonical_decomposition(c: char) -> Option<&'static [char]> {
const LOOKUP: &'static [(char, Slice)] =
include!("tables/canonical_decomposition_mapping_lookup.rsv");
const VALUES: &'static [char] =
include!("tables/canonical_decomposition_mapping_values.rsv");
const VALUES: &'static [char] = include!("tables/canonical_decomposition_mapping_values.rsv");
bsearch_lookup_table(c, LOOKUP, VALUES)
}

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.
41 changes: 37 additions & 4 deletions unic/utils/src/char_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
// 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,
Expand All @@ -26,7 +32,9 @@ where
}


/// TBD.
/// A Character Property defined for some characters.
///
/// Examples: *Decomposition_Type*, *Numeric_Type*
pub trait OptionCharProperty
where
Self: Copy + Debug + Display + Eq + Hash,
Expand All @@ -36,11 +44,36 @@ where
}


/// 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
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 03a8168

Please sign in to comment.