Reformat code.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use crate::password::{fix_password_recursion, Password};
|
||||
use regex::{Captures, Regex};
|
||||
use std::collections::HashMap;
|
||||
use regex::{Regex, Captures};
|
||||
use crate::password::{Password, fix_password_recursion};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct LK {
|
||||
@@ -21,15 +21,21 @@ impl LK {
|
||||
if name.borrow().comment.is_some() {
|
||||
let mut folder: Option<String> = None;
|
||||
let prev_comment = name.borrow().comment.as_ref().unwrap().clone();
|
||||
let comment = RE.replace(prev_comment.as_str(), |c: &Captures| { folder = Some(c[1].to_string()); "" });
|
||||
let comment = RE.replace(prev_comment.as_str(), |c: &Captures| {
|
||||
folder = Some(c[1].to_string());
|
||||
""
|
||||
});
|
||||
if folder.is_some() {
|
||||
let folder_name = folder.unwrap();
|
||||
for (_, entry) in &self.db {
|
||||
if *entry.borrow().name == *folder_name {
|
||||
let mut tmp = name.borrow_mut();
|
||||
tmp.parent = Some(entry.clone());
|
||||
if comment.len() == 0 { tmp.comment = None }
|
||||
else { tmp.comment = Some(comment.to_string()) }
|
||||
if comment.len() == 0 {
|
||||
tmp.comment = None
|
||||
} else {
|
||||
tmp.comment = Some(comment.to_string())
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+6
-9
@@ -1,24 +1,21 @@
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
mod structs;
|
||||
mod password;
|
||||
mod parser;
|
||||
mod repl;
|
||||
mod lk;
|
||||
mod parser;
|
||||
mod password;
|
||||
mod repl;
|
||||
mod structs;
|
||||
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use rustyline::Editor;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use crate::lk::LK;
|
||||
use crate::repl::LKRead;
|
||||
|
||||
pub fn main() {
|
||||
let lk = Rc::new(RefCell::new(LK::new()));
|
||||
let mut lkread = LKRead::new(
|
||||
Editor::<()>::new().unwrap(),
|
||||
String::from("❯ "),
|
||||
lk.clone());
|
||||
let mut lkread = LKRead::new(Editor::<()>::new().unwrap(), String::from("❯ "), lk.clone());
|
||||
|
||||
while lkread.read().eval().print() {
|
||||
lkread.refresh();
|
||||
|
||||
+183
-55
@@ -1,11 +1,11 @@
|
||||
extern crate peg;
|
||||
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use chrono::naive::NaiveDate;
|
||||
use crate::structs::{Command, LKErr, Mode};
|
||||
use crate::password::Password;
|
||||
use crate::structs::{Command, LKErr, Mode};
|
||||
use chrono::naive::NaiveDate;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
peg::parser!{
|
||||
peg::parser! {
|
||||
pub grammar command_parser() for str {
|
||||
pub rule cmd() -> Command<'input> = c:(
|
||||
help_cmd()
|
||||
@@ -75,56 +75,184 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn parse_password_test() {
|
||||
assert_eq!(command_parser::name("ableton89 R 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: None, mode: Mode::Regular,
|
||||
length: None, seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string()) }));
|
||||
assert_eq!(command_parser::name("ableton89 U 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: None, mode: Mode::RegularUpcase,
|
||||
length: None, seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string()) }));
|
||||
assert_eq!(command_parser::name("ableton89 U 2020-12-09"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: None, mode: Mode::RegularUpcase,
|
||||
length: None, seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(), comment: None }));
|
||||
assert_eq!(command_parser::name("#W9 ableton89 R 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: Some("#W9".to_string()), mode: Mode::Regular,
|
||||
length: None, seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string()) }));
|
||||
assert_eq!(command_parser::name("#W9 ableton89 N 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: Some("#W9".to_string()), mode: Mode::NoSpace,
|
||||
length: None, seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string()) }));
|
||||
assert_eq!(command_parser::name("#W9 ableton89 UN 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: Some("#W9".to_string()), mode: Mode::NoSpaceUpcase,
|
||||
length: None, seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string()) }));
|
||||
assert_eq!(command_parser::name("#W9 ableton89 20R 99 2020-12-09 a b c"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: Some("#W9".to_string()), mode: Mode::Regular,
|
||||
length: Some(20), seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string()) }));
|
||||
assert_eq!(command_parser::name("#W9 ableton89 20UR 99 2020-12-09 a b c"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: Some("#W9".to_string()), mode: Mode::RegularUpcase,
|
||||
length: Some(20), seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string()) }));
|
||||
assert_eq!(command_parser::name("#W9 ableton89 20UH 99 2020-12-09 a b c"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: Some("#W9".to_string()), mode: Mode::HexUpcase,
|
||||
length: Some(20), seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string()) }));
|
||||
assert_eq!(command_parser::name("#W9 ableton89 20UB 99 2020-12-09 a b c"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: Some("#W9".to_string()), mode: Mode::Base64Upcase,
|
||||
length: Some(20), seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string()) }));
|
||||
assert_eq!(command_parser::name("#W9 ableton89 20D 99 2020-12-09 a b c"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: Some("#W9".to_string()), mode: Mode::Decimal,
|
||||
length: Some(20), seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string()) }));
|
||||
assert_eq!(command_parser::name("ableton89 20D 98 2020-12-09 a b c"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: None, mode: Mode::Decimal,
|
||||
length: Some(20), seq: 98, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string()) }));
|
||||
assert_eq!(command_parser::name("ableton89 20D 2020-12-09 a b c"),
|
||||
Ok(Password { name: Rc::new("ableton89".to_string()), parent: None, prefix: None, mode: Mode::Decimal,
|
||||
length: Some(20), seq: 99, date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string()) }));
|
||||
assert_eq!(
|
||||
command_parser::name(
|
||||
"ableton89 R 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"
|
||||
),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::Regular,
|
||||
length: None,
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name(
|
||||
"ableton89 U 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"
|
||||
),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::RegularUpcase,
|
||||
length: None,
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name("ableton89 U 2020-12-09"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::RegularUpcase,
|
||||
length: None,
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: None
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name(
|
||||
"#W9 ableton89 R 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"
|
||||
),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::Regular,
|
||||
length: None,
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name(
|
||||
"#W9 ableton89 N 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"
|
||||
),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::NoSpace,
|
||||
length: None,
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name(
|
||||
"#W9 ableton89 UN 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"
|
||||
),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::NoSpaceUpcase,
|
||||
length: None,
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("xx.ableton@domain.info https://www.ableton.com".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20R 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::Regular,
|
||||
length: Some(20),
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20UR 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::RegularUpcase,
|
||||
length: Some(20),
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20UH 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::HexUpcase,
|
||||
length: Some(20),
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20UB 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::Base64Upcase,
|
||||
length: Some(20),
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20D 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::Decimal,
|
||||
length: Some(20),
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name("ableton89 20D 98 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::Decimal,
|
||||
length: Some(20),
|
||||
seq: 98,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
command_parser::name("ableton89 20D 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::Decimal,
|
||||
length: Some(20),
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2020, 12, 09).unwrap(),
|
||||
comment: Some("a b c".to_string())
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+78
-19
@@ -1,6 +1,6 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use chrono::naive::NaiveDate;
|
||||
use crate::structs::Mode;
|
||||
use chrono::naive::NaiveDate;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct Password {
|
||||
@@ -16,22 +16,41 @@ pub struct Password {
|
||||
|
||||
impl Password {
|
||||
pub fn new(prefix: Option<String>, name: String, mode: Mode, date: NaiveDate) -> Password {
|
||||
Password { prefix, mode, date,
|
||||
Password {
|
||||
prefix,
|
||||
mode,
|
||||
date,
|
||||
parent: None,
|
||||
name: Rc::new(name),
|
||||
length: None,
|
||||
seq: 99,
|
||||
comment: None }
|
||||
comment: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::string::ToString for Password {
|
||||
fn to_string(&self) -> String {
|
||||
let prefix = match self.prefix.as_ref() { Some(s) => format!("{} ", s), None => "".to_string() };
|
||||
let length = match self.length { Some(l) => format!("{}", l), None => "".to_string() };
|
||||
let comment = match self.comment.as_ref() { Some(s) => format!(" {}", s), None => "".to_string() };
|
||||
let parent = match &self.parent { Some(s) => format!(" ^{}", s.borrow().name), None => "".to_string() };
|
||||
format!("{}{} {}{} {} {}{}{}", prefix, self.name, length, self.mode, self.seq, self.date, comment, parent)
|
||||
let prefix = match self.prefix.as_ref() {
|
||||
Some(s) => format!("{} ", s),
|
||||
None => "".to_string(),
|
||||
};
|
||||
let length = match self.length {
|
||||
Some(l) => format!("{}", l),
|
||||
None => "".to_string(),
|
||||
};
|
||||
let comment = match self.comment.as_ref() {
|
||||
Some(s) => format!(" {}", s),
|
||||
None => "".to_string(),
|
||||
};
|
||||
let parent = match &self.parent {
|
||||
Some(s) => format!(" ^{}", s.borrow().name),
|
||||
None => "".to_string(),
|
||||
};
|
||||
format!(
|
||||
"{}{} {}{} {} {}{}{}",
|
||||
prefix, self.name, length, self.mode, self.seq, self.date, comment, parent
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,11 +59,26 @@ pub fn fix_password_recursion(entry: Rc<RefCell<Password>>) {
|
||||
let mut t2 = entry;
|
||||
let mut t3: Option<Rc<RefCell<Password>>> = None;
|
||||
loop {
|
||||
t2 = match &t2.clone().borrow().parent { Some(o) => o.clone(), None => break };
|
||||
if std::ptr::eq(&*t1.borrow(), &*t2.borrow()) { t3 = Some(t2.clone()); break; }
|
||||
t1 = match &t1.clone().borrow().parent { Some(o) => o.clone(), None => break };
|
||||
t2 = match &t2.clone().borrow().parent { Some(o) => o.clone(), None => break };
|
||||
if std::ptr::eq(&*t1.borrow(), &*t2.borrow()) { t3 = Some(t2.clone()); break; }
|
||||
t2 = match &t2.clone().borrow().parent {
|
||||
Some(o) => o.clone(),
|
||||
None => break,
|
||||
};
|
||||
if std::ptr::eq(&*t1.borrow(), &*t2.borrow()) {
|
||||
t3 = Some(t2.clone());
|
||||
break;
|
||||
}
|
||||
t1 = match &t1.clone().borrow().parent {
|
||||
Some(o) => o.clone(),
|
||||
None => break,
|
||||
};
|
||||
t2 = match &t2.clone().borrow().parent {
|
||||
Some(o) => o.clone(),
|
||||
None => break,
|
||||
};
|
||||
if std::ptr::eq(&*t1.borrow(), &*t2.borrow()) {
|
||||
t3 = Some(t2.clone());
|
||||
break;
|
||||
}
|
||||
}
|
||||
match t3 {
|
||||
Some(o) => o.borrow_mut().parent = None,
|
||||
@@ -58,19 +92,44 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn exec_recursion_test() {
|
||||
let p1 = Rc::new(RefCell::new(Password::new(None, "p1".to_string(), Mode::Regular, NaiveDate::from_ymd_opt(2022, 12, 3).unwrap())));
|
||||
let p1 = Rc::new(RefCell::new(Password::new(
|
||||
None,
|
||||
"p1".to_string(),
|
||||
Mode::Regular,
|
||||
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
|
||||
)));
|
||||
|
||||
p1.borrow_mut().parent = Some(p1.clone());
|
||||
fix_password_recursion(p1.clone());
|
||||
assert_eq!(p1.borrow().parent, None);
|
||||
|
||||
let p2 = Rc::new(RefCell::new(Password::new(None, "p2".to_string(), Mode::Regular, NaiveDate::from_ymd_opt(2022, 12, 3).unwrap())));
|
||||
let p2 = Rc::new(RefCell::new(Password::new(
|
||||
None,
|
||||
"p2".to_string(),
|
||||
Mode::Regular,
|
||||
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
|
||||
)));
|
||||
p2.borrow_mut().parent = Some(p1.clone());
|
||||
let p3 = Rc::new(RefCell::new(Password::new(None, "p3".to_string(), Mode::Regular, NaiveDate::from_ymd_opt(2022, 12, 3).unwrap())));
|
||||
let p3 = Rc::new(RefCell::new(Password::new(
|
||||
None,
|
||||
"p3".to_string(),
|
||||
Mode::Regular,
|
||||
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
|
||||
)));
|
||||
p3.borrow_mut().parent = Some(p2.clone());
|
||||
let p4 = Rc::new(RefCell::new(Password::new(None, "p4".to_string(), Mode::Regular, NaiveDate::from_ymd_opt(2022, 12, 3).unwrap())));
|
||||
let p4 = Rc::new(RefCell::new(Password::new(
|
||||
None,
|
||||
"p4".to_string(),
|
||||
Mode::Regular,
|
||||
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
|
||||
)));
|
||||
p4.borrow_mut().parent = Some(p3.clone());
|
||||
let p5 = Rc::new(RefCell::new(Password::new(None, "p5".to_string(), Mode::Regular, NaiveDate::from_ymd_opt(2022, 12, 3).unwrap())));
|
||||
let p5 = Rc::new(RefCell::new(Password::new(
|
||||
None,
|
||||
"p5".to_string(),
|
||||
Mode::Regular,
|
||||
NaiveDate::from_ymd_opt(2022, 12, 3).unwrap(),
|
||||
)));
|
||||
p5.borrow_mut().parent = Some(p4.clone());
|
||||
|
||||
p1.borrow_mut().parent = Some(p3.clone());
|
||||
|
||||
+117
-57
@@ -1,15 +1,15 @@
|
||||
use home::home_dir;
|
||||
use rustyline::Editor;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use home::home_dir;
|
||||
|
||||
use crate::lk::LK;
|
||||
use crate::structs::{LKErr, Command};
|
||||
use crate::password::fix_password_recursion;
|
||||
use crate::parser::command_parser;
|
||||
use crate::password::fix_password_recursion;
|
||||
use crate::structs::{Command, LKErr};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LKRead {
|
||||
rl: Editor::<()>,
|
||||
rl: Editor<()>,
|
||||
prompt: String,
|
||||
state: Rc<RefCell<LK>>,
|
||||
cmd: String,
|
||||
@@ -29,8 +29,13 @@ pub struct LKPrint {
|
||||
}
|
||||
|
||||
impl LKRead {
|
||||
pub fn new(rl: Editor::<()>, prompt: String, state: Rc<RefCell<LK>>) -> Self {
|
||||
Self { rl, prompt, state, cmd: "".to_string() }
|
||||
pub fn new(rl: Editor<()>, prompt: String, state: Rc<RefCell<LK>>) -> Self {
|
||||
Self {
|
||||
rl,
|
||||
prompt,
|
||||
state,
|
||||
cmd: "".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read(&mut self) -> LKEval {
|
||||
@@ -39,31 +44,40 @@ impl LKRead {
|
||||
self.rl.clear_history();
|
||||
match self.rl.load_history(&history_file) {
|
||||
Ok(_) => (),
|
||||
Err(_) => { self.rl.add_history_entry("ls"); () }
|
||||
Err(_) => {
|
||||
self.rl.add_history_entry("ls");
|
||||
()
|
||||
}
|
||||
}
|
||||
self.cmd = match self.rl.readline(&*self.prompt) {
|
||||
Ok(str) => str,
|
||||
Err(err) => return LKEval::new(Command::Error(LKErr::ReadError(err.to_string())), self.state.clone()),
|
||||
Err(err) => {
|
||||
return LKEval::new(
|
||||
Command::Error(LKErr::ReadError(err.to_string())),
|
||||
self.state.clone(),
|
||||
)
|
||||
}
|
||||
};
|
||||
self.rl.add_history_entry(self.cmd.as_str());
|
||||
match self.rl.save_history(&history_file) { Ok(_) => (), Err(_) => () }
|
||||
match self.rl.save_history(&history_file) {
|
||||
Ok(_) => (),
|
||||
Err(_) => (),
|
||||
}
|
||||
match command_parser::cmd(self.cmd.as_str()) {
|
||||
Ok(cmd) => LKEval::new(cmd, self.state.clone()),
|
||||
Err(err) => LKEval::new(Command::Error(LKErr::ParseError(err)), self.state.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refresh(&mut self) {
|
||||
pub fn refresh(&mut self) {}
|
||||
|
||||
}
|
||||
|
||||
pub fn quit(&mut self) {
|
||||
|
||||
}
|
||||
pub fn quit(&mut self) {}
|
||||
}
|
||||
|
||||
impl<'a> LKEval<'a> {
|
||||
pub fn new(cmd: Command<'a>, state: Rc<RefCell<LK>>) -> Self { Self { cmd, state } }
|
||||
pub fn new(cmd: Command<'a>, state: Rc<RefCell<LK>>) -> Self {
|
||||
Self { cmd, state }
|
||||
}
|
||||
|
||||
pub fn eval(&mut self) -> LKPrint {
|
||||
let mut out: Vec<String> = vec![];
|
||||
@@ -73,29 +87,33 @@ impl<'a> LKEval<'a> {
|
||||
Command::Quit => {
|
||||
out.push("Bye!".to_string());
|
||||
quit = true;
|
||||
},
|
||||
}
|
||||
Command::Ls => {
|
||||
for (_, name) in &self.state.borrow().db {
|
||||
out.push(name.borrow().to_string());
|
||||
}
|
||||
out.sort();
|
||||
},
|
||||
}
|
||||
Command::Add(name) => {
|
||||
if self.state.borrow().db.get(&name.borrow().name).is_some() {
|
||||
out.push("error: password already exist".to_string());
|
||||
} else {
|
||||
self.state.borrow_mut().db.insert(name.borrow().name.clone(), name.clone());
|
||||
self.state
|
||||
.borrow_mut()
|
||||
.db
|
||||
.insert(name.borrow().name.clone(), name.clone());
|
||||
self.state.borrow().fix_hierarchy();
|
||||
}
|
||||
},
|
||||
}
|
||||
Command::Help => {
|
||||
out.push("HELP".to_string());
|
||||
},
|
||||
}
|
||||
Command::Mv(name, folder) => {
|
||||
for (_, tmp) in &self.state.borrow().db {
|
||||
if *tmp.borrow().name == *name {
|
||||
if folder == "/" { tmp.borrow_mut().parent = None }
|
||||
else {
|
||||
if folder == "/" {
|
||||
tmp.borrow_mut().parent = None
|
||||
} else {
|
||||
for (_, fld) in &self.state.borrow().db {
|
||||
if *fld.borrow().name == *folder {
|
||||
tmp.borrow_mut().parent = Some(fld.clone());
|
||||
@@ -107,14 +125,12 @@ impl<'a> LKEval<'a> {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Command::Error(err) => match err {
|
||||
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())),
|
||||
},
|
||||
Command::Error(err) => {
|
||||
match err {
|
||||
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())
|
||||
@@ -122,7 +138,9 @@ impl<'a> LKEval<'a> {
|
||||
}
|
||||
|
||||
impl LKPrint {
|
||||
pub fn new(out: Vec<String>, quit: bool, state: Rc<RefCell<LK>>) -> Self { Self { out, quit, state } }
|
||||
pub fn new(out: Vec<String>, quit: bool, state: Rc<RefCell<LK>>) -> Self {
|
||||
Self { out, quit, state }
|
||||
}
|
||||
|
||||
pub fn print(&mut self) -> bool {
|
||||
for line in &self.out {
|
||||
@@ -135,43 +153,85 @@ impl LKPrint {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
use chrono::naive::NaiveDate;
|
||||
use crate::structs::Mode;
|
||||
use crate::password::Password;
|
||||
use crate::structs::Mode;
|
||||
use chrono::naive::NaiveDate;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn exec_cmds_basic() {
|
||||
let lk = Rc::new(RefCell::new(LK::new()));
|
||||
assert_eq!(LKEval::new(Command::Ls, lk.clone()).eval(), LKPrint::new(vec![], false, lk.clone()));
|
||||
let pwd1 = Rc::new(RefCell::new(Password { name: Rc::new("t1".to_string()),
|
||||
prefix: None, length: None,
|
||||
mode: Mode::Regular, seq: 99,
|
||||
assert_eq!(
|
||||
LKEval::new(Command::Ls, lk.clone()).eval(),
|
||||
LKPrint::new(vec![], false, lk.clone())
|
||||
);
|
||||
let pwd1 = Rc::new(RefCell::new(Password {
|
||||
name: Rc::new("t1".to_string()),
|
||||
prefix: None,
|
||||
length: None,
|
||||
mode: Mode::Regular,
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2022, 12, 30).unwrap(),
|
||||
comment: Some("comment".to_string()),
|
||||
parent: None }));
|
||||
assert_eq!(LKEval::new(Command::Add(pwd1.clone()), lk.clone()).eval().state.borrow().db,
|
||||
{ let mut db = HashMap::new();
|
||||
parent: None,
|
||||
}));
|
||||
assert_eq!(
|
||||
LKEval::new(Command::Add(pwd1.clone()), lk.clone())
|
||||
.eval()
|
||||
.state
|
||||
.borrow()
|
||||
.db,
|
||||
{
|
||||
let mut db = HashMap::new();
|
||||
db.insert(pwd1.borrow().name.clone(), pwd1.clone());
|
||||
db });
|
||||
assert_eq!(LKEval::new(Command::Ls, lk.clone()).eval(),
|
||||
LKPrint::new(vec!["t1 R 99 2022-12-30 comment".to_string()], false, lk.clone()));
|
||||
assert_eq!(LKEval::new(Command::Quit, lk.clone()).eval(),
|
||||
LKPrint::new(vec!["Bye!".to_string()], true, lk.clone()));
|
||||
let pwd2 = Rc::new(RefCell::new(Password { name: Rc::new("t2".to_string()),
|
||||
prefix: None, length: None,
|
||||
mode: Mode::Regular, seq: 99,
|
||||
db
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
LKEval::new(Command::Ls, lk.clone()).eval(),
|
||||
LKPrint::new(
|
||||
vec!["t1 R 99 2022-12-30 comment".to_string()],
|
||||
false,
|
||||
lk.clone()
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
LKEval::new(Command::Quit, lk.clone()).eval(),
|
||||
LKPrint::new(vec!["Bye!".to_string()], true, lk.clone())
|
||||
);
|
||||
let pwd2 = Rc::new(RefCell::new(Password {
|
||||
name: Rc::new("t2".to_string()),
|
||||
prefix: None,
|
||||
length: None,
|
||||
mode: Mode::Regular,
|
||||
seq: 99,
|
||||
date: NaiveDate::from_ymd_opt(2022, 12, 31).unwrap(),
|
||||
comment: Some("bli blup".to_string()),
|
||||
parent: None }));
|
||||
assert_eq!(LKEval::new(Command::Add(pwd2.clone()), lk.clone()).eval().state.borrow().db,
|
||||
{ let mut db = HashMap::new();
|
||||
parent: None,
|
||||
}));
|
||||
assert_eq!(
|
||||
LKEval::new(Command::Add(pwd2.clone()), lk.clone())
|
||||
.eval()
|
||||
.state
|
||||
.borrow()
|
||||
.db,
|
||||
{
|
||||
let mut db = HashMap::new();
|
||||
db.insert(pwd1.borrow().name.clone(), pwd1.clone());
|
||||
db.insert(pwd2.borrow().name.clone(), pwd2.clone());
|
||||
db });
|
||||
assert_eq!(LKEval::new(Command::Ls, lk.clone()).eval(),
|
||||
LKPrint::new(vec!["t1 R 99 2022-12-30 comment".to_string(),
|
||||
"t2 R 99 2022-12-31 bli blup".to_string()],
|
||||
false, lk.clone()));
|
||||
db
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
LKEval::new(Command::Ls, lk.clone()).eval(),
|
||||
LKPrint::new(
|
||||
vec![
|
||||
"t1 R 99 2022-12-30 comment".to_string(),
|
||||
"t2 R 99 2022-12-31 bli blup".to_string()
|
||||
],
|
||||
false,
|
||||
lk.clone()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+9
-4
@@ -1,5 +1,5 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use crate::password::Password;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
#[derive(thiserror::Error, Debug, PartialEq)]
|
||||
pub enum LKErr<'a> {
|
||||
@@ -18,7 +18,7 @@ pub enum Command<'a> {
|
||||
Mv(String, String),
|
||||
Error(LKErr<'a>),
|
||||
Help,
|
||||
Quit
|
||||
Quit,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
@@ -36,7 +36,10 @@ pub enum Mode {
|
||||
|
||||
impl std::fmt::Display for Mode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", match self {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
Mode::Regular => "R",
|
||||
Mode::RegularUpcase => "UR",
|
||||
Mode::NoSpace => "N",
|
||||
@@ -46,6 +49,8 @@ impl std::fmt::Display for Mode {
|
||||
Mode::Base64 => "B",
|
||||
Mode::Base64Upcase => "UB",
|
||||
Mode::Decimal => "D",
|
||||
}.to_string())
|
||||
}
|
||||
.to_string()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user