Skip to content

Commit

Permalink
alias existing commands (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich authored Mar 13, 2023
1 parent 1902219 commit 23e4b61
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 26 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "adana"
version = "0.13.20"
version = "0.13.21"
edition = "2021"
authors = ["Nordine Bittich"]
license = "MIT"
Expand All @@ -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"] }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
5 changes: 4 additions & 1 deletion src/cache_command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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> },
Expand All @@ -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!");
}
&[
Expand All @@ -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`"),
Expand Down
33 changes: 16 additions & 17 deletions src/cache_command/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ fn get_command(command: &str) -> Res<CacheCommand> {
map(extract_key(tag_no_case(GET)), CacheCommand::Get)(command)
}

fn alias_command(command: &str) -> Res<CacheCommand> {
map(
pair(extract_key(tag_no_case(ALIAS)), extract_command),
|(left, right)| CacheCommand::Alias((left, right)),
)(command)
}

fn concat_command(command: &str) -> Res<CacheCommand> {
map(
alt((
Expand Down Expand Up @@ -187,36 +194,27 @@ 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<CacheCommand> {
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<CacheCommand> {
map(
preceded(
tag_no_case(DUMP),
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)
Expand All @@ -242,6 +240,7 @@ pub fn parse_command(command: &str) -> Res<CacheCommand> {
cd_command,
del_command,
get_command,
alias_command,
using_command,
dump_command,
list_cache_command,
Expand Down
42 changes: 41 additions & 1 deletion src/cache_command/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -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!(
Expand Down

0 comments on commit 23e4b61

Please sign in to comment.