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
use super::{Hour, HourOutOfRange};
use crate::{Count, CountBase};

/// The hour in the 24-hour digital clock.
///
/// It can be instantiated via a *fallible* conversion from [u8],
/// succeeding if the integer is in the 0..=23 range; otherwise,
/// [HourOutOfRange] is returned.
///
/// ```
/// use chinese_format::{*, gregorian::*};
/// # fn main() -> GenericResult<()> {
/// let midnight: Hour24 = 0.try_into()?;
///
/// assert_eq!(midnight.to_chinese(Variant::Simplified), "零点");
/// assert_eq!(midnight.to_chinese(Variant::Traditional), "零點");
///
///
/// let two: Hour24 = 2.try_into()?;
///
/// assert_eq!(two.to_chinese(Variant::Simplified), "两点");
/// assert_eq!(two.to_chinese(Variant::Traditional), "兩點");
///
///
/// let before_midnight: Hour24 = 23.try_into()?;
///
/// assert_eq!(before_midnight.to_chinese(Variant::Simplified), "二十三点");
/// assert_eq!(before_midnight.to_chinese(Variant::Traditional), "二十三點");
///
///
/// let invalid_result: Result<Hour24, HourOutOfRange> = 24.try_into();
///
/// assert_eq!(invalid_result, Err(HourOutOfRange(24)));
///
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Hour24(Count);

impl Hour for Hour24 {
    fn clock_value(&self) -> &Count {
        &self.0
    }
}

impl TryFrom<u8> for Hour24 {
    type Error = HourOutOfRange;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        if value >= 24 {
            return Err(HourOutOfRange(value));
        }

        Ok(Hour24(Count(value as CountBase)))
    }
}