Skip to content

Commit d4f83b7

Browse files
committed
clippy
1 parent 7fbcc32 commit d4f83b7

3 files changed

Lines changed: 47 additions & 48 deletions

File tree

src/cli/file_input.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,26 @@ pub fn load_json_input(path: &Path, template: &Template) -> Result<HashMap<Strin
5858
}
5959

6060
// Choices validation
61-
if let Some(ref choices) = var.choices {
62-
if !choices.contains(&value) {
63-
let choices_str: Vec<_> = choices.iter().map(|c| format!("{}", c)).collect();
64-
bail!(
65-
"Variable '{}' value '{}' is not in allowed choice: [{}]",
66-
key,
67-
value,
68-
choices_str.join(", ")
69-
);
70-
}
61+
if let Some(ref choices) = var.choices
62+
&& !choices.contains(&value)
63+
{
64+
let choices_str: Vec<_> = choices.iter().map(|c| format!("{}", c)).collect();
65+
bail!(
66+
"Variable '{}' value '{}' is not in allowed choice: [{}]",
67+
key,
68+
value,
69+
choices_str.join(", ")
70+
);
7171
}
7272

7373
// Regex validation
74-
if let Some(ref pattern) = var.validation {
75-
if let Value::String(s) = &value {
76-
let re = Regex::new(pattern)
77-
.with_context(|| format!("Invalid validation regex for '{}'", key))?;
78-
if !re.is_match(s) {
79-
bail!("Variable '{}' value '{}' needs to pass the regex: {}", key, s, pattern);
80-
}
74+
if let Some(ref pattern) = var.validation
75+
&& let Value::String(s) = &value
76+
{
77+
let re = Regex::new(pattern)
78+
.with_context(|| format!("Invalid validation regex for '{}'", key))?;
79+
if !re.is_match(s) {
80+
bail!("Variable '{}' value '{}' needs to pass the regex: {}", key, s, pattern);
8181
}
8282
}
8383

src/definition.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ impl TemplateDefinition {
141141
));
142142
}
143143

144-
if let Some(ref prompt) = var.prompt {
145-
if prompt.trim().is_empty() {
146-
errs.push(format!(
147-
"Variable `{}` has an empty prompt, which is not allowed",
148-
var.name
149-
));
150-
}
144+
if let Some(ref prompt) = var.prompt
145+
&& prompt.trim().is_empty()
146+
{
147+
errs.push(format!(
148+
"Variable `{}` has an empty prompt, which is not allowed",
149+
var.name
150+
));
151151
}
152152

153153
let type_str = var.default.type_str();
@@ -276,14 +276,13 @@ impl TemplateDefinition {
276276

277277
#[cfg(test)]
278278
mod tests {
279-
use toml;
280279

281280
use super::*;
282281

283282
#[test]
284283
fn can_validate_definition() {
285284
insta::glob!("snapshots/validation/*.toml", |path| {
286-
match TemplateDefinition::validate_file(&path) {
285+
match TemplateDefinition::validate_file(path) {
287286
Ok(errs) => insta::assert_debug_snapshot!(&errs),
288287
Err(e) => insta::assert_snapshot!(&e),
289288
}

src/generation.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Template {
7474
pub fn from_git(remote: &str, directory: Option<&str>) -> Result<Template> {
7575
// Clone the remote in git first in /tmp
7676
let mut tmp = env::temp_dir();
77-
tmp.push(remote.split('/').last().unwrap_or("kickstart"));
77+
tmp.push(remote.split('/').next_back().unwrap_or("kickstart"));
7878
if tmp.exists() {
7979
fs::remove_dir_all(&tmp)?;
8080
}
@@ -318,7 +318,7 @@ impl Template {
318318

319319
// For patterns, we do not want the output directory to be included
320320
let glob_real_path = real_path.strip_prefix(&output_dir).expect("valid path");
321-
let no_render = patterns.iter().map(|p| p.matches_path(glob_real_path)).any(|x| x);
321+
let no_render = patterns.iter().any(|p| p.matches_path(glob_real_path));
322322

323323
if no_render || is_binary(&buffer) {
324324
map_io_err(fs::copy(entry.path(), &real_path), entry.path())?;
@@ -335,20 +335,20 @@ impl Template {
335335
}
336336

337337
for cleanup in &self.definition.cleanup {
338-
if let Some(val) = self.variables.get(&cleanup.name) {
339-
if *val == cleanup.value {
340-
for p in &cleanup.paths {
341-
let actual_path = render_one_off_template(p, &context, None)?;
342-
let path_to_delete = output_dir.join(actual_path).canonicalize()?;
343-
// Avoid path traversals
344-
if !path_to_delete.starts_with(&output_dir) || !path_to_delete.exists() {
345-
continue;
346-
}
347-
if path_to_delete.is_dir() {
348-
map_io_err(fs::remove_dir_all(&path_to_delete), &path_to_delete)?;
349-
} else {
350-
map_io_err(fs::remove_file(&path_to_delete), &path_to_delete)?;
351-
}
338+
if let Some(val) = self.variables.get(&cleanup.name)
339+
&& *val == cleanup.value
340+
{
341+
for p in &cleanup.paths {
342+
let actual_path = render_one_off_template(p, &context, None)?;
343+
let path_to_delete = output_dir.join(actual_path).canonicalize()?;
344+
// Avoid path traversals
345+
if !path_to_delete.starts_with(&output_dir) || !path_to_delete.exists() {
346+
continue;
347+
}
348+
if path_to_delete.is_dir() {
349+
map_io_err(fs::remove_dir_all(&path_to_delete), &path_to_delete)?;
350+
} else {
351+
map_io_err(fs::remove_file(&path_to_delete), &path_to_delete)?;
352352
}
353353
}
354354
}
@@ -369,7 +369,7 @@ mod tests {
369369
let dir = tempdir().unwrap();
370370
let mut tpl = Template::from_input("examples/complex", None).unwrap();
371371
tpl.set_variables(tpl.definition.default_values().unwrap()).unwrap();
372-
let res = tpl.generate(&dir.path().to_path_buf());
372+
let res = tpl.generate(dir.path());
373373

374374
assert!(res.is_ok());
375375
assert!(!dir.path().join("some-project").join("template.toml").exists());
@@ -381,7 +381,7 @@ mod tests {
381381
let dir = tempdir().unwrap();
382382
let mut tpl = Template::from_input("examples/with-directory", None).unwrap();
383383
tpl.set_variables(tpl.definition.default_values().unwrap()).unwrap();
384-
let res = tpl.generate(&dir.path().to_path_buf());
384+
let res = tpl.generate(dir.path());
385385
assert!(res.is_ok());
386386
assert!(dir.path().join("template_root").join("Howdy.py").exists());
387387
}
@@ -391,7 +391,7 @@ mod tests {
391391
let dir = tempdir().unwrap();
392392
let mut tpl = Template::from_input("./", Some("examples/complex")).unwrap();
393393
tpl.set_variables(tpl.definition.default_values().unwrap()).unwrap();
394-
let res = tpl.generate(&dir.path().to_path_buf());
394+
let res = tpl.generate(dir.path());
395395
assert!(res.is_ok());
396396
assert!(!dir.path().join("some-project").join("template.toml").exists());
397397
assert!(dir.path().join("some-project").join("logo.png").exists());
@@ -403,7 +403,7 @@ mod tests {
403403
let mut tpl =
404404
Template::from_input("https://github.com/Keats/rust-cli-template", None).unwrap();
405405
tpl.set_variables(tpl.definition.default_values().unwrap()).unwrap();
406-
let res = tpl.generate(&dir.path().to_path_buf());
406+
let res = tpl.generate(dir.path());
407407

408408
assert!(res.is_ok());
409409
assert!(!dir.path().join("My-CLI").join("template.toml").exists());
@@ -417,7 +417,7 @@ mod tests {
417417
Template::from_input("https://github.com/Keats/kickstart", Some("examples/complex"))
418418
.unwrap();
419419
tpl.set_variables(tpl.definition.default_values().unwrap()).unwrap();
420-
let res = tpl.generate(&dir.path().to_path_buf());
420+
let res = tpl.generate(dir.path());
421421

422422
assert!(res.is_ok());
423423
assert!(!dir.path().join("some-project").join("template.toml").exists());
@@ -429,7 +429,7 @@ mod tests {
429429
let dir = tempdir().unwrap();
430430
let mut tpl = Template::from_input("examples/slugify", None).unwrap();
431431
tpl.set_variables(tpl.definition.default_values().unwrap()).unwrap();
432-
let res = tpl.generate(&dir.path().to_path_buf());
432+
let res = tpl.generate(dir.path());
433433
assert!(res.is_ok());
434434
assert!(!dir.path().join("template.toml").exists());
435435
assert!(dir.path().join("hello.md").exists());

0 commit comments

Comments
 (0)