From afa0029ee9306764200e00e535b8a6ce94e4954c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 5 Oct 2019 10:27:58 +0300 Subject: Change: use RoadmapResult (nee RoadmapError) everywhere Daniel told me about .into(). --- src/lib.rs | 2 +- src/map.rs | 13 +++++++------ src/parser.rs | 19 +++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d6745a8..d3f672b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ pub use step::Step; mod map; pub use map::Roadmap; -pub use map::RoadmapError; +pub use map::RoadmapResult; mod parser; pub use parser::from_yaml; diff --git a/src/map.rs b/src/map.rs index 1f693ac..2364941 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,3 +1,4 @@ +use std::error::Error; use textwrap::fill; pub use crate::from_yaml; @@ -5,7 +6,7 @@ pub use crate::Status; pub use crate::Step; /// Error in Roadmap, from parsing or otherwise. -pub type RoadmapError = Result; +pub type RoadmapResult = Result>; /// Represent a full project roadmap. /// @@ -117,12 +118,12 @@ impl Roadmap { } // Validate that the parsed, constructed roadmap is valid. - pub fn validate(&self) -> RoadmapError<()> { + pub fn validate(&self) -> RoadmapResult<()> { // Is there exactly one goal? match self.count_goals() { - 0 => return Err(format!("the roadmap doesn't have a goal")), + 0 => return Err(format!("the roadmap doesn't have a goal").into()), 1 => (), - _ => return Err(format!("must have exactly one goal for roadmap")), + _ => return Err(format!("must have exactly one goal for roadmap").into()), } // Does every dependency exist? @@ -134,7 +135,7 @@ impl Roadmap { "step {} depends on missing {}", step.name(), depname - )) + ).into()) } Some(_) => (), } @@ -147,7 +148,7 @@ impl Roadmap { /// Get a Graphviz dot language representation of a roadmap. This /// is the textual representation, and the caller needs to use the /// Graphviz dot(1) tool to create an image from it. - pub fn format_as_dot(&self, label_width: usize) -> Result> { + pub fn format_as_dot(&self, label_width: usize) -> RoadmapResult { self.validate()?; let labels = self.steps.iter().map(|step| { diff --git a/src/parser.rs b/src/parser.rs index 411c074..7da2254 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,15 +1,14 @@ use serde_yaml; use serde_yaml::Value; use std::collections::HashMap; -use std::error::Error; pub use crate::Roadmap; -pub use crate::RoadmapError; +pub use crate::RoadmapResult; pub use crate::Status; pub use crate::Step; /// Create a new roadmap from a textual YAML representation. -pub fn from_yaml(yaml: &str) -> Result> { +pub fn from_yaml(yaml: &str) -> RoadmapResult { let mut roadmap = Roadmap::new(); let map: HashMap = serde_yaml::from_str(yaml)?; @@ -23,7 +22,7 @@ pub fn from_yaml(yaml: &str) -> Result> { } // Convert a Value into a Step, if possible. -fn step_from_value(name: &str, value: &Value) -> RoadmapError { +fn step_from_value(name: &str, value: &Value) -> RoadmapResult { match value { Value::Mapping(_) => { let label = parse_label(&value); @@ -38,12 +37,12 @@ fn step_from_value(name: &str, value: &Value) -> RoadmapError { Ok(step) } - _ => Err("step is not a mapping".to_string()), + _ => Err("step is not a mapping".to_string().into()), } } // Get a sequence of depenencies. -fn parse_depends(map: &Value) -> RoadmapError> { +fn parse_depends(map: &Value) -> RoadmapResult> { let key_name = "depends"; let key = Value::String(key_name.to_string()); let mut depends: Vec<&str> = vec![]; @@ -55,11 +54,11 @@ fn parse_depends(map: &Value) -> RoadmapError> { for depname in deps.iter() { match depname { Value::String(depname) => depends.push(depname), - _ => return Err(need_list_of_names), + _ => return Err(need_list_of_names.into()), } } } - _ => return Err(need_list_of_names), + _ => return Err(need_list_of_names.into()), } Ok(depends) @@ -71,11 +70,11 @@ fn parse_label(map: &Value) -> &str { } // Get status string from a Mapping element. Default to unknown status. -fn parse_status<'a>(map: &'a Value) -> RoadmapError { +fn parse_status<'a>(map: &'a Value) -> RoadmapResult { let text = parse_string("status", map); match Status::from_text(text) { Some(status) => Ok(status), - _ => Err(format!("unknown status: {:?}", text)), + _ => Err(format!("unknown status: {:?}", text).into()), } } -- cgit v1.2.1