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

use smol_str::SmolStr;

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Pair {
    base: SmolStr,
    quote: SmolStr,
}

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

impl Pair {
    pub fn new<S>(base: S, quote: S) -> Self
    where
        S: Into<SmolStr>,
    {
        Self {
            base: base.into(),
            quote: quote.into(),
        }
    }

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

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

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

    use super::Pair;

    #[test]
    fn test_display() {
        let id = Pair::new("GBP", "USD");
        assert_eq!(id.to_string(), "GBP/USD");
    }

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

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