From 46fb2c6b40ccb8d4049e7f4f92b3894e3bd5e0b5 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Sat, 24 Dec 2022 15:00:07 +0100 Subject: [PATCH] Add leave command, to directly save the generated password and restructure the parser a bit. --- src/commands.rs | 8 ++++++++ src/main.rs | 1 + src/parser.rs | 33 +++++++++------------------------ src/repl.rs | 1 + src/structs.rs | 1 + 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index b45c6d6..8877500 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -105,6 +105,14 @@ impl<'a> LKEval<'a> { } } + pub fn cmd_leave(&self, out: &LKOut, name: &Name) { + let pwd = match self.state.borrow().ls.get(name) { + Some(pwd) => pwd.clone(), + None => { out.e(format!("error: {} not found", name)); return; } + }; + self.cmd_add(&out, &pwd); + } + pub fn cmd_mv(&self, out: &LKOut, name: &String, folder: &String) { match self.get_password(name) { Some(pwd) => { diff --git a/src/main.rs b/src/main.rs index 650c414..a6afb9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#![recursion_limit = "1024"] #[macro_use] extern crate lazy_static; #[allow(unused_imports)] diff --git a/src/parser.rs b/src/parser.rs index 4f16973..1fe9bca 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,29 +8,12 @@ use std::{cell::RefCell, rc::Rc}; peg::parser! { pub grammar command_parser() for str { - pub rule cmd() -> Command<'input> = (" " / "\t" / "\r" / "\n")* c:( - help_cmd() - / add_cmd() - / quit_cmd() - / error_cmd() - / ls_cmd() - / ld_cmd() - / mv_cmd() - / rm_cmd() - / pb_cmd() - / source_cmd() - / dump_cmd() - / dump_def_cmd() - / enc_cmd() - / gen_cmd() - / pass_cmd() - / unpass_cmd() - / correct_cmd() - / uncorrect_cmd() - / noop_cmd() - / comment_cmd() - ) { c } - pub rule script() -> Vec> = c:cmd() ++ "\n" { c } + pub rule cmd() -> Command<'input> = c:(info_cmd_list() / mod_cmd_list() / enc_cmd_list() / asides_cmd_list()) { c } + pub rule info_cmd_list() -> Command<'input> = (" " / "\t" / "\r" / "\n")* c:(ls_cmd() / ld_cmd() / pb_cmd() / dump_cmd() / dump_def_cmd()) { c } + pub rule mod_cmd_list() -> Command<'input> = (" " / "\t" / "\r" / "\n")* c:(add_cmd() / leave_cmd() / mv_cmd() / rm_cmd() / comment_cmd ()) { c } + pub rule asides_cmd_list() -> Command<'input> = (" " / "\t" / "\r" / "\n")* c:(help_cmd() / source_cmd() / quit_cmd() / noop_cmd() / error_cmd()) { c } + pub rule enc_cmd_list() -> Command<'input> = (" " / "\t" / "\r" / "\n")* c:(enc_cmd() / gen_cmd() / pass_cmd() / unpass_cmd() / correct_cmd() / uncorrect_cmd()) { c } + pub rule script() -> Vec> = c:(info_cmd_list() / mod_cmd_list() / enc_cmd_list() / asides_cmd_list()) ++ "\n" { c } rule _() -> &'input str = s:$((" " / "\t" / "\r")+) { s } rule comment() -> String = _ c:$([' '..='~']+) { c.to_string() } @@ -83,7 +66,8 @@ peg::parser! { } } rule mode() -> Mode = m:(umode() / rmode()) { m } - rule noop_cmd() -> Command<'input> = (" " / "\r" / "\n" / "\t")* ("#" comment())? { Command::Noop } + + rule noop_cmd() -> Command<'input> = ("#" [' '..='~']*)? { Command::Noop } rule help_cmd() -> Command<'input> = "help" { Command::Help } rule quit_cmd() -> Command<'input> = "quit" { Command::Quit } rule pb_cmd() -> Command<'input> = "pb" _ e:$(([' '..='~'])+) { Command::PasteBuffer(e.to_string()) } @@ -93,6 +77,7 @@ peg::parser! { rule ls_cmd() -> Command<'input> = "ls" f:comment()? { Command::Ls(f.unwrap_or(".".to_string())) } rule ld_cmd() -> Command<'input> = "ld" f:comment()? { Command::Ld(f.unwrap_or(".".to_string())) } rule add_cmd() -> Command<'input> = "add" _ name:name() { Command::Add(Rc::new(RefCell::new(name))) } + rule leave_cmd() -> Command<'input> = "leave" _ name:word() { Command::Leave(name.to_string()) } rule gen_cmd() -> Command<'input> = "gen" n:num()? _ name:name() { Command::Gen(match n { Some(n) => n, None => 10_u32 }, Rc::new(RefCell::new(name))) } diff --git a/src/repl.rs b/src/repl.rs index f3361df..dc83acb 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -99,6 +99,7 @@ impl<'a> LKEval<'a> { Command::Ls(filter) => self.cmd_ls(&out, filter.to_string(), |a,b| a.borrow().name.cmp(&b.borrow().name)), Command::Ld(filter) => self.cmd_ls(&out, filter.to_string(), |a,b| b.borrow().date.cmp(&a.borrow().date)), Command::Add(name) => self.cmd_add(&out, &name), + Command::Leave(name) => self.cmd_leave(&out, &name), Command::Comment(name, comment) => self.cmd_comment(&out, &name, &comment), Command::Rm(name) => match self.get_password(name) { Some(pwd) => { diff --git a/src/structs.rs b/src/structs.rs index 44c078f..20c7ef8 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -56,6 +56,7 @@ pub enum LKErr<'a> { #[derive(PartialEq, Debug)] pub enum Command<'a> { Add(PasswordRef), + Leave(Name), Ls(String), Ld(String), Mv(Name, Name),