From 5008e7b45c09ede37435d5d934121561f1be8148 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Tue, 17 Sep 2024 09:20:16 +0000 Subject: [PATCH] db -> storage --- src/main.rs | 25 +++++++++++++------------ src/{db.rs => storage.rs} | 20 ++++++++++---------- 2 files changed, 23 insertions(+), 22 deletions(-) rename src/{db.rs => storage.rs} (94%) diff --git a/src/main.rs b/src/main.rs index 9872ed0..5d37230 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ -mod db; +mod storage; mod editor; -use db::{DB, Item}; +use storage::{Storage, Item}; use clap::{Parser, Subcommand}; use rpassword; @@ -74,7 +74,7 @@ fn login() -> io::Result { } fn init() -> io::Result<()> { - if db::DB::is_inited() { + if Storage::is_inited() { return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); } print!("Enter passphrase for storage: "); @@ -89,15 +89,15 @@ fn init() -> io::Result<()> { return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passwords must be equal")); } - db::DB::init(password)?; + Storage::init(password)?; Ok(()) } fn add(id: &String) -> io::Result<()> { // TODO: get login passphrase - let mut db = DB::new(String::from(""))?; - if db.contains(id) { + let mut st = Storage::new(String::from(""))?; + if st.contains(id) { // TODO: ask to edit existing in outer function which invoked this one return Err(io::Error::new( io::ErrorKind::InvalidInput, @@ -106,15 +106,16 @@ fn add(id: &String) -> io::Result<()> { } let content = editor::open_to_edit()?; - db.items.insert(Item::from(id.clone(), content)); - db.dump()?; + st.items.insert(Item::from(id.clone(), content)); + st.dump()?; Ok(()) } fn list() -> io:: Result<()> { - let db = DB::new(String::from(""))?; - let mut vec: Vec<_> = db.items.iter().collect(); + // TODO: ask login + let st = Storage::new(String::from(""))?; + let mut vec: Vec<_> = st.items.iter().collect(); vec.sort(); for item in &vec { println!("{}", item.id); @@ -135,10 +136,10 @@ fn run_command() -> io::Result<()> { list()?; } None => { - if !db::DB::is_inited() { + if !Storage::is_inited() { match get_prompt("Do you want to init your storage?")? { PROMPT::YES => init()?, - PROMPT::NO => db::DB::print_init_hint(), + PROMPT::NO => Storage::print_init_hint(), } } else { login()?; diff --git a/src/db.rs b/src/storage.rs similarity index 94% rename from src/db.rs rename to src/storage.rs index 2f0b2e8..cbfc581 100644 --- a/src/db.rs +++ b/src/storage.rs @@ -68,16 +68,17 @@ impl Ord for Item { } struct Encoder { - password: String + passphrase: String } impl Encoder { - pub fn from(password: String) -> Encoder { - Encoder { password: password } + pub fn from(passphrase: String) -> Encoder { + Encoder { passphrase } } // TODO: get by ref pub fn encode(&self, line: String) -> String { + // TODO: use passphrasee to encode BASE64_STANDARD.encode(line) } @@ -91,14 +92,14 @@ impl Encoder { } } -pub struct DB { +pub struct Storage { pub items: HashSet::, encoder: Encoder } -impl DB { +impl Storage { // TODO: make path as String too - pub fn new(password: String) -> io::Result { + pub fn new(password: String) -> io::Result { let encoder = Encoder::from(password); // TODO: throw error is password is incorrect let file = fs::File::open(&*STORAGE_PATH)?; @@ -121,11 +122,10 @@ impl DB { } } } - let result = DB { + Ok(Storage { items: items, encoder: encoder - }; - Ok(result) + }) } pub fn init(_passphrase: String) -> io::Result<()> { @@ -174,5 +174,5 @@ impl DB { Ok(()) } - } +}