unic_ucd_name_aliases/
name_aliases.rs

1// Copyright 2018 The UNIC Project Developers.
2//
3// See the COPYRIGHT file at the top-level directory of this distribution.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11/// Types of Unicode Name Aliases
12#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
13pub enum NameAliasType {
14    /// Corrections for serious problems in the character names
15    NameCorrections,
16
17    /// ISO 6429 names for C0 and C1 control functions, and other
18    /// commonly occurring names for control codes
19    ControlCodeNames,
20
21    /// A few widely used alternate names for format characters
22    AlternateNames,
23
24    /// Several documented labels for C1 control code points which
25    /// were never actually approved in any standard
26    Figments,
27
28    /// Commonly occurring abbreviations (or acronyms) for control codes,
29    /// format characters, spaces, and variation selectors
30    NameAbbreviations,
31}
32
33impl NameAliasType {
34    /// Find types of available Name Aliases for the given character
35    pub fn of(ch: char) -> Option<&'static [NameAliasType]> {
36        data::NAME_ALIAS_TYPES.find(ch)
37    }
38}
39
40/// Find Name Aliases for the given character with the specified type
41pub fn name_aliases_of(
42    ch: char,
43    name_alias_type: NameAliasType,
44) -> Option<&'static [&'static str]> {
45    match name_alias_type {
46        NameAliasType::NameCorrections => data::CORRECTIONS.find(ch),
47        NameAliasType::ControlCodeNames => data::CONTROLS.find(ch),
48        NameAliasType::AlternateNames => data::ALTERNATES.find(ch),
49        NameAliasType::Figments => data::FIGMENTS.find(ch),
50        NameAliasType::NameAbbreviations => data::ABBREVIATIONS.find(ch),
51    }
52}
53
54mod data {
55    use unic_char_property::tables::CharDataTable;
56    pub const CORRECTIONS: CharDataTable<&[&str]> = include!("../tables/corrections.rsv");
57    pub const CONTROLS: CharDataTable<&[&str]> = include!("../tables/controls.rsv");
58    pub const ALTERNATES: CharDataTable<&[&str]> = include!("../tables/alternates.rsv");
59    pub const FIGMENTS: CharDataTable<&[&str]> = include!("../tables/figments.rsv");
60    pub const ABBREVIATIONS: CharDataTable<&[&str]> = include!("../tables/abbreviations.rsv");
61
62    // Bring all enum cases into scope, because NameAliasType is omitted
63    // in name_alias_types.rsv to save space
64    use crate::NameAliasType::{
65        AlternateNames,
66        ControlCodeNames,
67        Figments,
68        NameAbbreviations,
69        NameCorrections,
70    };
71    pub const NAME_ALIAS_TYPES: CharDataTable<&[crate::NameAliasType]> =
72        include!("../tables/name_alias_types.rsv");
73}