Browse Source

basic delete

fix-typo
Coin de Gamma 3 months ago
parent
commit
c66fa2792d
  1. 36
      src/main.rs
  2. 11
      src/storage.rs

36
src/main.rs

@ -46,6 +46,11 @@ enum Commands {
Edit { Edit {
#[arg(value_name="item_id")] #[arg(value_name="item_id")]
id: String id: String
},
Delete {
#[arg(value_name="item_id")]
id: String
} }
} }
@ -157,6 +162,20 @@ impl MPS {
Ok(()) 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<()> { fn add(&mut self, id: &String) -> io::Result<()> {
self.login()?; self.login()?;
let st = self.storage.as_mut().unwrap(); let st = self.storage.as_mut().unwrap();
@ -190,29 +209,28 @@ impl MPS {
PROMPT::NO => return Ok(()) PROMPT::NO => return Ok(())
} }
} }
let mut item = (*st.get(id)).clone(); let mut item = (*st.get(id)).clone();
let new_content = editor::open_to_edit(&item.content)?; let new_content = editor::open_to_edit(&item.content)?;
item.content = new_content; item.content = new_content;
st.update(item); st.update(item);
st.dump()?; st.dump()?;
Ok(()) Ok(())
} }
fn show(&mut self, id: &String) -> io::Result<()> { fn delete(&mut self, id: &String) -> io::Result<()> {
self.login()?; self.login()?;
let st = self.storage.as_ref().unwrap(); let st = self.storage.as_mut().unwrap();
if !st.contains(id) { if !st.contains(id) {
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::InvalidInput, io::ErrorKind::InvalidInput,
format!("Can`t find id: {}", id) "No such item"
)); ));
} }
let item = st.get(id); st.remove(id);
editor::open_to_show(&item.content)?; st.dump()?;
Ok(()) Ok(())
} }
} }
fn run_command() -> io::Result<()> { fn run_command() -> io::Result<()> {
@ -237,6 +255,10 @@ fn run_command() -> io::Result<()> {
let mut mps = MPS::new(); let mut mps = MPS::new();
mps.edit(id)? mps.edit(id)?
} }
Some(Commands::Delete { id }) => {
let mut mps = MPS::new();
mps.delete(id)?
}
None => { None => {
match Storage::check_installed() { match Storage::check_installed() {
Ok(()) => (), Ok(()) => (),

11
src/storage.rs

@ -98,6 +98,8 @@ impl Storage {
"Bad storage db format: no passphrase in the beginnning" "Bad storage db format: no passphrase in the beginnning"
)), )),
}; };
// TODO: only in debug mode
//println!("passphrase ok");
if !encoder.test_encoded_passphrase(passtest)? { if !encoder.test_encoded_passphrase(passtest)? {
return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase")); return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase"));
} }
@ -106,9 +108,13 @@ impl Storage {
Ok(line) => { Ok(line) => {
if id.is_none() { if id.is_none() {
let line = encoder.decrypt(line)?; let line = encoder.decrypt(line)?;
// TODO: only in debug mode
//println!("{}", line);
id = Some(line); id = Some(line);
} else { } else {
let content = encoder.decrypt(line)?; let content = encoder.decrypt(line)?;
// TODO: only in debug
//println!("{}", content);
items.insert(Item::from(id.unwrap(), content)); items.insert(Item::from(id.unwrap(), content));
id = None; id = None;
} }
@ -189,6 +195,11 @@ impl Storage {
self.items.insert(item); 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<()> { pub fn dump(&self) -> io::Result<()> {
let mut file = fs::OpenOptions::new() let mut file = fs::OpenOptions::new()
.write(true) .write(true)

Loading…
Cancel
Save