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
//! # Metal
//!
//! Defines metal asset types

/// Defines the metal asset.
#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Hash)]
pub enum Metal {
    Gold,
    Palladium,
    Platinum,
    Silver,
}

impl ToString for Metal {
    fn to_string(&self) -> String {
        match self {
            Self::Gold => "XAU",
            Self::Palladium => "XPD",
            Self::Platinum => "XPT",
            Self::Silver => "XAG",
        }
        .to_string()
    }
}

#[cfg(test)]
mod test {

    use super::*;

    use pretty_assertions::assert_eq;
    use std::io::Cursor;

    #[test]
    fn should_decode_asset() {
        let csv = r#"id,metal
0,Gold
1,Palladium
2,Platinum
3,Silver
"#;
        let buffer = Cursor::new(csv);
        let mut reader = csv::Reader::from_reader(buffer);
        let mut fakes: Vec<Metal> = Vec::new();
        for result in reader.deserialize::<Fake>() {
            fakes.push(result.expect("failed to decode").metal);
        }
        assert_eq!(
            fakes,
            vec![
                Metal::Gold,
                Metal::Palladium,
                Metal::Platinum,
                Metal::Silver
            ]
        );
    }

    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct Fake {
        id: u64,
        metal: Metal,
    }
}