From d13ed1129ab51dbc659d791ea5d41b4dd3f086bb Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Wed, 14 Dec 2022 16:22:04 +0100 Subject: [PATCH] Format code and fix tests. --- src/parser.rs | 133 +++++++++++++++++++++++++++++++++++++++++++++----- src/repl.rs | 7 ++- 2 files changed, 127 insertions(+), 13 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 1d9ae77..48efcf8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -92,18 +92,129 @@ mod tests { #[test] fn parse_script_test() { - assert_eq!(command_parser::script(r###"add t1 -add t2 -add t3"###), Ok(vec![Command::Add(Rc::new(RefCell::new(Password { parent: None, prefix: None, name: Rc::new("t1".to_string()), length: None, mode: Mode::NoSpaceCamel, seq: 99, date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), comment: None }))), Command::Add(Rc::new(RefCell::new(Password { parent: None, prefix: None, name: Rc::new("t2".to_string()), length: None, mode: Mode::NoSpaceCamel, seq: 99, date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), comment: None }))), Command::Add(Rc::new(RefCell::new(Password { parent: None, prefix: None, name: Rc::new("t3".to_string()), length: None, mode: Mode::NoSpaceCamel, seq: 99, date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), comment: None })))])); - assert_eq!(command_parser::script(r###"add t1 -add t2 -add t3 -"###), Ok(vec![Command::Add(Rc::new(RefCell::new(Password { parent: None, prefix: None, name: Rc::new("t1".to_string()), length: None, mode: Mode::NoSpaceCamel, seq: 99, date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), comment: None }))), Command::Add(Rc::new(RefCell::new(Password { parent: None, prefix: None, name: Rc::new("t2".to_string()), length: None, mode: Mode::NoSpaceCamel, seq: 99, date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), comment: None }))), Command::Add(Rc::new(RefCell::new(Password { parent: None, prefix: None, name: Rc::new("t3".to_string()), length: None, mode: Mode::NoSpaceCamel, seq: 99, date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), comment: None }))), Command::Noop])); - assert_eq!(command_parser::script(r###"add t1 -add t2 -add t3 + assert_eq!( + command_parser::script( + r###"add t1 C 99 2022-12-14 +add t2 C 99 2022-12-14 +add t3 C 99 2022-12-14"### + ), + Ok(vec![ + Command::Add(Rc::new(RefCell::new(Password { + parent: None, + prefix: None, + name: Rc::new("t1".to_string()), + length: None, + mode: Mode::NoSpaceCamel, + seq: 99, + date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), + comment: None + }))), + Command::Add(Rc::new(RefCell::new(Password { + parent: None, + prefix: None, + name: Rc::new("t2".to_string()), + length: None, + mode: Mode::NoSpaceCamel, + seq: 99, + date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), + comment: None + }))), + Command::Add(Rc::new(RefCell::new(Password { + parent: None, + prefix: None, + name: Rc::new("t3".to_string()), + length: None, + mode: Mode::NoSpaceCamel, + seq: 99, + date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), + comment: None + }))) + ]) + ); + assert_eq!( + command_parser::script( + r###"add t1 C 99 2022-12-14 +add t2 C 99 2022-12-14 +add t3 C 99 2022-12-14 +"### + ), + Ok(vec![ + Command::Add(Rc::new(RefCell::new(Password { + parent: None, + prefix: None, + name: Rc::new("t1".to_string()), + length: None, + mode: Mode::NoSpaceCamel, + seq: 99, + date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), + comment: None + }))), + Command::Add(Rc::new(RefCell::new(Password { + parent: None, + prefix: None, + name: Rc::new("t2".to_string()), + length: None, + mode: Mode::NoSpaceCamel, + seq: 99, + date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), + comment: None + }))), + Command::Add(Rc::new(RefCell::new(Password { + parent: None, + prefix: None, + name: Rc::new("t3".to_string()), + length: None, + mode: Mode::NoSpaceCamel, + seq: 99, + date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), + comment: None + }))), + Command::Noop + ]) + ); + assert_eq!( + command_parser::script( + r###"add t1 C 99 2022-12-14 +add t2 C 99 2022-12-14 +add t3 C 99 2022-12-14 # some comment -"###), Ok(vec![Command::Add(Rc::new(RefCell::new(Password { parent: None, prefix: None, name: Rc::new("t1".to_string()), length: None, mode: Mode::NoSpaceCamel, seq: 99, date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), comment: None }))), Command::Add(Rc::new(RefCell::new(Password { parent: None, prefix: None, name: Rc::new("t2".to_string()), length: None, mode: Mode::NoSpaceCamel, seq: 99, date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), comment: None }))), Command::Add(Rc::new(RefCell::new(Password { parent: None, prefix: None, name: Rc::new("t3".to_string()), length: None, mode: Mode::NoSpaceCamel, seq: 99, date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), comment: None }))), Command::Noop, Command::Noop])); +"### + ), + Ok(vec![ + Command::Add(Rc::new(RefCell::new(Password { + parent: None, + prefix: None, + name: Rc::new("t1".to_string()), + length: None, + mode: Mode::NoSpaceCamel, + seq: 99, + date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), + comment: None + }))), + Command::Add(Rc::new(RefCell::new(Password { + parent: None, + prefix: None, + name: Rc::new("t2".to_string()), + length: None, + mode: Mode::NoSpaceCamel, + seq: 99, + date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), + comment: None + }))), + Command::Add(Rc::new(RefCell::new(Password { + parent: None, + prefix: None, + name: Rc::new("t3".to_string()), + length: None, + mode: Mode::NoSpaceCamel, + seq: 99, + date: NaiveDate::from_ymd_opt(2022, 12, 14).unwrap(), + comment: None + }))), + Command::Noop, + Command::Noop + ]) + ); } #[test] diff --git a/src/repl.rs b/src/repl.rs index 44238af..42cf475 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -1,8 +1,8 @@ use home::home_dir; +use regex::Regex; use rpassword::prompt_password; use rustyline::error::ReadlineError; use rustyline::Editor; -use regex::Regex; use std::{cell::RefCell, rc::Rc}; use crate::lk::LK; @@ -179,7 +179,10 @@ impl<'a> LKEval<'a> { fn cmd_ls(&self, out: &mut Vec, filter: String) { let re = match Regex::new(&filter) { Ok(re) => re, - Err(e) => { out.push(format!("error: failed to parse re: {:?}", e)); return; } + Err(e) => { + out.push(format!("error: failed to parse re: {:?}", e)); + return; + } }; let mut tmp: Vec = vec![]; for (_, name) in &self.state.borrow().db {