whiskers.git / web / cat / src / mood.rs
mood.rsannotatedmood.rssource58 lines · 1.7 KB · raw
1//! The moods the site shows: a subset of the app's (`whiskers_pet::Mood`), which `pet` maps to.
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4pub enum Mood {
5    Idle,
6    Listening,
7    Thinking,
8    Speaking,
9    Dizzy,
10    Sleeping,
11}
12
13impl Mood {
14    pub const ALL: [Mood; 6] = [Mood::Idle, Mood::Listening, Mood::Thinking, Mood::Speaking, Mood::Dizzy, Mood::Sleeping];
15
16    /// The word the page and its signals use.
17    pub fn name(self) -> &'static str {
18        match self {
19            Mood::Idle => "idle",
20            Mood::Listening => "listening",
21            Mood::Thinking => "thinking",
22            Mood::Speaking => "speaking",
23            Mood::Dizzy => "dizzy",
24            Mood::Sleeping => "sleeping",
25        }
26    }
27
28    /// The core's mood of the same name. An exhaustive match: a mood added here is not drawn
29    /// until the core has it.
30    pub fn pet(self) -> whiskers_pet::Mood {
31        match self {
32            Mood::Idle => whiskers_pet::Mood::Idle,
33            Mood::Listening => whiskers_pet::Mood::Listening,
34            Mood::Thinking => whiskers_pet::Mood::Thinking,
35            Mood::Speaking => whiskers_pet::Mood::Speaking,
36            Mood::Dizzy => whiskers_pet::Mood::Dizzy,
37            Mood::Sleeping => whiskers_pet::Mood::Sleeping,
38        }
39    }
40
41    pub fn parse(name: &str) -> Option<Mood> {
42        Mood::ALL.into_iter().find(|mood| mood.name() == name)
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn names_round_trip() {
52        for mood in Mood::ALL {
53            assert_eq!(Mood::parse(mood.name()), Some(mood));
54        }
55        assert_eq!(Mood::parse("Idle"), None);
56        assert_eq!(Mood::parse(""), None);
57    }
58}