From dcbcbd9457d4bd4d21bcf8dbcc9e416fff463d92 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 20 Mar 2020 08:46:16 +0200 Subject: Change: make error message for wrong number of goals more helpful --- legend.yaml | 5 +++++ src/map.rs | 17 ++++++++++++++--- src/step.rs | 7 +++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/legend.yaml b/legend.yaml index fc17ba8..951d369 100644 --- a/legend.yaml +++ b/legend.yaml @@ -1,3 +1,8 @@ +goal2: {} +goal3: {} +goal4: {} +goal5: {} + goal: label: | This is the end goal: diff --git a/src/map.rs b/src/map.rs index 77b2feb..c1294bf 100644 --- a/src/map.rs +++ b/src/map.rs @@ -27,7 +27,10 @@ impl Roadmap { // Find steps that nothing depends on. fn goals(&self) -> Vec<&Step> { - self.steps.iter().filter(|step| self.is_goal(step)).collect() + self.steps + .iter() + .filter(|step| self.is_goal(step)) + .collect() } /// Count number of steps that nothing depends on. @@ -125,10 +128,18 @@ impl Roadmap { // Validate that the parsed, constructed roadmap is valid. pub fn validate(&self) -> RoadmapResult<()> { // Is there exactly one goal? - match self.count_goals() { + let goals = self.goals(); + let n = goals.len(); + match n { 0 => return Err(format!("the roadmap doesn't have a goal").into()), 1 => (), - _ => return Err(format!("must have exactly one goal for roadmap").into()), + _ => { + let mut names: Vec<&str> = goals.iter().map(|s| s.name()).collect(); + names.sort(); + let names = names.join(", "); + let msg = format!("only one goal allowed, found {}: {}", n, names); + return Err(msg.into()); + } } // Does every dependency exist? diff --git a/src/step.rs b/src/step.rs index 1ed495b..92b8eec 100644 --- a/src/step.rs +++ b/src/step.rs @@ -1,4 +1,5 @@ use super::Status; +use std::fmt; /// A roadmap step. /// @@ -62,6 +63,12 @@ impl Step { } } +impl fmt::Display for Step { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.name) + } +} + #[cfg(test)] mod tests { use super::{Status, Step}; -- cgit v1.2.1