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