@@ -36,29 +36,38 @@ impl ScalarType for NoProxyItem {
36
36
}
37
37
}
38
38
39
- impl ToString for NoProxyItem {
40
- fn to_string ( & self ) -> String {
39
+ impl NoProxyItem {
40
+ fn as_str ( & self ) -> & str {
41
41
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
+ }
46
46
}
47
47
}
48
48
}
49
49
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 == "*" {
53
60
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)
60
63
} 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
+ }
62
71
}
63
72
}
64
73
}
@@ -83,7 +92,7 @@ impl NoProxyItem {
83
92
if value == source {
84
93
true
85
94
} else if let Ok ( ip_value) = IpAddr :: from_str ( value) {
86
- ip_cidr. contains ( ip_value)
95
+ ip_cidr. contains ( & ip_value)
87
96
} else {
88
97
false
89
98
}
@@ -114,9 +123,14 @@ pub struct NoProxy {
114
123
impl NoProxy {
115
124
fn from_iterator < V : AsRef < str > , I : Iterator < Item = V > > ( iterator : I ) -> Self {
116
125
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
+ } )
120
134
. collect ( ) ;
121
135
let has_wildcard = content. contains ( & NoProxyItem :: Wildcard ) ;
122
136
Self {
@@ -126,21 +140,9 @@ impl NoProxy {
126
140
}
127
141
}
128
142
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 ( ',' ) )
144
146
}
145
147
}
146
148
@@ -169,22 +171,19 @@ impl NoProxy {
169
171
if self . has_wildcard {
170
172
return true ;
171
173
}
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) )
178
175
}
179
176
}
180
177
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 ( ( ) )
188
187
}
189
188
}
190
189
@@ -227,8 +226,8 @@ mod tests {
227
226
228
227
#[ test]
229
228
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" ) ;
232
231
}
233
232
234
233
#[ test]
@@ -266,7 +265,7 @@ mod tests {
266
265
267
266
#[ test]
268
267
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" ;
270
269
shouldnt_match ( pattern, "hyper.rs" ) ;
271
270
shouldnt_match ( pattern, "foo.bar.baz" ) ;
272
271
shouldnt_match ( pattern, "10.43.1.1" ) ;
0 commit comments