ip2location_ip2proxy/
proxy_type.rs

1//! [Ref](https://lite.ip2location.com/database/px2-ip-proxytype-country#proxy-type)
2
3//
4#[derive(Debug, Clone, PartialEq, Eq)]
5#[cfg_attr(
6    feature = "serde",
7    derive(
8        serde_enum_str::Deserialize_enum_str,
9        serde_enum_str::Serialize_enum_str
10    )
11)]
12#[cfg_attr(feature = "serde", serde(rename_all = "UPPERCASE"))]
13pub enum ProxyType {
14    VPN,
15    TOR,
16    DCH,
17    PUB,
18    WEB,
19    SES,
20    RES,
21    #[cfg_attr(feature = "serde", serde(other))]
22    Other(Box<str>),
23}
24
25#[cfg(not(feature = "serde"))]
26impl core::str::FromStr for ProxyType {
27    type Err = core::convert::Infallible;
28
29    fn from_str(s: &str) -> Result<Self, Self::Err> {
30        match s {
31            "VPN" => Ok(Self::VPN),
32            "TOR" => Ok(Self::TOR),
33            "DCH" => Ok(Self::DCH),
34            "PUB" => Ok(Self::PUB),
35            "WEB" => Ok(Self::WEB),
36            "SES" => Ok(Self::SES),
37            "RES" => Ok(Self::RES),
38            s => Ok(Self::Other(s.into())),
39        }
40    }
41}
42
43#[cfg(not(feature = "serde"))]
44impl core::fmt::Display for ProxyType {
45    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46        match self {
47            Self::VPN => write!(f, "VPN"),
48            Self::TOR => write!(f, "TOR"),
49            Self::DCH => write!(f, "DCH"),
50            Self::PUB => write!(f, "PUB"),
51            Self::WEB => write!(f, "WEB"),
52            Self::SES => write!(f, "SES"),
53            Self::RES => write!(f, "RES"),
54            Self::Other(s) => write!(f, "{}", s),
55        }
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn from_str() {
65        assert_eq!("PUB".parse::<ProxyType>().unwrap(), ProxyType::PUB);
66    }
67}