diff --git a/src/db.rs b/src/db.rs index 64242cb..4df284e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -58,21 +58,40 @@ impl Ord for Item { } } -fn decode(line: String) -> Result { - 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)) +struct Encoder { + password: String +} + +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 { + 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 items: HashSet::, - path: String + path: String, + encoder: Encoder } impl DB { - pub fn new(path: &str) -> io::Result { + pub fn new(path: &str, password: String) -> io::Result { + let encoder = Encoder::from(password); + // TODO: throw error is password is incorrect let file = fs::File::open(path)?; let reader = io::BufReader::new(file); let mut items = HashSet::::new(); @@ -80,11 +99,10 @@ impl DB { for line in reader.lines() { match line { Ok(line) => { - // TODO: check content if id.is_none() { id = Some(line); } else { - let content = decode(line)?; + let content = encoder.decode(line)?; items.insert(Item::from(id.unwrap(), content)); id = None; } @@ -96,7 +114,8 @@ impl DB { } let result = DB { items: items, - path: String::from(path) + path: String::from(path), + encoder: encoder }; Ok(result) } @@ -114,7 +133,7 @@ impl DB { for item in self.items.iter() { writeln!(file, "{}", item.id)?; - let content = BASE64_STANDARD.encode(item.content.clone()); + let content = self.encoder.encode(item.content.clone()); writeln!(file, "{}", content)?; } diff --git a/src/main.rs b/src/main.rs index 7aacfe0..d3caf8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,9 @@ enum Commands { /// Lists all ids stored in db List + // TODO: show + // TODO: edit + } @@ -51,6 +54,7 @@ fn is_inited() -> bool { } fn init() -> io::Result<()> { + // TODO: ask password two times if is_inited() { return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); } @@ -67,7 +71,7 @@ fn init() -> 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) { // TODO: ask to edit existing in outer function which invoked this one 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) )) } - - // TODO: implement encryption + let content = editor::open_to_edit()?; db.items.insert(Item::from(id.clone(), content)); db.dump()?; @@ -85,7 +88,7 @@ fn add(id: &String) -> 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(); vec.sort(); for item in &vec {