| 1 | // Copyright 2017 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 | //! *Version* data types. |
| 12 | |
| 13 | use core::fmt; |
| 14 | |
| 15 | #[cfg(feature = "unstable")] |
| 16 | use core::char; |
| 17 | |
| 18 | /// Represents a *Unicode Version* type. |
| 19 | /// |
| 20 | /// UNIC's *Unicode Version* type is used for Unicode datasets and specifications, including The |
| 21 | /// Unicode Standard (TUS), Unicode Character Database (UCD), Common Local Data Repository (CLDR), |
| 22 | /// IDNA, Emoji, etc. |
| 23 | /// |
| 24 | /// TODO: *Unicode Version* is guaranteed to have three integer fields between 0 and 255. We are |
| 25 | /// going to switch over to `u8` after Unicode 11.0.0 release. |
| 26 | /// |
| 27 | /// Refs: |
| 28 | /// - <https://www.unicode.org/versions/> |
| 29 | /// - <https://www.unicode.org/L2/L2017/17222.htm#152-C3> |
| 30 | #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Default)] |
| 31 | pub struct UnicodeVersion { |
| 32 | /// Major version. |
| 33 | pub major: u16, |
| 34 | |
| 35 | /// Minor version. |
| 36 | pub minor: u16, |
| 37 | |
| 38 | /// Micro (or Update) version. |
| 39 | pub micro: u16, |
| 40 | } |
| 41 | |
| 42 | impl fmt::Display for UnicodeVersion { |
| 43 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 44 | write!(f, "{}.{}.{}", self.major, self.minor, self.micro) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[cfg(feature = "unstable")] |
| 49 | /// Convert from Rust's internal Unicode Version. |
| 50 | impl From<char::UnicodeVersion> for UnicodeVersion { |
| 51 | fn from(value: char::UnicodeVersion) -> UnicodeVersion { |
| 52 | UnicodeVersion { |
| 53 | major: value.major as u16, |
| 54 | minor: value.minor as u16, |
| 55 | micro: value.micro as u16, |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[cfg(test)] |
| 61 | mod tests { |
| 62 | #[cfg(feature = "unstable")] |
| 63 | #[test] |
| 64 | fn test_against_rust_internal() { |
| 65 | use core::char; |
| 66 | |
| 67 | use super::UnicodeVersion; |
| 68 | |
| 69 | let core_unicode_version: UnicodeVersion = char::UNICODE_VERSION.into(); |
| 70 | assert!(core_unicode_version.major >= 10); |
| 71 | } |
| 72 | } |