pzzld_server/cli/
interface.rs

1/*
2    Appellation: interface <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use super::cmd::*;
6use crate::{platform::Context, AsyncHandle};
7
8#[derive(
9    Clone,
10    Debug,
11    Default,
12    Eq,
13    Hash,
14    Ord,
15    PartialEq,
16    PartialOrd,
17    clap::Parser,
18    serde::Deserialize,
19    serde::Serialize,
20)]
21#[clap(about, author, long_about = None, version)]
22#[command(arg_required_else_help(true), allow_missing_positional(true))]
23pub struct Cli {
24    #[clap(subcommand)]
25    pub command: Option<Command>,
26    #[clap(long, short = 'C', default_value_t = String::from("Puzzled.toml"))]
27    pub config: String,
28    #[clap(action = clap::ArgAction::SetTrue, long, short)]
29    pub release: bool,
30    #[arg(action = clap::ArgAction::SetTrue, long, short)]
31    pub update: bool,
32    #[arg(action = clap::ArgAction::SetTrue, long, short)]
33    pub verbose: bool,
34}
35
36impl Cli {
37    pub fn new() -> Self {
38        clap::Parser::parse()
39    }
40
41    pub fn command(&self) -> Option<&Command> {
42        self.command.as_ref()
43    }
44
45    pub fn config(&self) -> &str {
46        &self.config
47    }
48
49    pub fn release(&self) -> bool {
50        self.release
51    }
52
53    pub fn update(&self) -> bool {
54        self.update
55    }
56
57    pub fn verbose(&self) -> bool {
58        self.verbose
59    }
60
61    #[tracing::instrument(skip_all, name = "handle", target = "cli")]
62    pub async fn handle<Ctx>(self, ctx: &mut Ctx) -> <Self as AsyncHandle<Ctx>>::Output
63    where
64        Self: AsyncHandle<Ctx>,
65        Ctx: core::fmt::Debug,
66    {
67        <Self as AsyncHandle<Ctx>>::handle(self, ctx).await
68    }
69}
70
71#[async_trait::async_trait]
72impl AsyncHandle<Context> for Cli {
73    type Output = anyhow::Result<()>;
74
75    async fn handle(self, ctx: &mut Context) -> Self::Output {
76        if let Some(cmd) = self.command {
77            cmd.handle(ctx).await?;
78        }
79
80        Ok(())
81    }
82}