summaryrefslogtreecommitdiff
path: root/src/step.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/step.rs')
-rw-r--r--src/step.rs92
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"]);
+ }
+}