Move LK structure into own file.

This commit is contained in:
Kiyomichi Kosaka
2022-12-03 11:43:48 +00:00
parent 0818d93f9a
commit 50a3398ad9
4 changed files with 42 additions and 38 deletions
+38
View File
@@ -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
View File
@@ -4,13 +4,14 @@ extern crate lazy_static;
mod structs;
mod parser;
mod repl;
mod lk;
use std::{cell::RefCell, rc::Rc};
use std::collections::HashMap;
use rustyline::Editor;
use crate::structs::LK;
use crate::lk::LK;
use crate::repl::LKRead;
pub fn main() {
+2 -1
View File
@@ -2,7 +2,8 @@ use rustyline::Editor;
use std::{cell::RefCell, rc::Rc};
use home::home_dir;
use crate::structs::{LKErr, Command, LK};
use crate::lk::LK;
use crate::structs::{LKErr, Command};
use crate::parser::command_parser;
#[derive(Debug)]
-36
View File
@@ -1,6 +1,4 @@
use std::{cell::RefCell, rc::Rc};
use std::collections::HashMap;
use regex::{Regex, Captures};
use chrono::naive::NaiveDate;
#[derive(thiserror::Error, Debug, PartialEq)]
@@ -48,11 +46,6 @@ pub enum Command<'a> {
Quit
}
#[derive(PartialEq, Debug)]
pub struct LK {
pub db: HashMap<Rc<String>, Rc<RefCell<Password>>>,
}
impl std::fmt::Display for Mode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
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)
}
}
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;
}
}
}
}
}
}
}