From c66fa2792df55fae7833a81ec14cc07ddd928b51 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sat, 21 Sep 2024 10:39:13 +0000 Subject: [PATCH] basic delete --- src/main.rs | 36 +++++++++++++++++++++++++++++------- src/storage.rs | 11 +++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index bc3275d..f6dea52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,11 @@ enum Commands { Edit { #[arg(value_name="item_id")] id: String + }, + + Delete { + #[arg(value_name="item_id")] + id: String } } @@ -157,6 +162,20 @@ impl MPS { Ok(()) } + fn show(&mut self, id: &String) -> io::Result<()> { + self.login()?; + let st = self.storage.as_ref().unwrap(); + if !st.contains(id) { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("Can`t find id: {}", id) + )); + } + let item = st.get(id); + editor::open_to_show(&item.content)?; + Ok(()) + } + fn add(&mut self, id: &String) -> io::Result<()> { self.login()?; let st = self.storage.as_mut().unwrap(); @@ -190,29 +209,28 @@ impl MPS { PROMPT::NO => return Ok(()) } } - let mut item = (*st.get(id)).clone(); let new_content = editor::open_to_edit(&item.content)?; item.content = new_content; st.update(item); st.dump()?; - Ok(()) } - fn show(&mut self, id: &String) -> io::Result<()> { + fn delete(&mut self, id: &String) -> io::Result<()> { self.login()?; - let st = self.storage.as_ref().unwrap(); + let st = self.storage.as_mut().unwrap(); if !st.contains(id) { return Err(io::Error::new( io::ErrorKind::InvalidInput, - format!("Can`t find id: {}", id) + "No such item" )); } - let item = st.get(id); - editor::open_to_show(&item.content)?; + st.remove(id); + st.dump()?; Ok(()) } + } fn run_command() -> io::Result<()> { @@ -237,6 +255,10 @@ fn run_command() -> io::Result<()> { let mut mps = MPS::new(); mps.edit(id)? } + Some(Commands::Delete { id }) => { + let mut mps = MPS::new(); + mps.delete(id)? + } None => { match Storage::check_installed() { Ok(()) => (), diff --git a/src/storage.rs b/src/storage.rs index 8d7bdf1..312e201 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -98,6 +98,8 @@ impl Storage { "Bad storage db format: no passphrase in the beginnning" )), }; + // TODO: only in debug mode + //println!("passphrase ok"); if !encoder.test_encoded_passphrase(passtest)? { return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase")); } @@ -106,9 +108,13 @@ impl Storage { Ok(line) => { if id.is_none() { let line = encoder.decrypt(line)?; + // TODO: only in debug mode + //println!("{}", line); id = Some(line); } else { let content = encoder.decrypt(line)?; + // TODO: only in debug + //println!("{}", content); items.insert(Item::from(id.unwrap(), content)); id = None; } @@ -189,6 +195,11 @@ impl Storage { self.items.insert(item); } + pub fn remove(&mut self, id: &String) { + let item = Item::from_empty(id.clone()); + self.items.remove(&item); + } + pub fn dump(&self) -> io::Result<()> { let mut file = fs::OpenOptions::new() .write(true)