nestrs-cli-rs 0.1.0

Rust port of the Nest CLI for the nestrs organization.
Documentation
//! Upstream source: `../nest-cli/commands/abstract.command.ts`.

use crate::actions::ActionInvocation;

use super::{CommandName, CommandParseError, CommandSpec, command_spec, load_command_invocation};

/// Typed Rust equivalent of upstream `AbstractCommand`.
///
/// Upstream command classes bind a concrete action and register a commander
/// callback. In this port the commander callback is represented by `load`,
/// which parses command-local arguments into the same action invocation shape.
pub trait AbstractCommand {
    fn command_name(&self) -> CommandName;

    fn spec(&self) -> &'static CommandSpec {
        command_spec(self.command_name()).expect("command spec must be registered")
    }

    fn load<I, S>(&self, args: I) -> Result<ActionInvocation, CommandParseError>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let mut tokens = Vec::new();
        tokens.push(self.command_name().as_str().to_string());
        tokens.extend(args.into_iter().map(Into::into));
        load_command_invocation(tokens)
    }
}