Skip to main content

Middleware

Trait Middleware 

Source
pub trait Middleware: Send + Sync {
    // Provided methods
    fn before_dispatch(
        &self,
        _parsed: &ParsedCommand<'_>,
    ) -> Result<(), Box<dyn Error + Send + Sync>> { ... }
    fn after_dispatch(
        &self,
        _parsed: &ParsedCommand<'_>,
        _result: &Result<(), Box<dyn Error + Send + Sync>>,
    ) { ... }
    fn on_parse_error(&self, _error: &ParseError) { ... }
}
Expand description

Hook into the crate::cli::Cli parse-and-dispatch lifecycle.

All methods have default no-op implementations so you only need to override the hooks you care about.

§Examples

use argot_cmd::middleware::Middleware;
use argot_cmd::ParsedCommand;

struct Logger;

impl Middleware for Logger {
    fn before_dispatch(&self, parsed: &ParsedCommand<'_>) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        eprintln!("[log] dispatching: {}", parsed.command.canonical);
        Ok(())
    }
}

Provided Methods§

Source

fn before_dispatch( &self, _parsed: &ParsedCommand<'_>, ) -> Result<(), Box<dyn Error + Send + Sync>>

Called after a successful parse, before the handler is invoked.

Return Err(...) to abort dispatch with a crate::cli::CliError::Handler.

§Examples
use argot_cmd::middleware::Middleware;
use argot_cmd::ParsedCommand;

struct RateLimiter { max: usize }

impl Middleware for RateLimiter {
    fn before_dispatch(
        &self,
        parsed: &ParsedCommand<'_>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        // Allow all commands in this example.
        // A real implementation would check a counter.
        println!("dispatching: {}", parsed.command.canonical);
        Ok(())
    }
}
Source

fn after_dispatch( &self, _parsed: &ParsedCommand<'_>, _result: &Result<(), Box<dyn Error + Send + Sync>>, )

Called after the handler returns (whether Ok or Err).

§Examples
use argot_cmd::middleware::Middleware;
use argot_cmd::ParsedCommand;

struct AuditLog;

impl Middleware for AuditLog {
    fn after_dispatch(
        &self,
        parsed: &ParsedCommand<'_>,
        result: &Result<(), Box<dyn std::error::Error + Send + Sync>>,
    ) {
        match result {
            Ok(()) => println!("✓ {}", parsed.command.canonical),
            Err(e) => eprintln!("✗ {}: {}", parsed.command.canonical, e),
        }
    }
}
Source

fn on_parse_error(&self, _error: &ParseError)

Called when Parser::parse returns an error, before it is surfaced to the caller.

§Examples
use argot_cmd::middleware::Middleware;
use argot_cmd::parser::ParseError;

struct ErrorLogger;

impl Middleware for ErrorLogger {
    fn on_parse_error(&self, err: &ParseError) {
        eprintln!("parse failed: {}", err);
    }
}

Implementors§