From 0c3e4d5c2e68d3c448bd99f8bd2cb51b56ed54d6 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Sat, 28 Dec 2019 11:44:45 -0500 Subject: [PATCH] add a tasks method to db --- src/lib.rs | 3 +++ src/taskdb.rs | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e0be8b685..3ef1ecf6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,6 @@ +// TODO: remove this eventually when there's an API +#![allow(dead_code)] + mod errors; mod operation; mod taskdb; diff --git a/src/taskdb.rs b/src/taskdb.rs index 02fd51da0..e5d8ee195 100644 --- a/src/taskdb.rs +++ b/src/taskdb.rs @@ -32,6 +32,7 @@ struct DB { } impl DB { + /// Create a new, empty database fn new() -> DB { DB { tasks: HashMap::new(), @@ -40,6 +41,8 @@ impl DB { } } + /// Apply an operation to the DB. Aside from synchronization operations, this + /// is the only way to modify the DB. fn apply(&mut self, op: Operation) -> Result<(), Error> { match op { Operation::Create { uuid } => { @@ -71,6 +74,13 @@ impl DB { self.operations.push(op); Ok(()) } + + /// Get a read-only reference to the underlying set of tasks. + /// + /// This API is temporary, but provides query access to the DB. + fn tasks(&self) -> &HashMap> { + &self.tasks + } } #[cfg(test)] @@ -87,7 +97,7 @@ mod tests { let mut exp = HashMap::new(); exp.insert(uuid, HashMap::new()); - assert_eq!(db.tasks, exp); + assert_eq!(db.tasks(), &exp); assert_eq!(db.operations, vec![op]); } @@ -120,7 +130,7 @@ mod tests { let mut task = HashMap::new(); task.insert(String::from("title"), Value::from("\"my task\"")); exp.insert(uuid, task); - assert_eq!(db.tasks, exp); + assert_eq!(db.tasks(), &exp); assert_eq!(db.operations, vec![op1, op2]); } @@ -138,5 +148,4 @@ mod tests { Err(Error::DBError(format!("Task {} does not exist", uuid))) ); } - }