clap_handler/
traits.rs

1use crate::context::Context;
2
3#[cfg(not(feature = "async"))]
4pub trait Handler: Sized + Clone + Send + 'static {
5    fn run(mut self) -> anyhow::Result<()> {
6        self.execute(Default::default())
7    }
8
9    fn execute(&mut self, mut ctx: Context) -> anyhow::Result<()> {
10        ctx.insert(self.clone());
11        self.handle_command(&mut ctx)?;
12        self.handle_subcommand(ctx)
13    }
14
15    fn handle_command(&mut self, _: &mut Context) -> anyhow::Result<()> {
16        Ok(())
17    }
18
19    fn handle_subcommand(&mut self, _: Context) -> anyhow::Result<()> {
20        Ok(())
21    }
22}
23
24#[cfg(feature = "async")]
25#[async_trait::async_trait]
26pub trait Handler: Sized + Clone + 'static {
27    async fn run(mut self) -> anyhow::Result<()> {
28        self.execute(Default::default()).await
29    }
30
31    async fn execute(&mut self, mut ctx: Context) -> anyhow::Result<()> {
32        ctx.insert(self.clone());
33        self.handle_command(&mut ctx).await?;
34        self.handle_subcommand(ctx).await
35    }
36
37    async fn handle_command(&mut self, _: &mut Context) -> anyhow::Result<()> {
38        Ok(())
39    }
40
41    async fn handle_subcommand(&mut self, _: Context) -> anyhow::Result<()> {
42        Ok(())
43    }
44}