Move Password to password.rs file
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use std::collections::HashMap;
|
||||
use regex::{Regex, Captures};
|
||||
use crate::structs::{Password, fix_password_recursion};
|
||||
use crate::password::{Password, fix_password_recursion};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct LK {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
extern crate lazy_static;
|
||||
|
||||
mod structs;
|
||||
mod password;
|
||||
mod parser;
|
||||
mod repl;
|
||||
mod lk;
|
||||
|
||||
+2
-1
@@ -2,7 +2,8 @@ extern crate peg;
|
||||
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use chrono::naive::NaiveDate;
|
||||
use crate::structs::{Mode, Command, Password, LKErr};
|
||||
use crate::structs::{Command, LKErr, Mode};
|
||||
use crate::password::Password;
|
||||
|
||||
peg::parser!{
|
||||
pub grammar command_parser() for str {
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use chrono::naive::NaiveDate;
|
||||
use crate::structs::Mode;
|
||||
|
||||
#[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 mode: Mode,
|
||||
pub seq: u32,
|
||||
pub date: NaiveDate,
|
||||
pub comment: Option<String>,
|
||||
}
|
||||
|
||||
impl Password {
|
||||
pub fn new(prefix: Option<String>, name: String, mode: Mode, date: NaiveDate) -> Password {
|
||||
Password { prefix, mode, date,
|
||||
parent: None,
|
||||
name: Rc::new(name),
|
||||
length: None,
|
||||
seq: 99,
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fix_password_recursion(entry: Rc<RefCell<Password>>) {
|
||||
let mut t1 = entry.clone();
|
||||
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).as_ptr(), (*t2).as_ptr()) { t3 = Some(t1.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).as_ptr(), (*t2).as_ptr()) { t3 = Some(t1.clone()); break; }
|
||||
}
|
||||
match t3 {
|
||||
Some(o) => o.borrow_mut().parent = None,
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[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())));
|
||||
|
||||
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())));
|
||||
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())));
|
||||
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())));
|
||||
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())));
|
||||
p5.borrow_mut().parent = Some(p4.clone());
|
||||
|
||||
p1.borrow_mut().parent = Some(p3.clone());
|
||||
fix_password_recursion(p5.clone());
|
||||
assert_eq!(p3.borrow().parent, None);
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -3,7 +3,8 @@ use std::{cell::RefCell, rc::Rc};
|
||||
use home::home_dir;
|
||||
|
||||
use crate::lk::LK;
|
||||
use crate::structs::{LKErr, Command, fix_password_recursion};
|
||||
use crate::structs::{LKErr, Command};
|
||||
use crate::password::fix_password_recursion;
|
||||
use crate::parser::command_parser;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -136,7 +137,8 @@ mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
use chrono::naive::NaiveDate;
|
||||
use crate::structs::*;
|
||||
use crate::structs::Mode;
|
||||
use crate::password::Password;
|
||||
|
||||
#[test]
|
||||
fn exec_cmds_basic() {
|
||||
|
||||
+11
-88
@@ -1,5 +1,5 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use chrono::naive::NaiveDate;
|
||||
use crate::password::Password;
|
||||
|
||||
#[derive(thiserror::Error, Debug, PartialEq)]
|
||||
pub enum LKErr<'a> {
|
||||
@@ -11,6 +11,16 @@ pub enum LKErr<'a> {
|
||||
ParseError(peg::error::ParseError<peg::str::LineCol>),
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Command<'a> {
|
||||
Add(Rc<RefCell<Password>>),
|
||||
Ls,
|
||||
Mv(String, String),
|
||||
Error(LKErr<'a>),
|
||||
Help,
|
||||
Quit
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Mode {
|
||||
Regular,
|
||||
@@ -24,28 +34,6 @@ pub enum Mode {
|
||||
Decimal,
|
||||
}
|
||||
|
||||
#[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 mode: Mode,
|
||||
pub seq: u32,
|
||||
pub date: NaiveDate,
|
||||
pub comment: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Command<'a> {
|
||||
Add(Rc<RefCell<Password>>),
|
||||
Ls,
|
||||
Mv(String, String),
|
||||
Error(LKErr<'a>),
|
||||
Help,
|
||||
Quit
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Mode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", match self {
|
||||
@@ -61,68 +49,3 @@ impl std::fmt::Display for Mode {
|
||||
}.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl Password {
|
||||
pub fn new(prefix: Option<String>, name: String, mode: Mode, date: NaiveDate) -> Password {
|
||||
Password { prefix, mode, date,
|
||||
parent: None,
|
||||
name: Rc::new(name),
|
||||
length: None,
|
||||
seq: 99,
|
||||
comment: None }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fix_password_recursion(entry: Rc<RefCell<Password>>) {
|
||||
let mut t1 = entry.clone();
|
||||
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).as_ptr(), (*t2).as_ptr()) { t3 = Some(t1.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).as_ptr(), (*t2).as_ptr()) { t3 = Some(t1.clone()); break; }
|
||||
}
|
||||
match t3 {
|
||||
Some(o) => o.borrow_mut().parent = None,
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[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())));
|
||||
|
||||
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())));
|
||||
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())));
|
||||
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())));
|
||||
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())));
|
||||
p5.borrow_mut().parent = Some(p4.clone());
|
||||
|
||||
p1.borrow_mut().parent = Some(p3.clone());
|
||||
fix_password_recursion(p5.clone());
|
||||
assert_eq!(p3.borrow().parent, None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user