From afec096ff271b60bd5cbf5e37db776b04b4ef27b Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Sun, 4 Dec 2022 01:14:18 +0100 Subject: [PATCH] Add remove command. --- src/parser.rs | 2 ++ src/repl.rs | 8 ++++++-- src/structs.rs | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index f99751d..a7ef395 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -14,6 +14,7 @@ peg::parser! { / error_cmd() / ls_cmd() / mv_cmd() + / rm_cmd() / comment_cmd() ) { c } @@ -65,6 +66,7 @@ peg::parser! { rule add_cmd() -> Command<'input> = "a" _ name:name() { Command::Add(Rc::new(RefCell::new(name))) } rule error_cmd() -> Command<'input> = "error" _ e:$(([' '..='~'])+) { Command::Error(LKErr::Error(e)) } rule mv_cmd() -> Command<'input> = "m" _ name:word() _ folder:word() { Command::Mv(name, folder) } + rule rm_cmd() -> Command<'input> = "r" _ name:word() { Command::Rm(name) } rule comment_cmd() -> Command<'input> = "c" _ name:word() c:comment()? { Command::Comment(name, c) } } } diff --git a/src/repl.rs b/src/repl.rs index fbbc7cd..c7e68fb 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -105,7 +105,11 @@ impl<'a> LKEval<'a> { } } None => out.push("error: password not found".to_string()), - }, + } + Command::Rm(name) => match self.state.borrow_mut().db.remove(name) { + Some(pwd) => out.push(format!("removed {}", pwd.borrow().name)), + None => out.push("error: password not found".to_string()), + } Command::Help => { out.push("HELP".to_string()); } @@ -131,7 +135,7 @@ impl<'a> LKEval<'a> { LKErr::ParseError(e) => out.push(e.to_string()), LKErr::ReadError(e) => out.push(e.to_string()), LKErr::Error(e) => out.push(format!("error: {}", e.to_string())), - }, + } } LKPrint::new(out, quit, self.state.clone()) diff --git a/src/structs.rs b/src/structs.rs index e0bc14b..14681a4 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -15,6 +15,7 @@ pub enum Command<'a> { Add(PasswordRef), Ls, Mv(Name, Name), + Rm(Name), Comment(Name, Comment), Error(LKErr<'a>), Help,