fedimint_api_client/api/
net.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
use std::fmt;
use std::str::FromStr;

use fedimint_core::encoding::{Decodable, Encodable};
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Encodable, Decodable, Serialize, Deserialize)]
pub enum Connector {
    Tcp,
    #[cfg(feature = "tor")]
    Tor,
}

impl Connector {
    #[cfg(feature = "tor")]
    pub fn tor() -> Connector {
        Connector::Tor
    }
}

impl Default for Connector {
    fn default() -> Self {
        Self::Tcp
    }
}

impl fmt::Display for Connector {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

impl FromStr for Connector {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "tcp" => Ok(Connector::Tcp),
            #[cfg(feature = "tor")]
            "tor" => Ok(Connector::Tor),
            _ => Err("invalid connector!"),
        }
    }
}