summaryrefslogtreecommitdiff
path: root/src/parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.rs')
-rw-r--r--src/parser.rs40
1 files changed, 5 insertions, 35 deletions
diff --git a/src/parser.rs b/src/parser.rs
index cb1c5ce..60bbf2a 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -4,12 +4,10 @@ use std::collections::HashMap;
use std::error::Error;
pub use crate::Roadmap;
+pub use crate::RoadmapError;
pub use crate::Status;
pub use crate::Step;
-/// Result type for roadmap parsing.
-type ParseResult<T> = Result<T, String>;
-
/// Create a new roadmap from a textual YAML representation.
pub fn from_yaml(yaml: &str) -> Result<Roadmap, Box<dyn Error>> {
let mut roadmap = Roadmap::new();
@@ -20,12 +18,12 @@ pub fn from_yaml(yaml: &str) -> Result<Roadmap, Box<dyn Error>> {
roadmap.add_step(step);
}
- validate(&roadmap)?;
+ roadmap.validate()?;
Ok(roadmap)
}
// Convert a Value into a Step, if possible.
-fn step_from_value(name: &str, value: &Value) -> ParseResult<Step> {
+fn step_from_value(name: &str, value: &Value) -> RoadmapError<Step> {
match value {
Value::Mapping(_) => {
let label = parse_label(&value);
@@ -45,7 +43,7 @@ fn step_from_value(name: &str, value: &Value) -> ParseResult<Step> {
}
// Get a sequence of depenencies.
-fn parse_depends(map: &Value) -> ParseResult<Vec<&str>> {
+fn parse_depends(map: &Value) -> RoadmapError<Vec<&str>> {
let key_name = "depends";
let key = Value::String(key_name.to_string());
let mut depends: Vec<&str> = vec![];
@@ -73,7 +71,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) -> ParseResult<Status> {
+fn parse_status<'a>(map: &'a Value) -> RoadmapError<Status> {
let text = parse_string("status", map);
match Status::from_text(text) {
Some(status) => Ok(status),
@@ -91,34 +89,6 @@ fn parse_string<'a>(key_name: &str, map: &'a Value) -> &'a str {
}
}
-// Validate that the parsed, constructed roadmap is valid.
-fn validate(roadmap: &Roadmap) -> ParseResult<()> {
- // Is there exactly one goal?
- match roadmap.count_goals() {
- 0 => return Err(format!("the roadmap doesn't have a goal")),
- 1 => (),
- _ => return Err(format!("must have exactly one goal for roadmap")),
- }
-
- // Does every dependency exist?
- for step in roadmap.iter() {
- for depname in step.dependencies() {
- match roadmap.get_step(depname) {
- None => {
- return Err(format!(
- "step {} depends on missing {}",
- step.name(),
- depname
- ))
- }
- Some(_) => (),
- }
- }
- }
-
- Ok(())
-}
-
#[cfg(test)]
mod tests {
use super::from_yaml;