Skip to content

Commit a73aafe

Browse files
committed
fix: clippy lints
1 parent 76424b4 commit a73aafe

7 files changed

+242
-246
lines changed

src/lib.rs

+23-29
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub struct VarSpec {
8080
/// Operator prefixes
8181
///
8282
/// rfc [section-1.2](rfc [section-2.4](https://www.rfc-editor.org/rfc/rfc6570#section-1.2))
83-
#[derive(PartialEq)]
83+
#[derive(PartialEq, Eq)]
8484
pub enum Operator {
8585
Null,
8686
Plus,
@@ -118,7 +118,7 @@ fn prefixed(s: &str, prefix: u16) -> String {
118118

119119
fn parse_varlist(varlist: &str) -> TemplateComponent {
120120
let mut varlist = varlist.to_string();
121-
let operator = match varlist.chars().nth(0) {
121+
let operator = match varlist.chars().next() {
122122
Some(ch) => ch,
123123
None => {
124124
return TemplateComponent::VarList(Operator::Null, Vec::new());
@@ -137,7 +137,7 @@ fn parse_varlist(varlist: &str) -> TemplateComponent {
137137
if operator != Operator::Null {
138138
varlist.remove(0);
139139
}
140-
let varspecs = varlist.split(",");
140+
let varspecs = varlist.split(',');
141141
let mut varspec_list = Vec::new();
142142
for varspec in varspecs {
143143
let mut varspec = varspec.to_string();
@@ -150,13 +150,10 @@ fn parse_varlist(varlist: &str) -> TemplateComponent {
150150
});
151151
continue;
152152
}
153-
if varspec.contains(":") {
154-
let parts: Vec<_> = varspec.splitn(2, ":").collect();
153+
if varspec.contains(':') {
154+
let parts: Vec<_> = varspec.splitn(2, ':').collect();
155155
let prefix = u16::from_str(parts[1]).ok();
156-
let prefix = match prefix {
157-
Some(p) => p,
158-
None => 9999u16,
159-
};
156+
let prefix = prefix.unwrap_or(9999u16);
160157
varspec_list.push(VarSpec {
161158
name: parts[0].to_string(),
162159
var_type: VarSpecType::Prefixed(prefix),
@@ -172,11 +169,11 @@ fn parse_varlist(varlist: &str) -> TemplateComponent {
172169
TemplateComponent::VarList(operator, varspec_list)
173170
}
174171

175-
fn encode_vec<E>(v: &Vec<String>, encoder: E) -> Vec<String>
172+
fn encode_vec<E>(v: &[String], encoder: E) -> Vec<String>
176173
where
177174
E: Fn(&str) -> String,
178175
{
179-
v.iter().map(|s| encoder(&s)).collect()
176+
v.iter().map(|s| encoder(s)).collect()
180177
}
181178

182179
impl UriTemplate {
@@ -200,7 +197,7 @@ impl UriTemplate {
200197
continue;
201198
}
202199
if !in_varlist && ch == '{' {
203-
if buf.len() > 0 {
200+
if !buf.is_empty() {
204201
components.push(TemplateComponent::Literal(buf));
205202
buf = String::new();
206203
}
@@ -210,12 +207,12 @@ impl UriTemplate {
210207
buf.push(ch);
211208
}
212209

213-
if buf.len() > 0 {
210+
if !buf.is_empty() {
214211
components.push(TemplateComponent::Literal(buf));
215212
}
216213

217214
UriTemplate {
218-
components: components,
215+
components,
219216
vars: HashMap::new(),
220217
}
221218
}
@@ -261,11 +258,9 @@ impl UriTemplate {
261258
/// assert_eq!(t.delete("house"), false);
262259
/// assert_eq!(t.delete("animal"), true);
263260
/// ```
261+
#[inline]
264262
pub fn delete(&mut self, varname: &str) -> bool {
265-
match self.vars.remove(varname) {
266-
Some(_) => true,
267-
None => false,
268-
}
263+
self.vars.remove(varname).is_some()
269264
}
270265

271266
/// Deletes the values of all variables currently set in the `URITemplate`.
@@ -295,7 +290,7 @@ impl UriTemplate {
295290
TemplateVar::Scalar(ref s) => {
296291
if named {
297292
res.push_str(&encode_reserved(&v.name));
298-
if s == "" {
293+
if s.is_empty() {
299294
res.push_str(ifemp);
300295
return Some(res);
301296
}
@@ -311,14 +306,14 @@ impl UriTemplate {
311306
};
312307
}
313308
TemplateVar::List(ref l) => {
314-
if l.len() == 0 {
309+
if l.is_empty() {
315310
return None;
316311
}
317312
match v.var_type {
318313
VarSpecType::Raw | VarSpecType::Prefixed(_) => {
319314
if named {
320315
res.push_str(&encode_reserved(&v.name));
321-
if l.join("").len() == 0 {
316+
if l.join("").is_empty() {
322317
res.push_str(ifemp);
323318
return Some(res);
324319
}
@@ -331,7 +326,7 @@ impl UriTemplate {
331326
let pairs: Vec<String> = l
332327
.iter()
333328
.map(|x| {
334-
let val: String = if x == "" {
329+
let val: String = if x.is_empty() {
335330
format!("{}{}", &encode_reserved(&v.name), ifemp)
336331
} else {
337332
format!("{}={}", &encode_reserved(&v.name), &encoder(x))
@@ -341,13 +336,13 @@ impl UriTemplate {
341336
.collect();
342337
res.push_str(&pairs.join(sep));
343338
} else {
344-
res.push_str(&encode_vec(&l, encoder).join(sep));
339+
res.push_str(&encode_vec(l, encoder).join(sep));
345340
}
346341
}
347342
}
348343
}
349344
TemplateVar::AssociativeArray(ref a) => {
350-
if a.len() == 0 {
345+
if a.is_empty() {
351346
return None;
352347
}
353348
match v.var_type {
@@ -369,7 +364,7 @@ impl UriTemplate {
369364
let pairs: Vec<String> = a
370365
.iter()
371366
.map(|&(ref k, ref v)| {
372-
let val: String = if v == "" {
367+
let val: String = if v.is_empty() {
373368
format!("{}{}", &encode_reserved(k), ifemp)
374369
} else {
375370
format!("{}={}", &encode_reserved(k), &encoder(v))
@@ -414,14 +409,13 @@ impl UriTemplate {
414409
} else {
415410
self.build_varspec(varspec, sep, named, ifemp, encode_unreserved)
416411
};
417-
match built {
418-
Some(s) => values.push(s),
419-
None => {}
412+
if let Some(built) = built {
413+
values.push(built);
420414
}
421415
}
422416

423417
let mut res = String::new();
424-
if values.len() != 0 {
418+
if !values.is_empty() {
425419
res.push_str(first);
426420
res.push_str(&values.join(sep));
427421
}

src/percent_encoding.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use regex::Regex;
55

6-
static UNRESERVED: [&'static str; 256] = [
6+
static UNRESERVED: [&str; 256] = [
77
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", "%08", "%09", "%0A", "%0B", "%0C",
88
"%0D", "%0E", "%0F", "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", "%18", "%19",
99
"%1A", "%1B", "%1C", "%1D", "%1E", "%1F", "%20", "%21", "%22", "%23", "%24", "%25", "%26",
@@ -24,7 +24,7 @@ static UNRESERVED: [&'static str; 256] = [
2424
"%F3", "%F4", "%F5", "%F6", "%F7", "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF",
2525
];
2626

27-
static RESERVED: [&'static str; 256] = [
27+
static RESERVED: [&str; 256] = [
2828
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", "%08", "%09", "%0A", "%0B", "%0C",
2929
"%0D", "%0E", "%0F", "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", "%18", "%19",
3030
"%1A", "%1B", "%1C", "%1D", "%1E", "%1F", "%20", "!", "%22", "#", "$", "%25", "&", "'", "(",

src/templatevar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub trait IntoTemplateVar {
6868

6969
impl IntoTemplateVar for TemplateVar {
7070
fn into_template_var(self) -> TemplateVar {
71-
self.clone()
71+
self
7272
}
7373
}
7474

tests/extended_tests.rs

+39-39
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@ fn test_additional_examples_1() {
2222
UriTemplate::new("{?assoc_special_chars*}"),
2323
];
2424

25-
for i in 0..templates.len() {
26-
templates[i].set("long", "37.76");
27-
templates[i].set("lat", "-122.427");
28-
templates[i].set("last.name", "Doe");
29-
templates[i].set("token", "12345");
30-
templates[i].set("uri", "http://example.org/?uri=http%3A%2F%2Fexample.org%2F");
31-
templates[i].set("Stra%C3%9Fe", "Grüner Weg");
32-
templates[i].set("Some%20Thing", "foo");
33-
templates[i].set("first_name", "John");
34-
templates[i].set("random", "šö䟜ñꀣ¥‡ÑÒÓÔÕÖרÙÚàáâãäåæçÿ");
35-
templates[i].set("geocode", &["37.76", "-122.427"] as &[&str]);
36-
templates[i].set(
25+
for template in &mut templates {
26+
template.set("long", "37.76");
27+
template.set("lat", "-122.427");
28+
template.set("last.name", "Doe");
29+
template.set("token", "12345");
30+
template.set("uri", "http://example.org/?uri=http%3A%2F%2Fexample.org%2F");
31+
template.set("Stra%C3%9Fe", "Grüner Weg");
32+
template.set("Some%20Thing", "foo");
33+
template.set("first_name", "John");
34+
template.set("random", "šö䟜ñꀣ¥‡ÑÒÓÔÕÖרÙÚàáâãäåæçÿ");
35+
template.set("geocode", &["37.76", "-122.427"] as &[&str]);
36+
template.set(
3737
"assoc_special_chars",
3838
&[("šö䟜ñꀣ¥‡ÑÒÓÔÕ", "ÖרÙÚàáâãäåæçÿ")] as &[(&str, &str)],
3939
);
40-
templates[i].set("id", "person");
41-
templates[i].set("number", "6");
42-
templates[i].set("q", "URI Templates");
43-
templates[i].set("query", "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?book ?who WHERE { ?book dc:creator ?who }");
44-
templates[i].set("word", "drücken");
45-
templates[i].set("format", "json");
46-
templates[i].set("group_id", "12345");
47-
templates[i].set("fields", &["id", "name", "picture"] as &[&str]);
48-
templates[i].set("page", "5");
49-
templates[i].set("lang", "en");
40+
template.set("id", "person");
41+
template.set("number", "6");
42+
template.set("q", "URI Templates");
43+
template.set("query", "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?book ?who WHERE { ?book dc:creator ?who }");
44+
template.set("word", "drücken");
45+
template.set("format", "json");
46+
template.set("group_id", "12345");
47+
template.set("fields", &["id", "name", "picture"] as &[&str]);
48+
template.set("page", "5");
49+
template.set("lang", "en");
5050
}
5151

5252
assert_eq!(templates[0].build(), "/person");
@@ -93,16 +93,16 @@ fn test_additional_examples_2() {
9393
UriTemplate::new("{/id*}{?fields,token}"),
9494
];
9595

96-
for i in 0..templates.len() {
97-
templates[i].set("q", "URI Templates");
98-
templates[i].set("lang", "en");
99-
templates[i].set("format", "atom");
100-
templates[i].set("token", "12345");
101-
templates[i].set("geocode", &["37.76", "-122.427"] as &[&str]);
102-
templates[i].set("fields", &["id", "name", "picture"] as &[&str]);
103-
templates[i].set("page", "10");
104-
templates[i].set("start", "5");
105-
templates[i].set("id", &["person", "albums"] as &[&str]);
96+
for template in &mut templates {
97+
template.set("q", "URI Templates");
98+
template.set("lang", "en");
99+
template.set("format", "atom");
100+
template.set("token", "12345");
101+
template.set("geocode", &["37.76", "-122.427"] as &[&str]);
102+
template.set("fields", &["id", "name", "picture"] as &[&str]);
103+
template.set("page", "10");
104+
template.set("start", "5");
105+
template.set("id", &["person", "albums"] as &[&str]);
106106
}
107107

108108
let template_0_answers = vec!["/person/albums", "/albums/person"];
@@ -136,9 +136,9 @@ fn test_additional_examples_3() {
136136
UriTemplate::new("{?empty_assoc*}"),
137137
];
138138

139-
for i in 0..templates.len() {
140-
templates[i].set("empty_assoc", &[] as &[(&str, &str)]);
141-
templates[i].set("empty_list", &[] as &[&str]);
139+
for template in &mut templates {
140+
template.set("empty_assoc", &[] as &[(&str, &str)]);
141+
template.set("empty_list", &[] as &[&str]);
142142
}
143143

144144
let template_0_answers = vec![""];
@@ -166,16 +166,16 @@ fn test_additional_examples_4() {
166166
UriTemplate::new("{?german*}"),
167167
];
168168

169-
for i in 0..templates.len() {
170-
templates[i].set(
169+
for template in &mut templates {
170+
template.set(
171171
"german",
172172
&[("12", "zwölf"), ("11", "elf")] as &[(&str, &str)],
173173
);
174-
templates[i].set(
174+
template.set(
175175
"42",
176176
"The Answer to the Ultimate Question of Life, the Universe, and Everything",
177177
);
178-
templates[i].set("1337", &["leet", "as", "it", "can", "be"] as &[&str]);
178+
template.set("1337", &["leet", "as", "it", "can", "be"] as &[&str]);
179179
}
180180

181181
assert_eq!(templates[0].build(), "The%20Answer%20to%20the%20Ultimate%20Question%20of%20Life%2C%20the%20Universe%2C%20and%20Everything");

tests/spec_examples.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use datta::UriTemplate;
88
fn test_level_1() {
99
let mut templates = [UriTemplate::new("{var}"), UriTemplate::new("{hello}")];
1010

11-
for i in 0..templates.len() {
12-
templates[i].set("hello", "Hello World!");
13-
templates[i].set("var", "value");
11+
for template in &mut templates {
12+
template.set("hello", "Hello World!");
13+
template.set("var", "value");
1414
}
1515

1616
assert_eq!(templates[0].build(), "value");
@@ -27,10 +27,10 @@ fn test_level_2() {
2727
UriTemplate::new("here?ref={+path}"),
2828
];
2929

30-
for i in 0..templates.len() {
31-
templates[i].set("path", "/foo/bar");
32-
templates[i].set("hello", "Hello World!");
33-
templates[i].set("var", "value");
30+
for template in &mut templates {
31+
template.set("path", "/foo/bar");
32+
template.set("hello", "Hello World!");
33+
template.set("var", "value");
3434
}
3535

3636
assert_eq!(templates[0].build(), "value");
@@ -61,13 +61,13 @@ fn test_level_3() {
6161
UriTemplate::new("{&x,y,empty}"),
6262
];
6363

64-
for i in 0..templates.len() {
65-
templates[i].set("empty", "");
66-
templates[i].set("var", "value");
67-
templates[i].set("y", "768");
68-
templates[i].set("path", "/foo/bar");
69-
templates[i].set("hello", "Hello World!");
70-
templates[i].set("x", "1024");
64+
for template in &mut templates {
65+
template.set("empty", "");
66+
template.set("var", "value");
67+
template.set("y", "768");
68+
template.set("path", "/foo/bar");
69+
template.set("hello", "Hello World!");
70+
template.set("x", "1024");
7171
}
7272

7373
assert_eq!(templates[0].build(), "map?1024,768");
@@ -135,12 +135,12 @@ fn test_level_4() {
135135
UriTemplate::new("{&keys*}"),
136136
];
137137

138-
for i in 0..templates.len() {
139-
templates[i].set("path", "/foo/bar");
140-
templates[i].set("hello", "Hello World!");
141-
templates[i].set("var", "value");
142-
templates[i].set("list", &["red", "green", "blue"] as &[&str]);
143-
templates[i].set(
138+
for template in &mut templates {
139+
template.set("path", "/foo/bar");
140+
template.set("hello", "Hello World!");
141+
template.set("var", "value");
142+
template.set("list", &["red", "green", "blue"] as &[&str]);
143+
template.set(
144144
"keys",
145145
&[("dot", "."), ("semi", ";"), ("comma", ",")] as &[(&str, &str)],
146146
);

0 commit comments

Comments
 (0)