CommandService

Trait CommandService 

Source
pub trait CommandService: Send + Sync {
    type Output: Serialize + Debug + Send;

    // Required method
    fn execute<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Output>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn to_json(&self, output: &Self::Output) -> Result<String> { ... }
    fn execute_with_json<'life0, 'async_trait>(
        &'life0 self,
        json: bool,
    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

Command service trait for all CLI commands

This trait provides a unified interface for command execution with support for:

  • Type-safe structured outputs
  • JSON serialization
  • Async execution
  • Error handling

§Example

use miyabi_cli::service::CommandService;
use miyabi_cli::error::Result;
use async_trait::async_trait;
use serde::Serialize;

#[derive(Debug, Serialize)]
pub struct StatusOutput {
    pub is_installed: bool,
    pub git_branch: Option<String>,
}

pub struct StatusCommand {
    pub watch: bool,
}

#[async_trait]
impl CommandService for StatusCommand {
    type Output = StatusOutput;

    async fn execute(&self) -> Result<Self::Output> {
        // Command implementation
        Ok(StatusOutput {
            is_installed: true,
            git_branch: Some("main".to_string()),
        })
    }
}

Required Associated Types§

Source

type Output: Serialize + Debug + Send

Output type for this command

Must implement Serialize for JSON output and Debug for error reporting

Required Methods§

Source

fn execute<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Self::Output>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute the command and return structured output

This method performs the core command logic and returns a structured result that can be serialized to JSON or displayed to the user.

Provided Methods§

Source

fn to_json(&self, output: &Self::Output) -> Result<String>

Convert output to JSON string

Default implementation uses serde_json::to_string_pretty

Source

fn execute_with_json<'life0, 'async_trait>( &'life0 self, json: bool, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute command and optionally output as JSON

This is a convenience method that combines execute() and to_json()

Implementors§