diff --git a/src/main.rs b/src/main.rs index 4d9f1bc..a5ba8f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,20 +20,25 @@ struct Cli { #[derive(Subcommand)] enum Commands { - + /// Initialization of storage and config, use this in first time of usage of mps Init, + /// Lists all ids stored in db + List, + /// Adds new item with unique id to the db Add { #[arg(value_name="item_id")] id: String }, - - /// Lists all ids stored in db - List - // TODO: show + /// Show content of item + Show { + #[arg(value_name="item_id")] + id: String + } + // TODO: edit } @@ -94,6 +99,17 @@ fn init() -> io::Result<()> { Ok(()) } +fn list() -> io:: Result<()> { + let passphrase = login()?; + let st = Storage::from_db(passphrase)?; + let mut vec: Vec<_> = st.items.iter().collect(); + vec.sort(); + for item in &vec { + println!("{}", item.id); + } + Ok(()) +} + fn add(id: &String) -> io::Result<()> { let passphrase = login()?; let mut st = Storage::from_db(passphrase)?; @@ -102,6 +118,7 @@ fn add(id: &String) -> io::Result<()> { return Err(io::Error::new( io::ErrorKind::InvalidInput, format!("Dublicate item id: {}. Item id's must be unique.", id) + // TODO: ask to edit [Y/n] )) } @@ -112,14 +129,19 @@ fn add(id: &String) -> io::Result<()> { Ok(()) } -fn list() -> io:: Result<()> { - let passphrase = login()?; - let st = Storage::from_db(passphrase)?; - let mut vec: Vec<_> = st.items.iter().collect(); - vec.sort(); - for item in &vec { - println!("{}", item.id); +fn show(id: &String) -> io::Result<()> { + let passphrase = login()?; + let mut st = Storage::from_db(passphrase)?; + if !st.contains(id) { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("Can`t find id: {}", id) + )); } + + let item = st.get(id); + println!("content:"); + println!("{}", item.content); Ok(()) } @@ -132,6 +154,9 @@ fn run_command() -> io::Result<()> { Some(Commands::Add{ id }) => { add(id)?; } + Some(Commands::Show{ id }) => { + show(id)?; + } Some(Commands::List) => { list()?; } diff --git a/src/storage.rs b/src/storage.rs index a50c61f..c605171 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -179,6 +179,11 @@ impl Storage { self.items.contains(&item) } + pub fn get(&self, id: &String) -> &Item { + let item = Item::from_empty(id.clone()); + self.items.get(&item).unwrap() + } + pub fn dump(&self) -> io::Result<()> { let mut file = fs::OpenOptions::new() .write(true)