Skip to main content

unic_idna_mapping/
mapping.rs

1// Copyright 2016 The rust-url developers.
2// Copyright 2017 The UNIC Project Developers.
3//
4// See the COPYRIGHT file at the top-level directory of this distribution.
5//
6// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9// option. This file may not be copied, modified, or distributed
10// except according to those terms.
11
12/// Represents the IDNA Mapping status of the Unicode character.
13#[repr(u8)]
14#[derive(Copy, Clone, Debug, PartialEq, Eq)]
15pub enum Mapping {
16    /// Valid, and not modified.
17    Valid,
18
19    /// Removed from the string.
20    Ignored,
21
22    /// Replaced in the string.
23    Mapped(&'static str),
24
25    /// Valid if *Nontransitional Processing*, or Mapped if Transitional Processing.
26    Deviation(&'static str),
27
28    /// Not allowed, result in error.
29    Disallowed,
30
31    /// Disallowed if *UseSTD3ASCIIRules* flag is set, Valid otherwise.
32    DisallowedStd3Valid,
33
34    /// Disallowed if *UseSTD3ASCIIRules* flag is set, Mapped otherwise.
35    DisallowedStd3Mapped(&'static str),
36}
37
38mod data {
39    use super::Mapping::*;
40    use unic_char_property::tables::CharDataTable;
41
42    #[cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
43    pub const MAPPING: CharDataTable<super::Mapping> = include!("../tables/idna_mapping.rsv");
44}
45
46impl Mapping {
47    /// Get Mapping status of the character.
48    pub fn of(ch: char) -> Mapping {
49        data::MAPPING.find(ch).expect("Table is missing value")
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_mapping() {
59        use crate::Mapping::*;
60
61        assert_eq!(Mapping::of('\u{0}'), DisallowedStd3Valid);
62        assert_eq!(Mapping::of('-'), Valid);
63        assert_eq!(Mapping::of('A'), Mapped("a"));
64        assert_eq!(Mapping::of('\u{80}'), Disallowed);
65        assert_eq!(Mapping::of('\u{a0}'), DisallowedStd3Mapped(" "));
66        assert_eq!(Mapping::of('\u{ad}'), Ignored);
67        assert_eq!(Mapping::of('\u{200c}'), Deviation(""));
68    }
69}