Browse Source

ecoder/password begin

db-refactoring
Coin de Gamma 3 months ago
parent
commit
966a0797dc
  1. 41
      src/db.rs
  2. 11
      src/main.rs

41
src/db.rs

@ -58,21 +58,40 @@ impl Ord for Item {
} }
} }
fn decode(line: String) -> Result<String, io::Error> { struct Encoder {
let content = BASE64_STANDARD.decode(line).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; password: String
match String::from_utf8(content) { }
Ok(s) => Ok(s),
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)) impl Encoder {
pub fn from(password: String) -> Encoder {
Encoder { password: password }
}
// TODO: get by ref
pub fn encode(&self, line: String) -> String {
BASE64_STANDARD.encode(line)
}
// TODO: review error type
pub fn decode(&self, line: String) -> io::Result<String> {
let content = BASE64_STANDARD.decode(line).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
match String::from_utf8(content) {
Ok(s) => Ok(s),
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e))
}
} }
} }
pub struct DB { pub struct DB {
pub items: HashSet::<Item>, pub items: HashSet::<Item>,
path: String path: String,
encoder: Encoder
} }
impl DB { impl DB {
pub fn new(path: &str) -> io::Result<DB> { pub fn new(path: &str, password: String) -> io::Result<DB> {
let encoder = Encoder::from(password);
// TODO: throw error is password is incorrect
let file = fs::File::open(path)?; let file = fs::File::open(path)?;
let reader = io::BufReader::new(file); let reader = io::BufReader::new(file);
let mut items = HashSet::<Item>::new(); let mut items = HashSet::<Item>::new();
@ -80,11 +99,10 @@ impl DB {
for line in reader.lines() { for line in reader.lines() {
match line { match line {
Ok(line) => { Ok(line) => {
// TODO: check content
if id.is_none() { if id.is_none() {
id = Some(line); id = Some(line);
} else { } else {
let content = decode(line)?; let content = encoder.decode(line)?;
items.insert(Item::from(id.unwrap(), content)); items.insert(Item::from(id.unwrap(), content));
id = None; id = None;
} }
@ -96,7 +114,8 @@ impl DB {
} }
let result = DB { let result = DB {
items: items, items: items,
path: String::from(path) path: String::from(path),
encoder: encoder
}; };
Ok(result) Ok(result)
} }
@ -114,7 +133,7 @@ impl DB {
for item in self.items.iter() { for item in self.items.iter() {
writeln!(file, "{}", item.id)?; writeln!(file, "{}", item.id)?;
let content = BASE64_STANDARD.encode(item.content.clone()); let content = self.encoder.encode(item.content.clone());
writeln!(file, "{}", content)?; writeln!(file, "{}", content)?;
} }

11
src/main.rs

@ -42,6 +42,9 @@ enum Commands {
/// Lists all ids stored in db /// Lists all ids stored in db
List List
// TODO: show
// TODO: edit
} }
@ -51,6 +54,7 @@ fn is_inited() -> bool {
} }
fn init() -> io::Result<()> { fn init() -> io::Result<()> {
// TODO: ask password two times
if is_inited() { if is_inited() {
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted"));
} }
@ -67,7 +71,7 @@ fn init() -> io::Result<()> {
} }
fn add(id: &String) -> io::Result<()> { fn add(id: &String) -> io::Result<()> {
let mut db = DB::new(&*STORAGE_PATH)?; let mut db = DB::new(&*STORAGE_PATH, String::from(""))?;
if db.contains(id) { if db.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(
@ -75,8 +79,7 @@ fn add(id: &String) -> io::Result<()> {
format!("Dublicate item id: {}. Item id's must be unique.", id) format!("Dublicate item id: {}. Item id's must be unique.", id)
)) ))
} }
// TODO: implement encryption
let content = editor::open_to_edit()?; let content = editor::open_to_edit()?;
db.items.insert(Item::from(id.clone(), content)); db.items.insert(Item::from(id.clone(), content));
db.dump()?; db.dump()?;
@ -85,7 +88,7 @@ fn add(id: &String) -> io::Result<()> {
} }
fn list() -> io:: Result<()> { fn list() -> io:: Result<()> {
let db = DB::new(&STORAGE_PATH)?; let db = DB::new(&STORAGE_PATH, String::from(""))?;
let mut vec: Vec<_> = db.items.iter().collect(); let mut vec: Vec<_> = db.items.iter().collect();
vec.sort(); vec.sort();
for item in &vec { for item in &vec {

Loading…
Cancel
Save