Browse Source

db -> storage

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

25
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<String> {
}
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()?;

20
src/db.rs → 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::<Item>,
encoder: Encoder
}
impl DB {
impl Storage {
// 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);
// 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(())
}
}
}
Loading…
Cancel
Save