Browse Source

db -> storage

fix-typo
Coin de Gamma 3 months ago
parent
commit
5008e7b45c
  1. 25
      src/main.rs
  2. 18
      src/storage.rs

25
src/main.rs

@ -1,8 +1,8 @@
mod db; mod storage;
mod editor; mod editor;
use db::{DB, Item}; use storage::{Storage, Item};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use rpassword; use rpassword;
@ -74,7 +74,7 @@ fn login() -> io::Result<String> {
} }
fn init() -> 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")); return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted"));
} }
print!("Enter passphrase for storage: "); 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")); return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passwords must be equal"));
} }
db::DB::init(password)?; Storage::init(password)?;
Ok(()) Ok(())
} }
fn add(id: &String) -> io::Result<()> { fn add(id: &String) -> io::Result<()> {
// TODO: get login passphrase // TODO: get login passphrase
let mut db = DB::new(String::from(""))?; let mut st = Storage::new(String::from(""))?;
if db.contains(id) { if st.contains(id) {
// TODO: ask to edit existing in outer function which invoked this one // TODO: ask to edit existing in outer function which invoked this one
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::InvalidInput, io::ErrorKind::InvalidInput,
@ -106,15 +106,16 @@ fn add(id: &String) -> io::Result<()> {
} }
let content = editor::open_to_edit()?; let content = editor::open_to_edit()?;
db.items.insert(Item::from(id.clone(), content)); st.items.insert(Item::from(id.clone(), content));
db.dump()?; st.dump()?;
Ok(()) Ok(())
} }
fn list() -> io:: Result<()> { fn list() -> io:: Result<()> {
let db = DB::new(String::from(""))?; // TODO: ask login
let mut vec: Vec<_> = db.items.iter().collect(); let st = Storage::new(String::from(""))?;
let mut vec: Vec<_> = st.items.iter().collect();
vec.sort(); vec.sort();
for item in &vec { for item in &vec {
println!("{}", item.id); println!("{}", item.id);
@ -135,10 +136,10 @@ fn run_command() -> io::Result<()> {
list()?; list()?;
} }
None => { None => {
if !db::DB::is_inited() { if !Storage::is_inited() {
match get_prompt("Do you want to init your storage?")? { match get_prompt("Do you want to init your storage?")? {
PROMPT::YES => init()?, PROMPT::YES => init()?,
PROMPT::NO => db::DB::print_init_hint(), PROMPT::NO => Storage::print_init_hint(),
} }
} else { } else {
login()?; login()?;

18
src/db.rs → src/storage.rs

@ -68,16 +68,17 @@ impl Ord for Item {
} }
struct Encoder { struct Encoder {
password: String passphrase: String
} }
impl Encoder { impl Encoder {
pub fn from(password: String) -> Encoder { pub fn from(passphrase: String) -> Encoder {
Encoder { password: password } Encoder { passphrase }
} }
// TODO: get by ref // TODO: get by ref
pub fn encode(&self, line: String) -> String { pub fn encode(&self, line: String) -> String {
// TODO: use passphrasee to encode
BASE64_STANDARD.encode(line) BASE64_STANDARD.encode(line)
} }
@ -91,14 +92,14 @@ impl Encoder {
} }
} }
pub struct DB { pub struct Storage {
pub items: HashSet::<Item>, pub items: HashSet::<Item>,
encoder: Encoder encoder: Encoder
} }
impl DB { impl Storage {
// TODO: make path as String too // TODO: make path as String too
pub fn new(password: String) -> io::Result<DB> { pub fn new(password: String) -> io::Result<Storage> {
let encoder = Encoder::from(password); let encoder = Encoder::from(password);
// TODO: throw error is password is incorrect // TODO: throw error is password is incorrect
let file = fs::File::open(&*STORAGE_PATH)?; let file = fs::File::open(&*STORAGE_PATH)?;
@ -121,11 +122,10 @@ impl DB {
} }
} }
} }
let result = DB { Ok(Storage {
items: items, items: items,
encoder: encoder encoder: encoder
}; })
Ok(result)
} }
pub fn init(_passphrase: String) -> io::Result<()> { pub fn init(_passphrase: String) -> io::Result<()> {
Loading…
Cancel
Save