Skip to content

Commit 1ea0cb8

Browse files
authored
Merge pull request #6 from jdrouet/deps-async-graphql-and-cidr-utils
build(deps): bump async-graphql and cidr-utils
2 parents de1c100 + 519a93d commit 1ea0cb8

File tree

3 files changed

+54
-55
lines changed

3 files changed

+54
-55
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ serialize = ["serde"]
1616
graphql = ["async-graphql"]
1717

1818
[dependencies]
19-
async-graphql = { version = "^6.0", optional = true }
20-
cidr-utils = "^0.5"
19+
async-graphql = { version = "^7.0", default-features = false, optional = true }
20+
cidr-utils = "^0.6"
2121
serde = { version = "^1.0", features = ["derive"], optional = true }
2222

2323
[dev-dependencies]

src/lib.rs

+50-51
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,38 @@ impl ScalarType for NoProxyItem {
3636
}
3737
}
3838

39-
impl ToString for NoProxyItem {
40-
fn to_string(&self) -> String {
39+
impl NoProxyItem {
40+
fn as_str(&self) -> &str {
4141
match self {
42-
Self::Wildcard => "*".into(),
43-
Self::IpCidr(value, _) => value.clone(),
44-
Self::WithDot(value, _, _) => value.clone(),
45-
Self::Plain(value) => value.clone(),
42+
Self::Wildcard => "*",
43+
Self::IpCidr(value, _) | Self::WithDot(value, _, _) | Self::Plain(value) => {
44+
value.as_str()
45+
}
4646
}
4747
}
4848
}
4949

50-
impl From<String> for NoProxyItem {
51-
fn from(value: String) -> Self {
52-
if value == "*" {
50+
impl std::fmt::Display for NoProxyItem {
51+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52+
f.write_str(self.as_str())
53+
}
54+
}
55+
56+
impl<V: AsRef<str> + Into<String>> From<V> for NoProxyItem {
57+
fn from(value: V) -> Self {
58+
let value_str = value.as_ref();
59+
if value_str == "*" {
5360
Self::Wildcard
54-
} else if let Ok(ip_cidr) = IpCidr::from_str(&value) {
55-
Self::IpCidr(value, ip_cidr)
56-
} else if value.starts_with('.') || value.ends_with('.') {
57-
let start = value.starts_with('.');
58-
let end = value.ends_with('.');
59-
Self::WithDot(value, start, end)
61+
} else if let Ok(ip_cidr) = IpCidr::from_str(value_str) {
62+
Self::IpCidr(value.into(), ip_cidr)
6063
} else {
61-
Self::Plain(value)
64+
let start = value_str.starts_with('.');
65+
let end = value_str.ends_with('.');
66+
if start || end {
67+
Self::WithDot(value.into(), start, end)
68+
} else {
69+
Self::Plain(value.into())
70+
}
6271
}
6372
}
6473
}
@@ -83,7 +92,7 @@ impl NoProxyItem {
8392
if value == source {
8493
true
8594
} else if let Ok(ip_value) = IpAddr::from_str(value) {
86-
ip_cidr.contains(ip_value)
95+
ip_cidr.contains(&ip_value)
8796
} else {
8897
false
8998
}
@@ -114,9 +123,14 @@ pub struct NoProxy {
114123
impl NoProxy {
115124
fn from_iterator<V: AsRef<str>, I: Iterator<Item = V>>(iterator: I) -> Self {
116125
let content: HashSet<_> = iterator
117-
.map(|item| item.as_ref().trim().to_string())
118-
.filter(|item| !item.is_empty())
119-
.map(NoProxyItem::from)
126+
.filter_map(|item| {
127+
let short = item.as_ref().trim();
128+
if short.is_empty() {
129+
None
130+
} else {
131+
Some(NoProxyItem::from(short))
132+
}
133+
})
120134
.collect();
121135
let has_wildcard = content.contains(&NoProxyItem::Wildcard);
122136
Self {
@@ -126,21 +140,9 @@ impl NoProxy {
126140
}
127141
}
128142

129-
impl From<&str> for NoProxy {
130-
fn from(value: &str) -> Self {
131-
Self::from_iterator(value.split(','))
132-
}
133-
}
134-
135-
impl From<String> for NoProxy {
136-
fn from(value: String) -> Self {
137-
Self::from_iterator(value.split(','))
138-
}
139-
}
140-
141-
impl From<Vec<String>> for NoProxy {
142-
fn from(value: Vec<String>) -> Self {
143-
Self::from_iterator(value.iter())
143+
impl<V: AsRef<str>> From<V> for NoProxy {
144+
fn from(value: V) -> Self {
145+
Self::from_iterator(value.as_ref().split(','))
144146
}
145147
}
146148

@@ -169,22 +171,19 @@ impl NoProxy {
169171
if self.has_wildcard {
170172
return true;
171173
}
172-
for item in self.content.iter() {
173-
if item.matches(input) {
174-
return true;
175-
}
176-
}
177-
false
174+
self.content.iter().any(|item| item.matches(input))
178175
}
179176
}
180177

181-
impl ToString for NoProxy {
182-
fn to_string(&self) -> String {
183-
self.content
184-
.iter()
185-
.map(ToString::to_string)
186-
.collect::<Vec<_>>()
187-
.join(",")
178+
impl std::fmt::Display for NoProxy {
179+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180+
for (index, item) in self.content.iter().enumerate() {
181+
if index > 0 {
182+
write!(f, ",")?;
183+
}
184+
item.fmt(f)?;
185+
}
186+
Ok(())
188187
}
189188
}
190189

@@ -227,8 +226,8 @@ mod tests {
227226

228227
#[test]
229228
fn cidr() {
230-
should_match("21.19.35.40/24", "21.19.35.4");
231-
shouldnt_match("21.19.35.40/24", "127.0.0.1");
229+
should_match("21.19.35.0/24", "21.19.35.4");
230+
shouldnt_match("21.19.35.0/24", "127.0.0.1");
232231
}
233232

234233
#[test]
@@ -266,7 +265,7 @@ mod tests {
266265

267266
#[test]
268267
fn from_reqwest() {
269-
let pattern = ".foo.bar,bar.baz,10.42.1.1/24,::1,10.124.7.8,2001::/17";
268+
let pattern = ".foo.bar,bar.baz,10.42.1.0/24,::1,10.124.7.8,2001::/17";
270269
shouldnt_match(pattern, "hyper.rs");
271270
shouldnt_match(pattern, "foo.bar.baz");
272271
shouldnt_match(pattern, "10.43.1.1");

src/serialize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl Serialize for NoProxy {
99
{
1010
let mut seq = serializer.serialize_seq(Some(self.content.len()))?;
1111
for elt in self.content.iter() {
12-
seq.serialize_element(&elt.to_string())?;
12+
seq.serialize_element(elt.as_str())?;
1313
}
1414
seq.end()
1515
}
@@ -49,7 +49,7 @@ impl<'de> Deserialize<'de> for NoProxy {
4949
{
5050
deserializer
5151
.deserialize_any(NoProxyVisitor)
52-
.map(NoProxy::from)
52+
.map(|list| NoProxy::from_iterator(list.into_iter()))
5353
}
5454
}
5555

0 commit comments

Comments
 (0)