Skip to main content

Handler

Trait Handler 

Source
pub trait Handler {
    type Output: Serialize;

    // Required method
    fn handle(
        &mut self,
        matches: &ArgMatches,
        ctx: &CommandContext,
    ) -> HandlerResult<Self::Output>;
}
Expand description

Trait for command handlers.

Handlers take &mut self allowing direct mutation of internal state. This is the common case for CLI applications which are single-threaded.

§Example

use standout_dispatch::{Handler, HandlerResult, Output, CommandContext};
use clap::ArgMatches;
use serde::Serialize;

struct Counter { count: u32 }

impl Handler for Counter {
    type Output = u32;

    fn handle(&mut self, _m: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<u32> {
        self.count += 1;
        Ok(Output::Render(self.count))
    }
}

Required Associated Types§

Source

type Output: Serialize

The output type produced by this handler (must be serializable)

Required Methods§

Source

fn handle( &mut self, matches: &ArgMatches, ctx: &CommandContext, ) -> HandlerResult<Self::Output>

Execute the handler with the given matches and context.

Implementors§

Source§

impl<F, T, R> Handler for FnHandler<F, T, R>

Source§

impl<F, T, R> Handler for SimpleFnHandler<F, T, R>
where F: FnMut(&ArgMatches) -> R, R: IntoHandlerResult<T>, T: Serialize,