Simplify the parser and define password types explicitly.

This commit is contained in:
Kiyomichi Kosaka
2022-12-03 22:57:28 +00:00
parent 47243330a9
commit 66acff9884
5 changed files with 76 additions and 35 deletions
+2 -3
View File
@@ -1,11 +1,10 @@
use crate::password::{fix_password_recursion, Password};
use crate::password::{fix_password_recursion, NameRef, PasswordRef};
use regex::{Captures, Regex};
use std::collections::HashMap;
use std::{cell::RefCell, rc::Rc};
#[derive(PartialEq, Debug)]
pub struct LK {
pub db: HashMap<Rc<String>, Rc<RefCell<Password>>>,
pub db: HashMap<NameRef, PasswordRef>,
}
impl LK {
+16 -16
View File
@@ -14,25 +14,24 @@ peg::parser! {
/ error_cmd()
/ ls_cmd()
/ mv_cmd()
/ comment_cmd()
) { c }
pub rule name() -> Password = name:(jname() / pname() / mname() / sname()) { name }
rule _() -> &'input str = s:$((" " / "\t" / "\r" / "\n")+) { s }
rule comment() -> &'input str = _ c:$([' '..='~']+) { c }
rule word() -> &'input str = n:$(['!'..='~']+) { n }
rule comment() -> String = _ c:$([' '..='~']+) { c.to_string() }
rule word() -> String = n:$(['!'..='~']+) { n.to_string() }
rule num() -> u32 = n:$(['0'..='9']+) {? n.parse().or(Err("not a number")) }
rule pname() -> Password = &(word() _ word() _ num()? mode() _ num() _ date()) pr:word() _ pn:word() _ pl:num()? pm:mode() _ ps:num() _ pd:date() pc:comment()? {
Password { prefix: Some(pr.to_string()), length: pl, name: Rc::new(pn.to_string()), mode: pm, seq: ps, date: pd, parent: None,
comment: match pc { Some(s) => Some(s.to_string()), None => None } } }
rule jname() -> Password = &(word() _ num()? mode() _ num() _ date()) pn:word() _ pl:num()? pm:mode() _ ps:num() _ pd:date() pc:comment()? {
Password { prefix: None, length: pl, name: Rc::new(pn.to_string()), mode: pm, seq: ps, date: pd, parent: None,
comment: match pc { Some(s) => Some(s.to_string()), None => None } } }
rule mname() -> Password = &(word() _ word() _ num()? mode() _ date()) pr:word() _ pn:word() _ pl:num()? pm:mode() _ pd:date() pc:comment()? {
Password { prefix: Some(pr.to_string()), length: pl, name: Rc::new(pn.to_string()), mode: pm, seq: 99, date: pd, parent: None,
comment: match pc { Some(s) => Some(s.to_string()), None => None } } }
rule sname() -> Password = &(word() _ num()? mode() _ date()) pn:word() _ pl:num()? pm:mode() _ pd:date() pc:comment()? {
Password { prefix: None, length: pl, name: Rc::new(pn.to_string()), mode: pm, seq: 99, date: pd, parent: None,
comment: match pc { Some(s) => Some(s.to_string()), None => None } } }
rule pname() -> Password = &(word() _ word() _ num()? mode() _ num() _ date()) pr:word() _ pn:word() _ pl:num()? pm:mode() _ ps:num() _ pd:date() pc:comment()?
{ Password::new(Some(pr), pn, pl, pm, ps, pd, pc) }
rule jname() -> Password = &(word() _ num()? mode() _ num() _ date()) pn:word() _ pl:num()? pm:mode() _ ps:num() _ pd:date() pc:comment()?
{ Password::new(None, pn, pl, pm, ps, pd, pc) }
rule mname() -> Password = &(word() _ word() _ num()? mode() _ date()) pr:word() _ pn:word() _ pl:num()? pm:mode() _ pd:date() pc:comment()?
{ Password::new(Some(pr), pn, pl, pm, 99, pd, pc) }
rule sname() -> Password = &(word() _ num()? mode() _ date()) pn:word() _ pl:num()? pm:mode() _ pd:date() pc:comment()?
{ Password::new(None, pn, pl, pm, 99, pd, pc) }
rule date() -> NaiveDate = y:$("-"? ['0'..='9']*<1,4>) "-" m:$(['0'..='9']*<1,2>) "-" d:$(['0'..='9']*<1,2>) {?
let year: i32 = match y.parse() { Ok(n) => n, Err(_) => return Err("year") };
let month: u32 = match m.parse() { Ok(n) => n, Err(_) => return Err("month") };
@@ -65,7 +64,8 @@ peg::parser! {
rule ls_cmd() -> Command<'input> = "ls" { Command::Ls }
rule add_cmd() -> Command<'input> = "add" _ 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> = "mv" _ name:word() _ folder:word() { Command::Mv(name.to_string(), folder.to_string()) }
rule mv_cmd() -> Command<'input> = "mv" _ name:word() _ folder:word() { Command::Mv(name, folder) }
rule comment_cmd() -> Command<'input> = "comment" _ name:word() c:comment()? { Command::Comment(name, c) }
}
}
+45 -12
View File
@@ -2,29 +2,47 @@ use crate::structs::Mode;
use chrono::naive::NaiveDate;
use std::{cell::RefCell, rc::Rc};
pub type Name = String;
pub type NameRef = Rc<Name>;
pub type Prefix = Option<String>;
pub type Comment = Option<String>;
pub type PasswordRef = Rc<RefCell<Password>>;
pub type Parent = Option<PasswordRef>;
pub type Length = Option<u32>;
pub type Seq = u32;
pub type Date = NaiveDate;
#[derive(PartialEq, Debug)]
pub struct Password {
pub parent: Option<Rc<RefCell<Password>>>,
pub prefix: Option<String>,
pub name: Rc<String>,
pub length: Option<u32>,
pub parent: Parent,
pub prefix: Prefix,
pub name: NameRef,
pub length: Length,
pub mode: Mode,
pub seq: u32,
pub date: NaiveDate,
pub comment: Option<String>,
pub seq: Seq,
pub date: Date,
pub comment: Comment,
}
impl Password {
pub fn new(prefix: Option<String>, name: String, mode: Mode, date: NaiveDate) -> Password {
pub fn new(
prefix: Prefix,
name: Name,
length: Length,
mode: Mode,
seq: Seq,
date: Date,
comment: Comment,
) -> Password {
Password {
prefix,
name: Rc::new(name),
length,
mode,
date,
comment,
parent: None,
name: Rc::new(name),
length: None,
seq: 99,
comment: None,
seq,
}
}
}
@@ -95,8 +113,11 @@ mod tests {
let p1 = Rc::new(RefCell::new(Password::new(
None,
"p1".to_string(),
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
None,
)));
p1.borrow_mut().parent = Some(p1.clone());
@@ -106,29 +127,41 @@ mod tests {
let p2 = Rc::new(RefCell::new(Password::new(
None,
"p2".to_string(),
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
None,
)));
p2.borrow_mut().parent = Some(p1.clone());
let p3 = Rc::new(RefCell::new(Password::new(
None,
"p3".to_string(),
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
None,
)));
p3.borrow_mut().parent = Some(p2.clone());
let p4 = Rc::new(RefCell::new(Password::new(
None,
"p4".to_string(),
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
None,
)));
p4.borrow_mut().parent = Some(p3.clone());
let p5 = Rc::new(RefCell::new(Password::new(
None,
"p5".to_string(),
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
None,
)));
p5.borrow_mut().parent = Some(p4.clone());
+9
View File
@@ -105,6 +105,15 @@ impl<'a> LKEval<'a> {
self.state.borrow().fix_hierarchy();
}
}
Command::Comment(name, comment) => match self.state.borrow().db.get(name) {
Some(pwd) => {
pwd.borrow_mut().comment = match comment {
Some(c) => Some(c.to_string()),
None => None,
}
}
None => out.push("error: password not found".to_string()),
},
Command::Help => {
out.push("HELP".to_string());
}
+4 -4
View File
@@ -1,5 +1,4 @@
use crate::password::Password;
use std::{cell::RefCell, rc::Rc};
use crate::password::{Comment, Name, PasswordRef};
#[derive(thiserror::Error, Debug, PartialEq)]
pub enum LKErr<'a> {
@@ -13,9 +12,10 @@ pub enum LKErr<'a> {
#[derive(PartialEq, Debug)]
pub enum Command<'a> {
Add(Rc<RefCell<Password>>),
Add(PasswordRef),
Ls,
Mv(String, String),
Mv(Name, Name),
Comment(Name, Comment),
Error(LKErr<'a>),
Help,
Quit,