summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2019-10-05 10:27:58 +0300
committerLars Wirzenius <liw@liw.fi>2019-10-05 10:27:58 +0300
commitafa0029ee9306764200e00e535b8a6ce94e4954c (patch)
tree1e26d0b8b5ed4e889e662b894c1e1ece2ff6b642
parentc234660420250c03f514791e854c59e6e8432410 (diff)
downloadroadmap-afa0029ee9306764200e00e535b8a6ce94e4954c.tar.gz
Change: use RoadmapResult (nee RoadmapError) everywhere
Daniel told me about .into().
-rw-r--r--src/lib.rs2
-rw-r--r--src/map.rs13
-rw-r--r--src/parser.rs19
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<T> = Result<T, String>;
+pub type RoadmapResult<T> = Result<T, Box<dyn Error>>;
/// 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<String, Box<dyn std::error::Error>> {
+ pub fn format_as_dot(&self, label_width: usize) -> RoadmapResult<String> {
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<Roadmap, Box<dyn Error>> {
+pub fn from_yaml(yaml: &str) -> RoadmapResult<Roadmap> {
let mut roadmap = Roadmap::new();
let map: HashMap<String, serde_yaml::Value> = serde_yaml::from_str(yaml)?;
@@ -23,7 +22,7 @@ pub fn from_yaml(yaml: &str) -> Result<Roadmap, Box<dyn Error>> {
}
// Convert a Value into a Step, if possible.
-fn step_from_value(name: &str, value: &Value) -> RoadmapError<Step> {
+fn step_from_value(name: &str, value: &Value) -> RoadmapResult<Step> {
match value {
Value::Mapping(_) => {
let label = parse_label(&value);
@@ -38,12 +37,12 @@ fn step_from_value(name: &str, value: &Value) -> RoadmapError<Step> {
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<Vec<&str>> {
+fn parse_depends(map: &Value) -> RoadmapResult<Vec<&str>> {
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<Vec<&str>> {
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<Status> {
+fn parse_status<'a>(map: &'a Value) -> RoadmapResult<Status> {
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()),
}
}