Skip to main content

cli/commands/
abstract_command.rs

1//! Upstream source: `../nest-cli/commands/abstract.command.ts`.
2
3use crate::actions::ActionInvocation;
4
5use super::{CommandName, CommandParseError, CommandSpec, command_spec, load_command_invocation};
6
7/// Typed Rust equivalent of upstream `AbstractCommand`.
8///
9/// Upstream command classes bind a concrete action and register a commander
10/// callback. In this port the commander callback is represented by `load`,
11/// which parses command-local arguments into the same action invocation shape.
12pub trait AbstractCommand {
13    fn command_name(&self) -> CommandName;
14
15    fn spec(&self) -> &'static CommandSpec {
16        command_spec(self.command_name()).expect("command spec must be registered")
17    }
18
19    fn load<I, S>(&self, args: I) -> Result<ActionInvocation, CommandParseError>
20    where
21        I: IntoIterator<Item = S>,
22        S: Into<String>,
23    {
24        let mut tokens = Vec::new();
25        tokens.push(self.command_name().as_str().to_string());
26        tokens.extend(args.into_iter().map(Into::into));
27        load_command_invocation(tokens)
28    }
29}