Browse Source

mps struct begin

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

89
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,20 +68,13 @@ 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 impl MPS {
Ok(String::from(password)) pub fn new() -> MPS {
MPS { storage: None }
} }
fn init() -> io::Result<()> { fn init() -> io::Result<()> {
@ -101,9 +94,27 @@ fn init() -> io::Result<()> {
Ok(()) Ok(())
} }
fn list() -> io:: Result<()> { // TODO: change password functionality
// TODO: set self.storage inited
pub fn login(&mut self) -> io::Result<String> {
// TODO: do nothing if already storate inited
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 list(&mut self) -> io:: Result<()> {
// TODO: get storage from login // TODO: get storage from login
let passphrase = login()?; let passphrase = self.login()?;
// TODO: use self.storage
let st = Storage::from_db(passphrase)?; let st = Storage::from_db(passphrase)?;
for id in st.ids() { for id in st.ids() {
println!("{}", id); println!("{}", id);
@ -111,14 +122,15 @@ fn list() -> io:: Result<()> {
Ok(()) Ok(())
} }
fn add(id: &String) -> io::Result<()> { fn add(&mut self, id: &String) -> io::Result<()> {
let passphrase = login()?; let passphrase = self.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 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)? {
PROMPT::YES => { PROMPT::YES => {
edit(id)?; self.edit(id)?;
return Ok(()); return Ok(());
}, },
PROMPT::NO => return Ok(()), PROMPT::NO => return Ok(()),
@ -132,15 +144,16 @@ fn add(id: &String) -> io::Result<()> {
Ok(()) Ok(())
} }
fn edit(id: &String) -> io::Result<()> { fn edit(&mut self, id: &String) -> io::Result<()> {
// TODO: implement // TODO: implement
let passphrase = login()?; let passphrase = self.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 add it instead?", id);
match get_prompt(&question)? { match get_prompt(&question)? {
PROMPT::YES => { PROMPT::YES => {
add(id)?; self.add(id)?;
return Ok(()); return Ok(());
} }
PROMPT::NO => return Ok(()) PROMPT::NO => return Ok(())
@ -156,9 +169,10 @@ fn edit(id: &String) -> io::Result<()> {
Ok(()) Ok(())
} }
fn show(id: &String) -> io::Result<()> { fn show(&mut self, id: &String) -> io::Result<()> {
// TODO: get storage from login // TODO: get storage from login
let passphrase = login()?; let passphrase = self.login()?;
// TODO: use self.storage
let st = Storage::from_db(passphrase)?; let st = Storage::from_db(passphrase)?;
if !st.contains(id) { if !st.contains(id) {
return Err(io::Error::new( return Err(io::Error::new(
@ -168,39 +182,42 @@ fn show(id: &String) -> io::Result<()> {
} }
let item = st.get(id); let item = st.get(id);
//editor::open_to_show(&item.content)?; editor::open_to_show(&item.content)?;
println!("---------");
println!("{}", item.content);
println!("---------");
Ok(()) 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