unic_ucd_core/lib.rs
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
12#![forbid(unsafe_code, missing_docs, unconditional_recursion)]
13
14//! # UNIC — UCD — Core
15//!
16//! A component of [`unic`: Unicode and Internationalization Crates for Rust](/unic/).
17//!
18//! Core create indicating the version of Unicode Character Database.
19
20
21use std::fmt;
22
23
24/// Type of `UNICODE_VERSION` value:
25#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Default)]
26pub struct UnicodeVersion {
27 /// Major version.
28 pub major: u16,
29
30 /// Minor version.
31 pub minor: u16,
32
33 /// Micro (or Update) version.
34 pub micro: u16,
35}
36
37
38/// The [Unicode version](http://www.unicode.org/versions/) of data
39pub const UNICODE_VERSION: UnicodeVersion = include!("../tables/unicode_version.rsv");
40
41
42impl UnicodeVersion {
43 /// Human-readable description of the Age property value.
44 #[inline]
45 pub fn display(&self) -> String {
46 format!("{}.{}.{}", self.major, self.minor, self.micro)
47 }
48}
49
50
51impl fmt::Display for UnicodeVersion {
52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 write!(f, "{}", self.display())
54 }
55}
56
57
58/// NOTE: (T, T, T) is the type of Rust's internal UNICODE_VERSION until Rust 1.20.
59impl<T: Into<u16>> From<(T, T, T)> for UnicodeVersion {
60 fn from(value: (T, T, T)) -> UnicodeVersion {
61 UnicodeVersion {
62 major: value.0.into(),
63 minor: value.1.into(),
64 micro: value.2.into(),
65 }
66 }
67}
68
69
70/// NOTE: (T, T, T) is the type of Rust's internal UNICODE_VERSION until Rust 1.20.
71impl<T: From<u16>> Into<(T, T, T)> for UnicodeVersion {
72 fn into(self) -> (T, T, T) {
73 (self.major.into(), self.minor.into(), self.micro.into())
74 }
75}
76
77
78// TODO: Add conversion to/from `std::char::UnicodeVersion` whenever it becomes accessible.
79
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn validate_version_values() {
87 assert!(UNICODE_VERSION.major > 0);
88
89 // Current release schedule of Unicode is to have one Major version update each year, with
90 // no Minor updates. We hard-code this internal policy while it stans.
91 assert!(UNICODE_VERSION.minor == 0);
92 }
93
94 #[test]
95 fn test_display() {
96 assert_eq!(
97 format!(
98 "Unicode {}",
99 UnicodeVersion {
100 major: 1,
101 minor: 2,
102 micro: 0,
103 }
104 ),
105 "Unicode 1.2.0"
106 );
107 }
108
109 #[test]
110 fn test_against_rust_core_type() {
111 // Same type as std::char::UNICODE_VERSION
112 let uni_ver: (u64, u64, u64) = (9, 0, 0);
113 assert!(uni_ver <= UNICODE_VERSION.into() || uni_ver > UNICODE_VERSION.into());
114 }
115}