From 3efdb0d59897b9ef6f24bdfea5d2781912f33343 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 7 Nov 2021 09:22:02 +0200 Subject: chore: clean up code based on clippy warnings Sponsored-by: author --- src/err.rs | 1 - src/map.rs | 14 +++++++------- src/parser.rs | 43 ++++++++++++++++--------------------------- 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/err.rs b/src/err.rs index 6a224a1..b928b09 100644 --- a/src/err.rs +++ b/src/err.rs @@ -1,4 +1,3 @@ -use serde_yaml; use thiserror::Error; /// Errors that can be returned for roadmaps. diff --git a/src/map.rs b/src/map.rs index 671318e..874e537 100644 --- a/src/map.rs +++ b/src/map.rs @@ -12,7 +12,7 @@ pub type RoadmapResult = Result; /// /// This stores all the steps needed to reach the end goal. See the /// crate leve documentation for an example. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct Roadmap { steps: Vec, } @@ -21,8 +21,8 @@ impl Roadmap { /// Create a new, empty roadmap. /// /// You probably want the `from_yaml` function instead. - pub fn new() -> Roadmap { - Roadmap { steps: vec![] } + pub fn new() -> Self { + Self::default() } // Find steps that nothing depends on. @@ -45,7 +45,7 @@ impl Roadmap { /// Get a step, given its name. pub fn get_step(&self, name: &str) -> Option<&Step> { - self.steps.iter().filter(|step| step.name() == name).next() + self.steps.iter().find(|step| step.name() == name) } /// Add a step to the roadmap. @@ -122,7 +122,7 @@ impl Roadmap { /// Should status be goal? In other words, does any other step /// depend on this one? pub fn is_goal(&self, step: &Step) -> bool { - self.steps.iter().all(|other| !other.depends_on(&step)) + self.steps.iter().all(|other| !other.depends_on(step)) } // Validate that the parsed, constructed roadmap is valid. @@ -137,7 +137,7 @@ impl Roadmap { let names: Vec = goals.iter().map(|s| s.name().into()).collect(); return Err(RoadmapError::ManyGoals { count: n, - names: names, + names, }); } } @@ -169,7 +169,7 @@ impl Roadmap { format!( "{} [label=\"{}\" style=filled fillcolor=\"{}\" shape=\"{}\"];\n", step.name(), - fill(&step.label(), label_width).replace("\n", "\\n"), + fill(step.label(), label_width).replace("\n", "\\n"), Roadmap::get_status_color(step), Roadmap::get_status_shape(step), ) diff --git a/src/parser.rs b/src/parser.rs index 7602b94..d83f618 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,4 +1,3 @@ -use serde_yaml; use serde_yaml::Value; use std::collections::HashMap; @@ -26,13 +25,13 @@ pub fn from_yaml(yaml: &str) -> RoadmapResult { fn step_from_value(name: &str, value: &Value) -> RoadmapResult { match value { Value::Mapping(_) => { - let label = parse_label(&value); - let status = parse_status(&value)?; + let label = parse_label(value); + let status = parse_status(value)?; let mut step = Step::new(name, label); step.set_status(status); - for depname in parse_depends(&value)?.iter() { + for depname in parse_depends(value)?.iter() { step.add_dependency(depname); } @@ -70,7 +69,7 @@ fn parse_label(map: &Value) -> &str { } // Get status string from a Mapping element. Default to unknown status. -fn parse_status<'a>(map: &'a Value) -> RoadmapResult { +fn parse_status(map: &Value) -> RoadmapResult { let text = parse_string("status", map); match Status::from_text(text) { Some(status) => Ok(status), @@ -94,46 +93,36 @@ mod tests { #[test] fn yaml_is_empty() { - let r = from_yaml(""); - match r { - Ok(_) => panic!("expected a parse error"), - _ => (), + if from_yaml("").is_ok() { + panic!("expected a parse error"); } } #[test] fn yaml_is_list() { - let r = from_yaml("[]"); - match r { - Ok(_) => panic!("expected a parse error"), - _ => (), + if from_yaml("[]").is_ok() { + panic!("expected a parse error"); } } #[test] fn yaml_map_entries_not_maps() { - let r = from_yaml("foo: []"); - match r { - Ok(_) => panic!("expected a parse error"), - _ => (), + if from_yaml("foo: []").is_ok() { + panic!("expected a parse error"); } } #[test] fn yaml_unknown_dep() { - let r = from_yaml("foo: {depends: [bar]}"); - match r { - Ok(_) => panic!("expected a parse error"), - _ => (), + if from_yaml("foo: {depends: [bar]}").is_ok() { + panic!("expected a parse error"); } } #[test] fn yaml_unknown_status() { - let r = from_yaml(r#"foo: {status: "bar"}"#); - match r { - Ok(_) => panic!("expected a parse error"), - _ => (), + if from_yaml(r#"foo: {status: "bar"}"#).is_ok() { + panic!("expected a parse error"); } } @@ -159,8 +148,8 @@ second: let first = roadmap.get_step("first").unwrap(); assert_eq!(first.name(), "first"); assert_eq!(first.label(), "the first step"); - let deps: Vec<&str> = first.dependencies().collect(); - assert_eq!(deps.len(), 0); + let deps = first.dependencies().count(); + assert_eq!(deps, 0); let second = roadmap.get_step("second").unwrap(); assert_eq!(second.name(), "second"); -- cgit v1.2.1 From 86cc978aea3edb3c8b1b8674b9d24af0dc0db7cf Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 7 Nov 2021 09:29:25 +0200 Subject: test: add script ./check to run clippy and tests Sponsored-by: author --- check | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 check diff --git a/check b/check new file mode 100755 index 0000000..e5d1646 --- /dev/null +++ b/check @@ -0,0 +1,7 @@ +#!/bin/bash + +set -euo pipefail + +cargo clippy -q -- --deny=clippy::all +cargo build -q --all-targets +cargo test -q -- cgit v1.2.1