forked from orhun/halp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parser): create the command parser
- Loading branch information
Showing
1 changed file
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use std::collections::HashMap; | ||
|
||
/// Parses a command string into a HashMap that contains the command parts. | ||
/// | ||
/// the command string is expected to be in the format: | ||
/// `cmd <subcommand> <args>` | ||
/// the subcommand and args are optional. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// # use halp::helper::docs::cmd_parse::parse_cmd; | ||
/// | ||
/// let git_commit = "git commit -a"; | ||
/// let mut values_map = parse_cmd(git_commit); | ||
/// assert!(values_map.contains_key("cmd")); | ||
/// assert_eq!(values_map.get("cmd").unwrap(), "git"); | ||
/// assert!(values_map.contains_key("subcommand")); | ||
/// assert_eq!(values_map.get("subcommand").unwrap(), "commit"); | ||
/// assert!(values_map.contains_key("args")); | ||
/// assert_eq!(values_map.get("args").unwrap(), "-a"); | ||
/// ``` | ||
/// | ||
/// # Panics | ||
/// THIS FUNCTION WILL PANIC IF THE COMMAND STRING IS EMPTY. | ||
pub fn parse_cmd(cmd: &str) -> HashMap<String, String> { | ||
let mut values_map = HashMap::with_capacity(3); | ||
let cmd = cmd.to_string(); | ||
if cmd.contains(' ') { | ||
let mut cmd = cmd.split(' '); | ||
values_map.insert("cmd".to_string(), cmd.next().expect("The command should exist").to_string()); | ||
if let Some(subcommand) = cmd.next() { | ||
values_map.insert("subcommand".to_string(), subcommand.to_string()); | ||
} | ||
if let Some(args) = cmd.next() { | ||
values_map.insert("args".to_string(), args.to_string()); | ||
} | ||
} else { | ||
values_map.insert("cmd".to_string(), cmd); | ||
} | ||
values_map | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn test_parse_complete_cmd() { | ||
let git_commit = "git commit -a"; | ||
let values_map = parse_cmd(git_commit); | ||
assert!(values_map.contains_key("cmd")); | ||
assert_eq!(values_map.get("cmd"), Some(&"git".to_string())); | ||
assert!(values_map.contains_key("subcommand")); | ||
assert_eq!(values_map.get("subcommand"), Some(&"commit".to_string())); | ||
assert!(values_map.contains_key("args")); | ||
assert_eq!(values_map.get("args"), Some(&"-a".to_string())); | ||
} | ||
|
||
#[test] | ||
fn test_parse_cmd_with_no_args() { | ||
let git_commit = "git commit"; | ||
let values_map = parse_cmd(git_commit); | ||
assert!(values_map.contains_key("cmd")); | ||
assert_eq!(values_map.get("cmd"), Some(&"git".to_string())); | ||
assert!(values_map.contains_key("subcommand")); | ||
assert_eq!(values_map.get("subcommand"), Some(&"commit".to_string())); | ||
assert!(!values_map.contains_key("args")); | ||
} | ||
|
||
#[test] | ||
fn test_parse_cmd_with_no_subcommand() { | ||
let git_commit = "git"; | ||
let values_map = parse_cmd(git_commit); | ||
assert!(values_map.contains_key("cmd")); | ||
assert_eq!(values_map.get("cmd"), Some(&"git".to_string())); | ||
assert!(!values_map.contains_key("subcommand")); | ||
assert!(!values_map.contains_key("args")); | ||
} | ||
|
||
#[test] | ||
fn test_parse_cmd_with_no_subcommand_and_args() { | ||
let git_commit = "git"; | ||
let values_map = parse_cmd(git_commit); | ||
assert!(values_map.contains_key("cmd")); | ||
assert_eq!(values_map.get("cmd"), Some(&"git".to_string())); | ||
assert!(!values_map.contains_key("subcommand")); | ||
assert!(!values_map.contains_key("args")); | ||
} | ||
} |