1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2017 The UNIC Project Developers.
//
// See the COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.


#![forbid(unsafe_code, missing_docs, unconditional_recursion)]

//! # UNIC — UCD — Core
//!
//! A component of [`unic`: Unicode and Internationalization Crates for Rust](/unic/).
//!
//! Core create indicating the version of Unicode Character Database.


use std::fmt;


/// Type of `UNICODE_VERSION` value:
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Default)]
pub struct UnicodeVersion {
    /// Major version.
    pub major: u16,

    /// Minor version.
    pub minor: u16,

    /// Micro (or Update) version.
    pub micro: u16,
}


/// The [Unicode version](http://www.unicode.org/versions/) of data
pub const UNICODE_VERSION: UnicodeVersion = include!("../tables/unicode_version.rsv");


impl UnicodeVersion {
    /// Human-readable description of the Age property value.
    #[inline]
    pub fn display(&self) -> String {
        format!("{}.{}.{}", self.major, self.minor, self.micro)
    }
}


impl fmt::Display for UnicodeVersion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.display())
    }
}


/// NOTE: (T, T, T) is the type of Rust's internal UNICODE_VERSION until Rust 1.20.
impl<T: Into<u16>> From<(T, T, T)> for UnicodeVersion {
    fn from(value: (T, T, T)) -> UnicodeVersion {
        UnicodeVersion {
            major: value.0.into(),
            minor: value.1.into(),
            micro: value.2.into(),
        }
    }
}


/// NOTE: (T, T, T) is the type of Rust's internal UNICODE_VERSION until Rust 1.20.
impl<T: From<u16>> Into<(T, T, T)> for UnicodeVersion {
    fn into(self) -> (T, T, T) {
        (self.major.into(), self.minor.into(), self.micro.into())
    }
}


// TODO: Add conversion to/from `std::char::UnicodeVersion` whenever it becomes accessible.


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn validate_version_values() {
        assert!(UNICODE_VERSION.major > 0);

        // Current release schedule of Unicode is to have one Major version update each year, with
        // no Minor updates. We hard-code this internal policy while it stans.
        assert!(UNICODE_VERSION.minor == 0);
    }

    #[test]
    fn test_display() {
        assert_eq!(
            format!(
                "Unicode {}",
                UnicodeVersion {
                    major: 1,
                    minor: 2,
                    micro: 0,
                }
            ),
            "Unicode 1.2.0"
        );
    }

    #[test]
    fn test_against_rust_core_type() {
        // Same type as std::char::UNICODE_VERSION
        let uni_ver: (u64, u64, u64) = (9, 0, 0);
        assert!(uni_ver <= UNICODE_VERSION.into() || uni_ver > UNICODE_VERSION.into());
    }
}