diff --git a/src/main.rs b/src/main.rs index 28dcf8a..e251079 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,28 +94,26 @@ impl MPS { Ok(()) } - // TODO: change password functionality - // TODO: set self.storage inited - pub fn login(&mut self) -> io::Result { - // TODO: do nothing if already storate inited + pub fn login(&mut self) -> io::Result<()> { + if self.storage.is_some() { + return Ok(()); + } print!("Enter passphrase for storage: "); io::stdout().flush()?; // TODO: check in db - // TODO: return error if db is not inited - let password = rpassword::read_password()?; - if password != "pass" { + let passphrase = rpassword::read_password()?; + print!("\x1B[1A\x1B[2K"); // Move cursor up one line and clear that line + io::stdout().flush()?; + if passphrase != "pass" { return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase")); } - print!("\x1B[1A\x1B[2K"); // Move cursor up one line and clear that line - io::stdout().flush()?; // Ensure the changes are reflected immediately - Ok(String::from(password)) + self.storage = Some(Storage::from_db(passphrase)?); + Ok(()) } fn list(&mut self) -> io:: Result<()> { - // TODO: get storage from login - let passphrase = self.login()?; - // TODO: use self.storage - let st = Storage::from_db(passphrase)?; + self.login()?; + let st = self.storage.as_ref().unwrap(); for id in st.ids() { println!("{}", id); } @@ -123,9 +121,8 @@ impl MPS { } fn add(&mut self, id: &String) -> io::Result<()> { - let passphrase = self.login()?; - // TODO: use self.storage - let mut st = Storage::from_db(passphrase)?; + self.login()?; + let st = self.storage.as_mut().unwrap(); if st.contains(id) { let question = format!("Item [{}] exist. Do you want to edit it instead?", id); match get_prompt(&question)? { @@ -140,15 +137,12 @@ impl MPS { let content = editor::open_to_edit(&String::from(""))?; st.add(Item::from(id.clone(), content)); st.dump()?; - Ok(()) } fn edit(&mut self, id: &String) -> io::Result<()> { - // TODO: implement - let passphrase = self.login()?; - // TODO: use self.storage - let mut st = Storage::from_db(passphrase)?; + self.login()?; + let mut st = self.storage.as_mut().unwrap(); if !st.contains(id) { let question = format!("Item [{}] exist. Do you want to add it instead?", id); match get_prompt(&question)? { @@ -170,10 +164,8 @@ impl MPS { } fn show(&mut self, id: &String) -> io::Result<()> { - // TODO: get storage from login - let passphrase = self.login()?; - // TODO: use self.storage - let st = Storage::from_db(passphrase)?; + self.login()?; + let st = self.storage.as_ref().unwrap(); if !st.contains(id) { return Err(io::Error::new( io::ErrorKind::InvalidInput,