Move LK structure into own file.
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use regex::{Regex, Captures};
|
||||||
|
use crate::structs::Password;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
pub struct LK {
|
||||||
|
pub db: HashMap<Rc<String>, Rc<RefCell<Password>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LK {
|
||||||
|
pub fn fix_hierarchy(&self) {
|
||||||
|
lazy_static! {
|
||||||
|
static ref RE: Regex = Regex::new(r"\s*\^([!-~]+)").unwrap();
|
||||||
|
}
|
||||||
|
for (_, name) in &self.db {
|
||||||
|
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()); "" });
|
||||||
|
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()) }
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-1
@@ -4,13 +4,14 @@ extern crate lazy_static;
|
|||||||
mod structs;
|
mod structs;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod repl;
|
mod repl;
|
||||||
|
mod lk;
|
||||||
|
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use rustyline::Editor;
|
use rustyline::Editor;
|
||||||
|
|
||||||
use crate::structs::LK;
|
use crate::lk::LK;
|
||||||
use crate::repl::LKRead;
|
use crate::repl::LKRead;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
|||||||
+2
-1
@@ -2,7 +2,8 @@ use rustyline::Editor;
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use home::home_dir;
|
use home::home_dir;
|
||||||
|
|
||||||
use crate::structs::{LKErr, Command, LK};
|
use crate::lk::LK;
|
||||||
|
use crate::structs::{LKErr, Command};
|
||||||
use crate::parser::command_parser;
|
use crate::parser::command_parser;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use std::collections::HashMap;
|
|
||||||
use regex::{Regex, Captures};
|
|
||||||
use chrono::naive::NaiveDate;
|
use chrono::naive::NaiveDate;
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug, PartialEq)]
|
#[derive(thiserror::Error, Debug, PartialEq)]
|
||||||
@@ -48,11 +46,6 @@ pub enum Command<'a> {
|
|||||||
Quit
|
Quit
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
|
||||||
pub struct LK {
|
|
||||||
pub db: HashMap<Rc<String>, Rc<RefCell<Password>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
@@ -78,32 +71,3 @@ impl std::string::ToString for Password {
|
|||||||
format!("{}{} {}{} {} {}{}{}", prefix, self.name, length, self.mode, self.seq, self.date, comment, parent)
|
format!("{}{} {}{} {} {}{}{}", prefix, self.name, length, self.mode, self.seq, self.date, comment, parent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LK {
|
|
||||||
pub fn fix_hierarchy(&self) {
|
|
||||||
lazy_static! {
|
|
||||||
static ref RE: Regex = Regex::new(r"\s*\^([!-~]+)").unwrap();
|
|
||||||
}
|
|
||||||
for (_, name) in &self.db {
|
|
||||||
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()); "" });
|
|
||||||
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()) }
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user