diff --git a/Cargo.lock b/Cargo.lock index 777773a..23a22b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "adana" -version = "0.13.20" +version = "0.13.21" dependencies = [ "anyhow", "bincode", @@ -552,18 +552,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.154" +version = "1.0.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cdd151213925e7f1ab45a9bbfb129316bd00799784b174b7cc7bcd16961c49e" +checksum = "71f2b4817415c6d4210bfe1c7bfcf4801b2d904cb4d0e1a8fdb651013c9e86b8" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.154" +version = "1.0.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fc80d722935453bcafdc2c9a73cd6fac4dc1938f0346035d84bf99fa9e33217" +checksum = "d071a94a3fac4aff69d023a7f411e33f40f3483f8c5190b1953822b6b76d7630" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 362c532..932f7fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "adana" -version = "0.13.20" +version = "0.13.21" edition = "2021" authors = ["Nordine Bittich"] license = "MIT" @@ -18,7 +18,7 @@ nom = { version = "7.1.3"} nu-ansi-term = "0.46.0" rustyline = "11.0.0" rustyline-derive = "0.8.0" -serde = {version = "1.0.154", features= ['serde_derive', 'rc']} +serde = {version = "1.0.155", features= ['serde_derive', 'rc']} serde_json = "1.0.94" slab_tree = "0.3.2" strum = { version = "0.24.1", features = ["derive"] } diff --git a/README.md b/README.md index 40f7a99..efe346c 100644 --- a/README.md +++ b/README.md @@ -690,6 +690,7 @@ There is no possible interaction with the scripting language yet. | name | alt | description | | ---------------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | put | N/A | Put a new value to current namespace. can have multiple aliases with option '-a'. e.g `put -a drc -a drcomp docker-compose` | +| alias | N/A | Alias a key with another. e.g alias commit gc | | describe | ds | List values within the current namespace. | | listns | lsns | List available namespaces. | | currentns | currentns | Print current namespace. | diff --git a/src/cache_command/mod.rs b/src/cache_command/mod.rs index 0579fc0..4ee016d 100644 --- a/src/cache_command/mod.rs +++ b/src/cache_command/mod.rs @@ -8,6 +8,7 @@ use strum::EnumCount; pub mod constants { pub const PUT: &str = "put"; + pub const ALIAS: &str = "alias"; pub const GET: &str = "get"; pub const DESCRIBE: &str = "describe"; pub const DESCRIBE_ALT: &str = "ds"; @@ -51,6 +52,7 @@ pub enum CacheCommand<'a> { Restore, DeleteCache(Option<&'a str>), Merge(&'a str), + Alias((&'a str, &'a str)), Del(&'a str), Get(&'a str), Exec { key: &'a str, args: Option<&'a str> }, @@ -66,7 +68,7 @@ pub enum CacheCommand<'a> { impl CacheCommand<'_> { pub const fn doc() -> &'static [(&'static [&'static str], &'static str)] { - if CacheCommand::COUNT != 20 { + if CacheCommand::COUNT != 21 { panic!("CacheCommand::doc() no longer valid!"); } &[ @@ -77,6 +79,7 @@ impl CacheCommand<'_> { (&[BACKUP, BACKUP_ALT], "Backup the database of namespaces to the current directory"), (&[FLUSH], "Flush to database."), (&[RESTORE], "Restore the database from current directory"), + (&[ALIAS], "Alias a key with another. e.g alias commit gc"), (&[DEL_CACHE,DEL_CACHE_ALT], "Delete namespace or clear current namespace values."), (&[MERGE_CACHE,MERGE_CACHE_ALT], "Merge current with a given namespace"), (&[DEL,DEL_ALT], "Remove value from namespace. Accept either a hashkey or an alias. e.g `del drc`"), diff --git a/src/cache_command/parser.rs b/src/cache_command/parser.rs index 3e293c2..6229b8a 100644 --- a/src/cache_command/parser.rs +++ b/src/cache_command/parser.rs @@ -42,6 +42,13 @@ fn get_command(command: &str) -> Res { map(extract_key(tag_no_case(GET)), CacheCommand::Get)(command) } +fn alias_command(command: &str) -> Res { + map( + pair(extract_key(tag_no_case(ALIAS)), extract_command), + |(left, right)| CacheCommand::Alias((left, right)), + )(command) +} + fn concat_command(command: &str) -> Res { map( alt(( @@ -187,23 +194,19 @@ fn extract_key<'a, F>(parser: F) -> impl Fn(&'a str) -> Res<&'a str> where F: Fn(&'a str) -> Res<&'a str>, { - move |s: &str| { - preceded( - &parser, - preceded( - multispace1, - take_while1(|s: char| { - s.is_alphanumeric() || s == '-' || s == '_' - }), - ), - )(s) - } + move |s: &str| preceded(&parser, extract_command)(s) } fn using_command(command: &str) -> Res { map(extract_key(tag_no_case(USE)), CacheCommand::Using)(command) } +fn extract_command(command: &str) -> Res<&str> { + preceded( + multispace1, + take_while1(|s: char| s.is_alphanumeric() || s == '-' || s == '_'), + )(command) +} fn dump_command(command: &str) -> Res { map( preceded( @@ -211,12 +214,7 @@ fn dump_command(command: &str) -> Res { cut(verify(rest, |s: &str| { s.is_empty() || s.starts_with(' ') || s == "\n" })) - .and_then(opt(preceded( - multispace1, - take_while1(|s: char| { - s.is_alphanumeric() || s == '-' || s == '_' - }), - ))), + .and_then(opt(extract_command)), ), CacheCommand::Dump, )(command) @@ -242,6 +240,7 @@ pub fn parse_command(command: &str) -> Res { cd_command, del_command, get_command, + alias_command, using_command, dump_command, list_cache_command, diff --git a/src/cache_command/process.rs b/src/cache_command/process.rs index 881ea15..f9e38d4 100644 --- a/src/cache_command/process.rs +++ b/src/cache_command/process.rs @@ -34,7 +34,7 @@ pub fn process_command( insert_value(db, current_cache, aliases, value, false) { println!( - "added {} with hash keys {}", + "added {} with keys {}", Yellow.paint(value), Red.paint(key) ); @@ -45,6 +45,46 @@ pub fn process_command( ))); } } + CacheCommand::Alias((left, right)) => { + if check_reserved_keyword(&[right]) { + return Err(anyhow::Error::msg( + format!("{}",Red.paint("You cannot use a reserved keyword name as an alias.")), + )); + } + match ( + get_value(db, current_cache, left), + get_value(db, current_cache, right), + ) { + (Some(value), None) => { + if let Some(key) = insert_value( + db, + current_cache, + vec![right], + &value, + false, + ) { + println!( + "aliased {} with keys {}", + Yellow.paint(value), + Red.paint(key) + ); + } else { + return Err(anyhow::Error::msg(format!( + "{}", + Red.paint( + "could not alias! Right Key already exists" + ) + ))); + } + } + _ => { + return Err(anyhow::Error::msg(format!( + "{}", + Red.paint("could not alias! Wrong combination") + ))) + } + } + } CacheCommand::Del(key) => { if let Some(v) = remove_value(db, current_cache, key, false) { println!(