koios_sdk/
network.rs

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
use std::fmt;

/// Available Cardano networks for the Koios API
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Network {
    /// Mainnet - production network
    #[default]
    Mainnet,
    /// Preprod - pre-production test network
    Preprod,
    /// Preview - preview test network
    Preview,
    /// Guild - community test network
    Guild,
}

impl Network {
    /// Get the base URL for the network
    pub fn base_url(&self) -> &'static str {
        match self {
            Network::Mainnet => "https://api.koios.rest/api/v1",
            Network::Preprod => "https://preprod.koios.rest/api/v1",
            Network::Preview => "https://preview.koios.rest/api/v1",
            Network::Guild => "https://guild.koios.rest/api/v1",
        }
    }
}

impl fmt::Display for Network {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Network::Mainnet => write!(f, "mainnet"),
            Network::Preprod => write!(f, "preprod"),
            Network::Preview => write!(f, "preview"),
            Network::Guild => write!(f, "guild"),
        }
    }
}

impl std::str::FromStr for Network {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "mainnet" => Ok(Network::Mainnet),
            "preprod" => Ok(Network::Preprod),
            "preview" => Ok(Network::Preview),
            "guild" => Ok(Network::Guild),
            _ => Err(format!("Unknown network: {}", s)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::str::FromStr;

    #[test]
    fn test_network_default() {
        assert_eq!(Network::default(), Network::Mainnet);
    }

    #[test]
    fn test_network_base_urls() {
        assert_eq!(Network::Mainnet.base_url(), "https://api.koios.rest/api/v1");
        assert_eq!(
            Network::Preprod.base_url(),
            "https://preprod.koios.rest/api/v1"
        );
        assert_eq!(
            Network::Preview.base_url(),
            "https://preview.koios.rest/api/v1"
        );
        assert_eq!(Network::Guild.base_url(), "https://guild.koios.rest/api/v1");
    }

    #[test]
    fn test_network_display() {
        assert_eq!(Network::Mainnet.to_string(), "mainnet");
        assert_eq!(Network::Preprod.to_string(), "preprod");
        assert_eq!(Network::Preview.to_string(), "preview");
        assert_eq!(Network::Guild.to_string(), "guild");
    }

    #[test]
    fn test_network_from_str() {
        assert_eq!(Network::from_str("mainnet").unwrap(), Network::Mainnet);
        assert_eq!(Network::from_str("PREPROD").unwrap(), Network::Preprod);
        assert_eq!(Network::from_str("Preview").unwrap(), Network::Preview);
        assert_eq!(Network::from_str("guild").unwrap(), Network::Guild);
        assert!(Network::from_str("invalid").is_err());
    }
}