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
mod err;

use std::fmt;
use std::char;

pub use self::err::{EmotionError, Result};

#[repr(u32)]
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
pub enum Emotion {
    /// Symbol '_'.
    None = 0x5f,
    /// Symbol 'a'.
    Angry = 0x61,
    /// Symbol 'h'.
    Happy = 0x68,
    /// Symbol 'l'.
    Love = 0x6c,
    /// Symbol 'm'.
    Malicious = 0x6d,
    /// Symbol 'i'.
    Misunderstanding = 0x69,
    /// Symbol 'o'.
    Shocked = 0x6f,
    /// Symbol 's'.
    Sleepy = 0x73,
    /// Symbol 'e'.
    Speechless = 0x65,
}

impl Emotion {
    pub fn new(content: &str) -> Result<Self> {
        match content {
            "Angry" => Ok(Emotion::Angry),
            "Happy" => Ok(Emotion::Happy),
            "Love" => Ok(Emotion::Love),
            "Malicious" => Ok(Emotion::Malicious),
            "Misunderstanding" => Ok(Emotion::Misunderstanding),
            "Shocked" => Ok(Emotion::Shocked),
            "Sleepy" => Ok(Emotion::Sleepy),
            "Speechless" => Ok(Emotion::Speechless),
            "None" => Ok(Emotion::None),
            name => Err(EmotionError::UnknownEmotion(name.to_string())),
        }
    }

    /// The accessor method `is_none` returns a boolean
    /// for None, axiom of emotion.
    pub fn is_none(&self) -> bool {
        self.eq(&Emotion::None)
    }

    pub fn not_empty(&self) -> Option<&Emotion> {
        match *self {
            Emotion::None => None,
            ref other => Some(other),
        }
    }
}

impl fmt::Display for Emotion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", unsafe {
            char::from_u32_unchecked(*self as u32)
        })
    }
}

impl Default for Emotion {
    fn default() -> Emotion {
        Emotion::None
    }
}