ayun_console/
command.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use crate::{traits, STYLES};
use ayun_core::{Callback, Result};
use clap::ArgMatches;
use std::sync::Arc;

pub struct Command {
    inner: clap::Command,
    handler: Callback<ArgMatches, Result<()>>,
}

impl Command {
    pub fn new<A, T>() -> Self
    where
        A: traits::ApplicationTrait,
        T: traits::CommandTrait,
    {
        Self {
            inner: T::command().styles(STYLES),
            handler: Arc::new(|arg_matches: ArgMatches| T::handle::<A>(arg_matches)),
        }
    }

    pub fn name(&self) -> &str {
        self.inner.get_name()
    }

    pub fn command(&self) -> &clap::Command {
        &self.inner
    }

    pub fn handler(&self) -> &Callback<ArgMatches, Result<()>> {
        &self.handler
    }
}