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
//! Emoji clock... what it says on the tin

extern crate chrono;
#[macro_use]
extern crate maplit;
#[macro_use]
extern crate lazy_static;

// Std lib
use std::collections::HashMap;
use std::fmt;

// Third party
use chrono::Timelike;

lazy_static! {
    static ref DIALS: HashMap<(u32, bool), char> = hashmap!(
        (1, true) => '🕐',
        (1, false) => '🕜',
        (2, true) => '🕑',
        (2, false) => '🕝',
        (3, true) => '🕒',
        (3, false) => '🕞',
        (4, true) => '🕓',
        (4, false) => '🕟',
        (5, true) => '🕔',
        (5, false) => '🕠',
        (6, true) => '🕕',
        (6, false) => '🕡',
        (7, true) => '🕖',
        (7, false) => '🕢',
        (8, true) => '🕗',
        (8, false) => '🕣',
        (9, true) => '🕘',
        (9, false) => '🕣',
        (10, true) => '🕙',
        (10, false) => '🕣',
        (11, true) => '🕚',
        (11, false) => '🕦',
        (12, true) => '🕛',
        (12, false) => '🕧'
    );
}

/// Renders a clock in emoji
pub enum Clock<T> {
    /// Dial with 12 hour time
    Dial(T),
    /// Dial with 12 hour time and a.m/p.m indication ( 🌞/🌙 )
    DialMeridiem(T),
}

impl<T> fmt::Display for Clock<T>
where
    T: Timelike,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Clock::Dial(time) => write!(
                f,
                "{}",
                DIALS
                    .get(&(time.hour12().1, time.minute() < 30))
                    .cloned()
                    .unwrap_or_else(|| '⌛')
            ),
            Clock::DialMeridiem(time) => {
                let (is_pm, hour) = time.hour12();
                write!(
                    f,
                    "{}{}",
                    DIALS
                        .get(&(hour, time.minute() < 30))
                        .cloned()
                        .unwrap_or_else(|| '⌛'),
                    if is_pm { '🌙' } else { '🌞' }
                )
            }
        }
    }
}