summaryrefslogtreecommitdiff
path: root/src/parser.rs
blob: ac917eba030adb1438c27af3633daae9f631374d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use serde_yaml;
use serde_yaml::Value;
use std::collections::HashMap;
use std::error::Error;

pub use crate::Roadmap;
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();
    let map: HashMap<String, serde_yaml::Value> = serde_yaml::from_str(yaml)?;

    for (name, value) in map {
        let step = step_from_value(&name, &value)?;
        roadmap.add_step(&step);
    }

    validate(&roadmap)?;
    Ok(roadmap)
}

// Convert a Value into a Step, if possible.
fn step_from_value(name: &str, value: &Value) -> ParseResult<Step> {
    match value {
        Value::Mapping(_) => {
            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() {
                step.add_dependency(depname);
            }

            Ok(step)
        }
        _ => Err("step is not a mapping".to_string()),
    }
}

// Get a sequence of depenencies.
fn parse_depends(map: &Value) -> ParseResult<Vec<&str>> {
    let key_name = "depends";
    let key = Value::String(key_name.to_string());
    let mut depends: Vec<&str> = vec![];
    let need_list_of_names = format!("'depends' must be a list of step names");

    match map.get(&key) {
        None => (),
        Some(Value::Sequence(deps)) => {
            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),
    }

    Ok(depends)
}

// Get label string from a Mapping element, or empty string.
fn parse_label(map: &Value) -> &str {
    parse_string("label", map)
}

// Get status string from a Mapping element. Default to unknown status.
fn parse_status<'a>(map: &'a Value) -> ParseResult<Status> {
    let text = parse_string("status", map);
    match Status::from_text(text) {
        Some(status) => Ok(status),
        _ => Err(format!("unknown status: {:?}", text)),
    }
}

// Get string value from a Mapping field, or empty string if field
// isn't there.
fn parse_string<'a>(key_name: &str, map: &'a Value) -> &'a str {
    let key = Value::String(key_name.to_string());
    match map.get(&key) {
        Some(Value::String(s)) => s,
        _ => "",
    }
}

// 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;

    #[test]
    fn yaml_is_empty() {
        let r = from_yaml("");
        match r {
            Ok(_) => panic!("expected a parse error"),
            _ => (),
        }
    }

    #[test]
    fn yaml_is_list() {
        let r = from_yaml("[]");
        match r {
            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"),
            _ => (),
        }
    }

    #[test]
    fn yaml_unknown_dep() {
        let r = from_yaml("foo: {depends: [bar]}");
        match r {
            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"),
            _ => (),
        }
    }

    #[test]
    fn yaml_happy() {
        let roadmap = from_yaml(
            "
first:
  label: the first step
second:
  label: the second step
  depends:
  - first
",
        )
        .unwrap();

        let names: Vec<&str> = roadmap.step_names().collect();
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"first"));
        assert!(names.contains(&"second"));

        let first = roadmap.get_step("first").unwrap();
        assert_eq!(first.name(), "first");
        assert_eq!(first.label(), "the first step");
        let deps: Vec<&String> = first.dependencies().collect();
        assert_eq!(deps.len(), 0);

        let second = roadmap.get_step("second").unwrap();
        assert_eq!(second.name(), "second");
        assert_eq!(second.label(), "the second step");
        let deps: Vec<&String> = second.dependencies().collect();
        assert_eq!(deps, vec!["first"]);
    }
}