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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum Size {
    Tiny { height: u16, weight: u16 },
    Small { height: u16, weight: u16 },
    Medium { height: u16, weight: u16 },
    // Large { height: u16, weight: u16 },
    // Huge { height: u16, weight: u16 },
    // Gargantuan { height: u16, weight: u16 },
}

impl Size {
    pub fn height_weight(&self) -> (u16, u16) {
        match self {
            Self::Tiny { height, weight } => (*height, *weight),
            Self::Small { height, weight } => (*height, *weight),
            Self::Medium { height, weight } => (*height, *weight),
        }
    }

    pub fn height(&self) -> u16 {
        self.height_weight().0
    }

    pub fn height_ft_in(&self) -> (u8, u8) {
        let height = self.height();
        ((height / 12) as u8, (height % 12) as u8)
    }

    pub fn weight(&self) -> u16 {
        self.height_weight().1
    }

    pub fn name(&self) -> &'static str {
        match self {
            Self::Tiny { .. } => "tiny",
            Self::Small { .. } => "small",
            Self::Medium { .. } => "medium",
        }
    }
}

impl fmt::Display for Size {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let (height_ft, height_in) = self.height_ft_in();
        write!(
            f,
            "{}'{}\", {} lbs ({})",
            height_ft,
            height_in,
            self.weight(),
            self.name(),
        )
    }
}

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

    #[test]
    fn height_weight_test() {
        assert_eq!(
            (71, 140),
            Size::Small {
                height: 71,
                weight: 140
            }
            .height_weight()
        );

        assert_eq!(
            (71, 140),
            Size::Medium {
                height: 71,
                weight: 140
            }
            .height_weight()
        );
    }

    #[test]
    fn height_test() {
        assert_eq!(71, size().height());
    }

    #[test]
    fn height_ft_in_test() {
        assert_eq!((5, 11), size().height_ft_in());
    }

    #[test]
    fn weight_test() {
        assert_eq!(140, size().weight());
    }

    #[test]
    fn name_test() {
        assert_eq!(
            "small",
            Size::Small {
                height: 0,
                weight: 0
            }
            .name()
        );
        assert_eq!(
            "medium",
            Size::Medium {
                height: 0,
                weight: 0
            }
            .name()
        );
    }

    #[test]
    fn fmt_test() {
        assert_eq!("5'11\", 140 lbs (medium)", format!("{}", size()));
    }

    #[test]
    fn serialize_deserialize_test() {
        assert_eq!(
            r#"{"type":"Medium","height":71,"weight":140}"#,
            serde_json::to_string(&size()).unwrap(),
        );

        let value: Size =
            serde_json::from_str(r#"{"type":"Medium","height":71,"weight":140}"#).unwrap();
        assert_eq!(size(), value);
    }

    fn size() -> Size {
        Size::Medium {
            height: 71,
            weight: 140,
        }
    }
}