add a tasks method to db

This commit is contained in:
Dustin J. Mitchell 2019-12-28 11:44:45 -05:00
parent 83b2318a06
commit 0c3e4d5c2e
2 changed files with 15 additions and 3 deletions

View file

@ -1,3 +1,6 @@
// TODO: remove this eventually when there's an API
#![allow(dead_code)]
mod errors; mod errors;
mod operation; mod operation;
mod taskdb; mod taskdb;

View file

@ -32,6 +32,7 @@ struct DB {
} }
impl DB { impl DB {
/// Create a new, empty database
fn new() -> DB { fn new() -> DB {
DB { DB {
tasks: HashMap::new(), 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> { fn apply(&mut self, op: Operation) -> Result<(), Error> {
match op { match op {
Operation::Create { uuid } => { Operation::Create { uuid } => {
@ -71,6 +74,13 @@ impl DB {
self.operations.push(op); self.operations.push(op);
Ok(()) 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<Uuid, HashMap<String, Value>> {
&self.tasks
}
} }
#[cfg(test)] #[cfg(test)]
@ -87,7 +97,7 @@ mod tests {
let mut exp = HashMap::new(); let mut exp = HashMap::new();
exp.insert(uuid, HashMap::new()); exp.insert(uuid, HashMap::new());
assert_eq!(db.tasks, exp); assert_eq!(db.tasks(), &exp);
assert_eq!(db.operations, vec![op]); assert_eq!(db.operations, vec![op]);
} }
@ -120,7 +130,7 @@ mod tests {
let mut task = HashMap::new(); let mut task = HashMap::new();
task.insert(String::from("title"), Value::from("\"my task\"")); task.insert(String::from("title"), Value::from("\"my task\""));
exp.insert(uuid, task); exp.insert(uuid, task);
assert_eq!(db.tasks, exp); assert_eq!(db.tasks(), &exp);
assert_eq!(db.operations, vec![op1, op2]); assert_eq!(db.operations, vec![op1, op2]);
} }
@ -138,5 +148,4 @@ mod tests {
Err(Error::DBError(format!("Task {} does not exist", uuid))) Err(Error::DBError(format!("Task {} does not exist", uuid)))
); );
} }
} }