ip2location_ip2proxy/
usage_type.rs

1//! [Ref](https://lite.ip2location.com/database/px6-ip-proxytype-country-region-city-isp-domain-usagetype#database-fields)
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 UsageType {
14    COM,
15    ORG,
16    GOV,
17    MIL,
18    EDU,
19    LIB,
20    CDN,
21    ISP,
22    MOB,
23    DCH,
24    SES,
25    RSV,
26    #[cfg_attr(feature = "serde", serde(other))]
27    Other(Box<str>),
28}
29
30#[cfg(not(feature = "serde"))]
31impl core::str::FromStr for UsageType {
32    type Err = core::convert::Infallible;
33
34    fn from_str(s: &str) -> Result<Self, Self::Err> {
35        match s {
36            "COM" => Ok(Self::COM),
37            "ORG" => Ok(Self::ORG),
38            "GOV" => Ok(Self::GOV),
39            "MIL" => Ok(Self::MIL),
40            "EDU" => Ok(Self::EDU),
41            "LIB" => Ok(Self::LIB),
42            "CDN" => Ok(Self::CDN),
43            "ISP" => Ok(Self::ISP),
44            "MOB" => Ok(Self::MOB),
45            "DCH" => Ok(Self::DCH),
46            "SES" => Ok(Self::SES),
47            "RSV" => Ok(Self::RSV),
48            s => Ok(Self::Other(s.into())),
49        }
50    }
51}
52
53#[cfg(not(feature = "serde"))]
54impl core::fmt::Display for UsageType {
55    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
56        match self {
57            Self::COM => write!(f, "COM"),
58            Self::ORG => write!(f, "ORG"),
59            Self::GOV => write!(f, "GOV"),
60            Self::MIL => write!(f, "MIL"),
61            Self::EDU => write!(f, "EDU"),
62            Self::LIB => write!(f, "LIB"),
63            Self::CDN => write!(f, "CDN"),
64            Self::ISP => write!(f, "ISP"),
65            Self::MOB => write!(f, "MOB"),
66            Self::DCH => write!(f, "DCH"),
67            Self::SES => write!(f, "SES"),
68            Self::RSV => write!(f, "RSV"),
69            Self::Other(s) => write!(f, "{}", s),
70        }
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn from_str() {
80        assert_eq!("ISP".parse::<UsageType>().unwrap(), UsageType::ISP);
81    }
82}