unicode_types 0.2.0

A mapping of all the unicode characters into convenience types (one enum per block of characters with one variant per character).
Documentation

/// An enum to represent all characters in the Ogham block.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Ogham {
    /// \u{1680}: ' '
    SpaceMark,
    /// \u{1681}: 'ᚁ'
    LetterBeith,
    /// \u{1682}: 'ᚂ'
    LetterLuis,
    /// \u{1683}: 'ᚃ'
    LetterFearn,
    /// \u{1684}: 'ᚄ'
    LetterSail,
    /// \u{1685}: 'ᚅ'
    LetterNion,
    /// \u{1686}: 'ᚆ'
    LetterUath,
    /// \u{1687}: 'ᚇ'
    LetterDair,
    /// \u{1688}: 'ᚈ'
    LetterTinne,
    /// \u{1689}: 'ᚉ'
    LetterColl,
    /// \u{168a}: 'ᚊ'
    LetterCeirt,
    /// \u{168b}: 'ᚋ'
    LetterMuin,
    /// \u{168c}: 'ᚌ'
    LetterGort,
    /// \u{168d}: 'ᚍ'
    LetterNgeadal,
    /// \u{168e}: 'ᚎ'
    LetterStraif,
    /// \u{168f}: 'ᚏ'
    LetterRuis,
    /// \u{1690}: 'ᚐ'
    LetterAilm,
    /// \u{1691}: 'ᚑ'
    LetterOnn,
    /// \u{1692}: 'ᚒ'
    LetterUr,
    /// \u{1693}: 'ᚓ'
    LetterEadhadh,
    /// \u{1694}: 'ᚔ'
    LetterIodhadh,
    /// \u{1695}: 'ᚕ'
    LetterEabhadh,
    /// \u{1696}: 'ᚖ'
    LetterOr,
    /// \u{1697}: 'ᚗ'
    LetterUilleann,
    /// \u{1698}: 'ᚘ'
    LetterIfin,
    /// \u{1699}: 'ᚙ'
    LetterEamhancholl,
    /// \u{169a}: 'ᚚ'
    LetterPeith,
    /// \u{169b}: '᚛'
    FeatherMark,
    /// \u{169c}: '᚜'
    ReversedFeatherMark,
}

impl Into<char> for Ogham {
    fn into(self) -> char {
        match self {
            Ogham::SpaceMark => '',
            Ogham::LetterBeith => '',
            Ogham::LetterLuis => '',
            Ogham::LetterFearn => '',
            Ogham::LetterSail => '',
            Ogham::LetterNion => '',
            Ogham::LetterUath => '',
            Ogham::LetterDair => '',
            Ogham::LetterTinne => '',
            Ogham::LetterColl => '',
            Ogham::LetterCeirt => '',
            Ogham::LetterMuin => '',
            Ogham::LetterGort => '',
            Ogham::LetterNgeadal => '',
            Ogham::LetterStraif => '',
            Ogham::LetterRuis => '',
            Ogham::LetterAilm => '',
            Ogham::LetterOnn => '',
            Ogham::LetterUr => '',
            Ogham::LetterEadhadh => '',
            Ogham::LetterIodhadh => '',
            Ogham::LetterEabhadh => '',
            Ogham::LetterOr => '',
            Ogham::LetterUilleann => '',
            Ogham::LetterIfin => '',
            Ogham::LetterEamhancholl => '',
            Ogham::LetterPeith => '',
            Ogham::FeatherMark => '',
            Ogham::ReversedFeatherMark => '',
        }
    }
}

impl std::convert::TryFrom<char> for Ogham {
    type Error = ();
    fn try_from(c: char) -> Result<Self, Self::Error> {
        match c {
            '' => Ok(Ogham::SpaceMark),
            '' => Ok(Ogham::LetterBeith),
            '' => Ok(Ogham::LetterLuis),
            '' => Ok(Ogham::LetterFearn),
            '' => Ok(Ogham::LetterSail),
            '' => Ok(Ogham::LetterNion),
            '' => Ok(Ogham::LetterUath),
            '' => Ok(Ogham::LetterDair),
            '' => Ok(Ogham::LetterTinne),
            '' => Ok(Ogham::LetterColl),
            '' => Ok(Ogham::LetterCeirt),
            '' => Ok(Ogham::LetterMuin),
            '' => Ok(Ogham::LetterGort),
            '' => Ok(Ogham::LetterNgeadal),
            '' => Ok(Ogham::LetterStraif),
            '' => Ok(Ogham::LetterRuis),
            '' => Ok(Ogham::LetterAilm),
            '' => Ok(Ogham::LetterOnn),
            '' => Ok(Ogham::LetterUr),
            '' => Ok(Ogham::LetterEadhadh),
            '' => Ok(Ogham::LetterIodhadh),
            '' => Ok(Ogham::LetterEabhadh),
            '' => Ok(Ogham::LetterOr),
            '' => Ok(Ogham::LetterUilleann),
            '' => Ok(Ogham::LetterIfin),
            '' => Ok(Ogham::LetterEamhancholl),
            '' => Ok(Ogham::LetterPeith),
            '' => Ok(Ogham::FeatherMark),
            '' => Ok(Ogham::ReversedFeatherMark),
            _ => Err(()),
        }
    }
}

impl Into<u32> for Ogham {
    fn into(self) -> u32 {
        let c: char = self.into();
        let hex = c
            .escape_unicode()
            .to_string()
            .replace("\\u{", "")
            .replace("}", "");
        u32::from_str_radix(&hex, 16).unwrap()
    }
}

impl std::convert::TryFrom<u32> for Ogham {
    type Error = ();
    fn try_from(u: u32) -> Result<Self, Self::Error> {
        if let Ok(c) = char::try_from(u) {
            Self::try_from(c)
        } else {
            Err(())
        }
    }
}

impl Iterator for Ogham {
    type Item = Self;
    fn next(&mut self) -> Option<Self> {
        let index: u32 = (*self).into();
        use std::convert::TryFrom;
        Self::try_from(index + 1).ok()
    }
}

impl Ogham {
    /// The character with the lowest index in this unicode block
    pub fn new() -> Self {
        Ogham::SpaceMark
    }

    /// The character's name, in sentence case
    pub fn name(&self) -> String {
        let s = std::format!("Ogham{:#?}", self);
        string_morph::to_sentence_case(&s)
    }
}