add a simple CLI

This commit is contained in:
Dustin J. Mitchell
2020-01-01 18:52:40 -05:00
parent b898ec1fde
commit e17943d878
6 changed files with 111 additions and 2 deletions

39
src/bin/task.rs Normal file
View File

@@ -0,0 +1,39 @@
extern crate clap;
use clap::{App, Arg, SubCommand};
use ot::{Replica, DB};
use uuid::Uuid;
fn main() {
let matches = App::new("Rask")
.version("0.1")
.author("Dustin J. Mitchell <dustin@v.igoro.us>")
.about("Replacement for TaskWarrior")
.subcommand(
SubCommand::with_name("add")
.about("adds a task")
.arg(Arg::with_name("title").help("task title").required(true)),
)
.subcommand(SubCommand::with_name("list").about("lists tasks"))
.get_matches();
let mut replica = Replica::new(DB::new().into());
match matches.subcommand() {
("add", Some(matches)) => {
let uuid = Uuid::new_v4();
replica.create_task(uuid).unwrap();
replica
.update_task(uuid, "title", Some(matches.value_of("title").unwrap()))
.unwrap();
}
("list", _) => {
for task in replica.all_tasks() {
println!("{:?}", task);
}
}
("", None) => {
unreachable!();
}
_ => unreachable!(),
};
}

View File

@@ -8,5 +8,6 @@ mod server;
mod taskdb;
pub use operation::Operation;
pub use replica::Replica;
pub use server::Server;
pub use taskdb::DB;

View File

@@ -6,7 +6,7 @@ use std::collections::HashMap;
use uuid::Uuid;
/// A replica represents an instance of a user's task data.
struct Replica {
pub struct Replica {
taskdb: Box<DB>,
}
@@ -46,6 +46,11 @@ impl Replica {
})
}
/// Get all tasks as an iterator of (&Uuid, &HashMap)
pub fn all_tasks(&self) -> impl Iterator<Item = (&Uuid, &HashMap<String, String>)> {
self.taskdb.tasks().iter()
}
/// Get an existing task by its UUID
pub fn get_task(&self, uuid: &Uuid) -> Option<&HashMap<String, String>> {
self.taskdb.tasks().get(&uuid)