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
use std::fmt::{Debug, Display};

use smol_str::SmolStr;

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Identifier {
    /// Market Identification Code
    mic: SmolStr,

    /// Ticker Symbol
    symbol: SmolStr,
}

impl Display for Identifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{}:{}", self.mic, self.symbol))
    }
}

impl Identifier {
    pub fn new<S>(mic: S, symbol: S) -> Self
    where
        S: Into<SmolStr>,
    {
        Self {
            mic: mic.into(),
            symbol: symbol.into(),
        }
    }

    #[must_use]
    pub fn mic(&self) -> &SmolStr {
        &self.mic
    }

    #[must_use]
    pub fn symbol(&self) -> &SmolStr {
        &self.symbol
    }
}

#[cfg(test)]
mod test {
    use std::collections::{BTreeSet, HashSet};

    use super::Identifier;

    #[test]
    fn test_display() {
        let id = Identifier::new("XASX", "ANZ");
        assert_eq!(id.to_string(), "XASX:ANZ");
    }

    #[test]
    fn test_hash() {
        let mut set = HashSet::<Identifier>::new();
        set.insert(Identifier::new("XASX", "ANZ"));
    }

    #[test]
    fn test_btree() {
        let mut set = BTreeSet::<Identifier>::new();
        set.insert(Identifier::new("XASX", "ANZ"));
    }
}