diff options
author | Lars Wirzenius <liw@liw.fi> | 2019-09-28 10:31:50 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2019-09-28 11:36:36 +0300 |
commit | 6d4af8bd7fbfa88a7dd3820af2b557fe072a8311 (patch) | |
tree | c677aafd7fc44ffe763b2b25610bdfab0c059219 /src/step.rs | |
parent | 03b9822809b71fe302c83df2ddc832afd58777e0 (diff) | |
download | roadmap-6d4af8bd7fbfa88a7dd3820af2b557fe072a8311.tar.gz |
Change: move Status, Step, Roadmap, and parser to separate modules
Diffstat (limited to 'src/step.rs')
-rw-r--r-- | src/step.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/step.rs b/src/step.rs new file mode 100644 index 0000000..acbb078 --- /dev/null +++ b/src/step.rs @@ -0,0 +1,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::{Step, Status}; + + #[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"]); + } +} |