1use serde::de::{Error as DeError, Unexpected, Visitor};
9use serde::{Deserialize, Deserializer, Serialize, Serializer};
10use std::fmt;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
17#[non_exhaustive]
18pub enum RequestMode {
19 Http,
22 #[default]
26 Smart,
27 Browser,
30}
31
32impl RequestMode {
33 pub const fn as_str(&self) -> &'static str {
35 match self {
36 RequestMode::Http => "http",
37 RequestMode::Smart => "smart",
38 RequestMode::Browser => "browser",
39 }
40 }
41
42 pub fn from_wire(value: &str) -> Option<RequestMode> {
45 match value.trim().to_ascii_lowercase().as_str() {
46 "http" => Some(RequestMode::Http),
47 "smart" | "smart_mode" | "smartmode" => Some(RequestMode::Smart),
48 "browser" | "chrome" | "headless" => Some(RequestMode::Browser),
49 _ => None,
50 }
51 }
52}
53
54impl fmt::Display for RequestMode {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 f.write_str(self.as_str())
57 }
58}
59
60impl Serialize for RequestMode {
61 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
62 serializer.serialize_str(self.as_str())
63 }
64}
65
66impl<'de> Deserialize<'de> for RequestMode {
67 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<RequestMode, D::Error> {
68 struct ModeVisitor;
69
70 impl<'de> Visitor<'de> for ModeVisitor {
71 type Value = RequestMode;
72
73 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 f.write_str("one of \"http\", \"smart\" or \"browser\"")
75 }
76
77 fn visit_str<E: DeError>(self, value: &str) -> Result<RequestMode, E> {
78 RequestMode::from_wire(value)
79 .ok_or_else(|| E::invalid_value(Unexpected::Str(value), &self))
80 }
81 }
82
83 deserializer.deserialize_str(ModeVisitor)
84 }
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
93#[non_exhaustive]
94pub enum ProxyPool {
95 #[serde(rename = "isp", alias = "datacenter", alias = "ISP")]
98 #[default]
99 Isp,
100 #[serde(rename = "residential", alias = "RESIDENTIAL")]
103 Residential,
104}
105
106impl ProxyPool {
107 pub const fn as_str(&self) -> &'static str {
109 match self {
110 ProxyPool::Isp => "isp",
111 ProxyPool::Residential => "residential",
112 }
113 }
114}
115
116impl fmt::Display for ProxyPool {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 f.write_str(self.as_str())
119 }
120}
121
122pub const PROXY_POOLS: [ProxyPool; 2] = [ProxyPool::Isp, ProxyPool::Residential];
124
125#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
131pub struct Country(String);
132
133impl Country {
134 pub fn new(code: &str) -> Option<Country> {
138 let code = code.trim();
139
140 if code.len() == 2 && code.bytes().all(|b| b.is_ascii_alphabetic()) {
141 Some(Country(code.to_ascii_lowercase()))
142 } else {
143 None
144 }
145 }
146
147 pub fn as_str(&self) -> &str {
149 &self.0
150 }
151}
152
153impl fmt::Display for Country {
154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 f.write_str(&self.0)
156 }
157}
158
159impl std::str::FromStr for Country {
160 type Err = InvalidCountry;
161
162 fn from_str(value: &str) -> Result<Country, InvalidCountry> {
163 Country::new(value).ok_or(InvalidCountry)
164 }
165}
166
167impl TryFrom<&str> for Country {
168 type Error = InvalidCountry;
169
170 fn try_from(value: &str) -> Result<Country, InvalidCountry> {
171 Country::new(value).ok_or(InvalidCountry)
172 }
173}
174
175#[derive(Debug, Clone, Copy, PartialEq, Eq)]
177pub struct InvalidCountry;
178
179impl fmt::Display for InvalidCountry {
180 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181 f.write_str("a country code is two ASCII letters, such as us or de")
182 }
183}
184
185impl std::error::Error for InvalidCountry {}
186
187impl Serialize for Country {
188 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
189 serializer.serialize_str(&self.0)
190 }
191}
192
193impl<'de> Deserialize<'de> for Country {
194 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Country, D::Error> {
195 struct CountryVisitor;
196
197 impl<'de> Visitor<'de> for CountryVisitor {
198 type Value = Country;
199
200 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 f.write_str("a two letter ISO 3166-1 country code")
202 }
203
204 fn visit_str<E: DeError>(self, value: &str) -> Result<Country, E> {
205 Country::new(value).ok_or_else(|| E::invalid_value(Unexpected::Str(value), &self))
206 }
207 }
208
209 deserializer.deserialize_str(CountryVisitor)
210 }
211}