-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from opeolluwa/store
Store
- Loading branch information
Showing
5 changed files
with
89 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,61 @@ | ||
use clap::{Args, Subcommand}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{database::Database, style::PrintColoredText}; | ||
|
||
// utils store k v | ||
#[derive(Args, Debug, Serialize)] | ||
pub struct StoreCommands { | ||
/// a unique key | ||
#[clap(short, long, value_parser)] | ||
pub key: String, | ||
///' value | ||
#[clap(short, long, value_parser)] | ||
pub value: String, | ||
/// sub commands | ||
#[command(subcommand)] | ||
pub subcommands: Option<StoreSubCommand>, | ||
pub subcommands: StoreSubCommand, | ||
} | ||
|
||
#[derive(Debug, Subcommand, Serialize, Deserialize, Clone)] | ||
pub enum StoreSubCommand { | ||
Set, | ||
Add, | ||
Replace, | ||
Remove, | ||
/// update value | ||
Set { key: String, value: String }, | ||
/// save new value | ||
Add { | ||
/// a unique key | ||
#[clap(short, long, value_parser)] | ||
key: String, | ||
///' value | ||
#[clap(short, long, value_parser)] | ||
value: String, | ||
}, | ||
/// remove value | ||
Remove { key: String }, | ||
} | ||
|
||
impl StoreCommands { | ||
pub fn parse(&self) { | ||
match &self.subcommands { | ||
Some(StoreSubCommand::Set) => println!("set"), | ||
_ => println!("default"), | ||
// Some() | ||
StoreSubCommand::Set { key, value } => println!("set {} {}", key, value), | ||
StoreSubCommand::Remove { key } => println!("remove"), | ||
StoreSubCommand::Add { key, value } => println!("add"), | ||
_ => PrintColoredText::warning("invalid input"), | ||
} | ||
println!("{:?}", self) | ||
; } | ||
} | ||
|
||
/*store the key value pair in the database after checking that the key does not exist, if the key exist prompt use to overwrite */ | ||
fn add(key: &String, value: &String) { | ||
let conn = Database::conn(); | ||
let query = format!("SELECT * FROM store WHERE key = '{}'", key); | ||
let mut stmt = conn.prepare(&query).unwrap(); | ||
// let mut rows = stmt.query([]).unwrap(); | ||
} | ||
/* accept a key and update the value of the key */ | ||
fn set(key: &String, value: &String) { | ||
let conn = Database::conn(); | ||
let query = format!("SELECT * FROM store WHERE key = '{}'", key); | ||
let mut stmt = conn.prepare(&query).unwrap(); | ||
} | ||
|
||
fn remove(key: &String) { | ||
let conn = Database::conn(); | ||
let query = format!("SELECT * FROM store WHERE key = '{}'", key); | ||
let mut stmt = conn.prepare(&query).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters