pzzld_server/cli/
cmd.rs

1/*
2    Appellation: opts <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5#[doc(inline)]
6pub use self::{build::*, deploy::*, serve::*};
7
8mod build;
9mod deploy;
10mod serve;
11
12use crate::{platform::Context, AsyncHandle};
13
14#[derive(
15    Clone,
16    Debug,
17    Eq,
18    Hash,
19    Ord,
20    PartialEq,
21    PartialOrd,
22    clap::Subcommand,
23    serde::Deserialize,
24    serde::Serialize,
25    strum::AsRefStr,
26    strum::Display,
27    strum::EnumCount,
28    strum::EnumIs,
29    strum::VariantNames,
30)]
31#[serde(rename_all = "lowercase")]
32#[strum(serialize_all = "lowercase")]
33pub enum Command {
34    Build(BuildCmd),
35    Serve(ServeCmd),
36}
37
38impl Command {
39    pub fn build(args: BuildCmd) -> Self {
40        Self::Build(args)
41    }
42
43    pub fn serve(args: ServeCmd) -> Self {
44        Self::Serve(args)
45    }
46
47    #[tracing::instrument(skip_all, name = "handle", target = "cmd")]
48    pub async fn handle<Ctx>(self, ctx: &mut Ctx) -> <Self as AsyncHandle<Ctx>>::Output
49    where
50        Self: AsyncHandle<Ctx>,
51        Ctx: core::fmt::Debug,
52    {
53        <Self as AsyncHandle<Ctx>>::handle(self, ctx).await
54    }
55}
56
57#[async_trait::async_trait]
58impl AsyncHandle<Context> for Command {
59    type Output = anyhow::Result<()>;
60
61    async fn handle(self, ctx: &mut Context) -> Self::Output {
62        match self {
63            Self::Build(cmd) => {
64                cmd.handle(ctx).await?;
65            }
66            Self::Serve(cmd) => {
67                cmd.handle(ctx).await?;
68            }
69        }
70
71        Ok(())
72    }
73}