unic_char_property

Macro char_property

Source
macro_rules! char_property {
    (
        $(#[$prop_meta:meta])*
        pub enum $prop_name:ident {
            abbr => $prop_abbr:expr;
            long => $prop_long:expr;
            human => $prop_human:expr;

            $(
                $(#[$variant_meta:meta])*
                $variant_name:ident {
                    abbr => $variant_abbr:ident,
                    long => $variant_long:ident,
                    human => $variant_human:expr,
                }
            )*
        }

        $(#[$abbr_mod_meta:meta])*
        pub mod $abbr_mod:ident for abbr;

        $(#[$long_mod_meta:meta])*
        pub mod $long_mod:ident for long;

    ) => { ... };
    (
        $(#[$prop_meta:meta])*
        pub struct $prop_name:ident(bool) {
            abbr => $prop_abbr:expr;
            long => $prop_long:expr;
            human => $prop_human:expr;

            data_table_path => $data_path:expr;
        }

        $(#[$is_fn_meta:meta])*
        pub fn $is_fn:ident(char) -> bool;

    ) => { ... };
    (
        __impl CharProperty for $prop_name:ident;
        $prop_abbr:expr;
        $prop_long:expr;
        $prop_human:expr;
    ) => { ... };
    (
        __impl FromStr for $prop_name:ident;
        $( $id:expr => $value:expr; )*
    ) => { ... };
    ( __impl Display for $prop_name:ident by $trait:ident ) => { ... };
}
Expand description

Macro for declaring a character property.

§Syntax (Enumerated Property)

#[macro_use]
extern crate unic_char_property;

// First we define the type itself.
char_property! {
    /// This is the enum type created for the character property.
    pub enum MyProp {
        abbr => "AbbrPropName";
        long => "Long_Property_Name";
        human => "Human-Readable Property Name";

        /// Zero or more documentation or other attributes.
        RustName {
            abbr => AbbrName,
            long => Long_Name,
            human => "&'static str that is a nicer presentation of the name",
        }
    }

    /// Module aliasing property value abbreviated names.
    pub mod abbr_names for abbr;

    /// Module aliasing property value long names.
    pub mod long_names for long;
}

// We also need to impl `PartialCharProperty` or `TotalCharProperty` manually.

§Syntax (Binary Property)

#[macro_use] extern crate unic_char_property;

char_property! {
    /// This is the newtype used for the character property.
    pub struct MyProp(bool) {
        abbr => "AbbrPropName";
        long => "Long_Property_Name";
        human => "Human-Readable Property Name";

        // Unlike an enumerated property, a binary property will handle the table for you.
        data_table_path => "../tests/tables/property_table.rsv";
    }

    /// A function that returns whether the given character has the property or not.
    pub fn is_prop(char) -> bool;
}

// You may also want to create a trait for easy access to the properties you define.

§Effect

  • Implements the CharProperty trait and appropriate range trait
  • Implements FromStr accepting either the abbr or long name, ascii case insensitive
  • Implements Display using the human string
  • Populates the module abbr_names with pub use bindings of variants to their abbr names (Enumerated properties only)
  • Populates the module long_names with pub use bindings of variants to their long names (Enumerated properties only)
  • Maintains all documentation comments and other #[attributes] as would be expected (with some limitations, listed below)