Browse Source

MPS struct++

fix-typo
Coin de Gamma 3 months ago
parent
commit
ef2537e204
  1. 44
      src/main.rs

44
src/main.rs

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

Loading…
Cancel
Save