diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-01-22 08:32:29 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-01-22 08:32:29 +0800 |
| commit | aca8b408755f9041da9ee083c625de2a8d8c6785 (patch) | |
| tree | 5747d389d5218ccf39e2153ae1346f7b5bfe8fb8 /src/subcmd/processer.rs | |
| parent | 0d614f3e2104e9b840ebc7e53a6caa6af1671636 (diff) | |
Refactor CLI command processing with new architecture
Diffstat (limited to 'src/subcmd/processer.rs')
| -rw-r--r-- | src/subcmd/processer.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/subcmd/processer.rs b/src/subcmd/processer.rs new file mode 100644 index 0000000..5849c62 --- /dev/null +++ b/src/subcmd/processer.rs @@ -0,0 +1,40 @@ +use crate::subcmd::cmd::JVCommandContext; +use crate::subcmd::cmds::_registry::{jv_cmd_nodes, jv_cmd_process_node}; +use crate::subcmd::errors::CmdProcessError; +use crate::subcmd::renderer::JVRenderResult; + +pub async fn jv_cmd_process( + args: Vec<String>, + ctx: JVCommandContext, +) -> Result<JVRenderResult, CmdProcessError> { + let nodes = jv_cmd_nodes(); + let command = args.join(" "); + + // Find nodes that match the beginning of the command + let matching_nodes: Vec<&String> = nodes + .iter() + .filter(|node| command.starts_with(node.as_str())) + .collect(); + + match matching_nodes.len() { + 0 => { + // No matching node found + return Err(CmdProcessError::NoMatchingCommand); + } + 1 => { + let matched_prefix = matching_nodes[0]; + let prefix_len = matched_prefix.split_whitespace().count(); + let trimmed_args: Vec<String> = args.into_iter().skip(prefix_len).collect(); + return jv_cmd_process_node(matched_prefix, trimmed_args, ctx).await; + } + _ => { + // Multiple matching nodes found + return Err(CmdProcessError::AmbiguousCommand( + matching_nodes + .iter() + .map(|s| s.to_string()) + .collect::<Vec<String>>(), + )); + } + } +} |
