Browse Source

mps struct begin

fix-typo
Coin de Gamma 3 months ago
parent
commit
8eb62289a1
  1. 209
      src/main.rs

209
src/main.rs

@ -27,13 +27,13 @@ enum Commands {
/// Lists all ids stored in db /// Lists all ids stored in db
List, List,
/// Show content of item /// Show content of an item
Show { Show {
#[arg(value_name="item_id")] #[arg(value_name="item_id")]
id: String id: String
}, },
/// Adds new item with unique id to the db /// Adds new item with unique id to the storage
Add { Add {
#[arg(value_name="item_id")] #[arg(value_name="item_id")]
id: String id: String
@ -68,139 +68,156 @@ fn get_prompt(question: &str) -> io::Result<PROMPT> {
} }
// TODO: change password functionality struct MPS {
// TODO: return storage inited storage: Option<Storage>
fn login() -> io::Result<String> {
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" {
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))
} }
fn init() -> io::Result<()> { impl MPS {
if Storage::is_inited() { pub fn new() -> MPS {
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); MPS { storage: None }
} }
print!("Enter passphrase for storage: ");
io::stdout().flush()?;
let ps = rpassword::read_password()?;
print!("Reenter passphrase: ");
io::stdout().flush()?;
let ps2 = rpassword::read_password()?;
if ps != ps2 {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passphrases must be equal"));
}
Storage::init(ps)?;
Ok(())
}
fn list() -> io:: Result<()> { fn init() -> io::Result<()> {
// TODO: get storage from login if Storage::is_inited() {
let passphrase = login()?; return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted"));
let st = Storage::from_db(passphrase)?; }
for id in st.ids() { print!("Enter passphrase for storage: ");
println!("{}", id); io::stdout().flush()?;
let ps = rpassword::read_password()?;
print!("Reenter passphrase: ");
io::stdout().flush()?;
let ps2 = rpassword::read_password()?;
if ps != ps2 {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passphrases must be equal"));
}
Storage::init(ps)?;
Ok(())
} }
Ok(())
}
fn add(id: &String) -> io::Result<()> { // TODO: change password functionality
let passphrase = login()?; // TODO: set self.storage inited
let mut st = Storage::from_db(passphrase)?; pub fn login(&mut self) -> io::Result<String> {
if st.contains(id) { // TODO: do nothing if already storate inited
let question = format!("Item [{}] exist. Do you want to edit it instead?", id); print!("Enter passphrase for storage: ");
match get_prompt(&question)? { io::stdout().flush()?;
PROMPT::YES => { // TODO: check in db
edit(id)?; // TODO: return error if db is not inited
return Ok(()); let password = rpassword::read_password()?;
}, if password != "pass" {
PROMPT::NO => return Ok(()), 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))
} }
// set empty string because there is no content yet
let content = editor::open_to_edit(&String::from(""))?;
st.add(Item::from(id.clone(), content));
st.dump()?;
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)?;
for id in st.ids() {
println!("{}", id);
}
Ok(())
}
fn edit(id: &String) -> io::Result<()> { fn add(&mut self, id: &String) -> io::Result<()> {
// TODO: implement let passphrase = self.login()?;
let passphrase = login()?; // TODO: use self.storage
let mut st = Storage::from_db(passphrase)?; 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 edit it instead?", id);
match get_prompt(&question)? { match get_prompt(&question)? {
PROMPT::YES => { PROMPT::YES => {
add(id)?; self.edit(id)?;
return Ok(()); return Ok(());
},
PROMPT::NO => return Ok(()),
} }
PROMPT::NO => return Ok(())
} }
// set empty string because there is no content yet
let content = editor::open_to_edit(&String::from(""))?;
st.add(Item::from(id.clone(), content));
st.dump()?;
Ok(())
} }
let mut item = (*st.get(id)).clone(); fn edit(&mut self, id: &String) -> io::Result<()> {
let new_content = editor::open_to_edit(&item.content)?; // TODO: implement
item.content = new_content; let passphrase = self.login()?;
st.update(item); // TODO: use self.storage
st.dump()?; let mut st = Storage::from_db(passphrase)?;
if !st.contains(id) {
let question = format!("Item [{}] exist. Do you want to add it instead?", id);
match get_prompt(&question)? {
PROMPT::YES => {
self.add(id)?;
return Ok(());
}
PROMPT::NO => return Ok(())
}
}
Ok(()) let mut item = (*st.get(id)).clone();
} let new_content = editor::open_to_edit(&item.content)?;
item.content = new_content;
st.update(item);
st.dump()?;
fn show(id: &String) -> io::Result<()> { Ok(())
// TODO: get storage from login
let passphrase = login()?;
let st = Storage::from_db(passphrase)?;
if !st.contains(id) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Can`t find id: {}", id)
));
} }
let item = st.get(id); fn show(&mut self, id: &String) -> io::Result<()> {
//editor::open_to_show(&item.content)?; // TODO: get storage from login
println!("---------"); let passphrase = self.login()?;
println!("{}", item.content); // TODO: use self.storage
println!("---------"); let st = Storage::from_db(passphrase)?;
Ok(()) if !st.contains(id) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Can`t find id: {}", id)
));
}
let item = st.get(id);
editor::open_to_show(&item.content)?;
Ok(())
}
} }
fn run_command() -> io::Result<()> { fn run_command() -> io::Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
match &cli.command { match &cli.command {
Some(Commands::Init) => { Some(Commands::Init) => {
init()?; MPS::init()?;
} }
Some(Commands::List) => { Some(Commands::List) => {
list()?; let mut mps = MPS::new();
mps.list()?;
} }
Some(Commands::Show { id }) => { Some(Commands::Show { id }) => {
show(id)?; let mut mps = MPS::new();
mps.show(id)?;
} }
Some(Commands::Add { id }) => { Some(Commands::Add { id }) => {
add(id)?; let mut mps = MPS::new();
mps.add(id)?;
} }
Some(Commands::Edit { id }) => { Some(Commands::Edit { id }) => {
edit(id)? let mut mps = MPS::new();
mps.edit(id)?
} }
None => { None => {
if !Storage::is_inited() { if !Storage::is_inited() {
match get_prompt("Do you want to init your storage?")? { match get_prompt("Do you want to init your storage?")? {
PROMPT::YES => init()?, PROMPT::YES => MPS::init()?,
PROMPT::NO => Storage::print_init_hint(), PROMPT::NO => Storage::print_init_hint(),
} }
} else { } else {
list()? let mut mps = MPS::new();
mps.list()?
} }
} }
} }

Loading…
Cancel
Save