summaryrefslogtreecommitdiff
path: root/src/step.rs
blob: aae69ff2f7f67b3f406f72e39e79132ee2a8cb73 (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
use super::Status;

/// A roadmap step.
///
/// See the crate documentation for an example. You
/// probably don't want to create steps manually, but via the roadmap
/// YAML parsing function.
#[derive(Clone, Debug, PartialEq)]
pub struct Step {
    name: String,
    status: Status,
    label: String,
    depends: Vec<String>,
}

impl Step {
    /// Create a new step with a name and a label.
    pub fn new(name: &str, label: &str) -> Step {
        Step {
            name: name.to_string(),
            status: Status::Unknown,
            label: label.to_string(),
            depends: vec![],
        }
    }

    /// Return the name of a step.
    pub fn name<'a>(&'a self) -> &'a str {
        &self.name
    }

    /// Return the label of a step.
    pub fn label<'a>(&'a self) -> &'a str {
        &self.label
    }

    /// Return the status of a step.
    pub fn status<'a>(&'a self) -> Status {
        self.status
    }

    /// Set the status of a step.
    pub fn set_status(&mut self, status: Status) {
        self.status = status
    }

    /// Return vector of names of dependencies for a step.
    pub fn dependencies(&self) -> impl Iterator<Item = &String> {
        self.depends.iter()
    }

    /// Add the name of a dependency to step. Steps are referred by
    /// name. Steps don't know about other steps, and can't validate
    /// that the dependency exists, so this always works.
    pub fn add_dependency(&mut self, name: &str) {
        self.depends.push(String::from(name));
    }

    /// Does this step depend on given other step?
    pub fn depends_on(&self, other_name: &str) -> bool {
        self.depends.iter().any(|depname| depname == other_name)
    }
}

#[cfg(test)]
mod tests {
    use super::{Status, Step};

    #[test]
    fn new_step() {
        let step = Step::new("myname", "my label");
        assert_eq!(step.name(), "myname");
        assert_eq!(step.status(), Status::Unknown);
        assert_eq!(step.label(), "my label");
        assert_eq!(step.dependencies().count(), 0);
    }

    #[test]
    fn set_status() {
        let mut step = Step::new("myname", "my label");
        step.set_status(Status::Next);
        assert_eq!(step.status(), Status::Next);
    }

    #[test]
    fn add_step_dependency() {
        let mut second = Step::new("second", "the second step");
        second.add_dependency("first");
        let deps: Vec<&String> = second.dependencies().collect();
        assert_eq!(deps, vec!["first"]);
    }
}