diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 5548744a..299285cf 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -10,6 +10,12 @@ name = "acme-core" repository = "https://github.com/FL03/acme" version = "0.2.4" +[features] +default = [ "cli"] +full = [ "cli" ] + +cli = [ "clap/cargo", "clap/derive", "clap/default", "clap/env"] + [lib] crate-type = ["cdylib", "rlib"] test = true @@ -19,9 +25,10 @@ test = true [dev-dependencies] [dependencies] -async-trait = "0.1.59" +async-trait = "0.1.60" axum = "0.6.1" -clap = { features = ["derive"], version = "4.0.29" } +axum-core = "0.3.0" +clap = { features = ["cargo", "derive", "env"], optional = true, version = "4.0.29" } hyper = { features = ["full"], version = "0.14.23" } http = "0.2.8" http-body = "0.4.5" diff --git a/crates/core/src/specs/apps.rs b/crates/core/src/specs/apps.rs index 40acea12..d8345718 100644 --- a/crates/core/src/specs/apps.rs +++ b/crates/core/src/specs/apps.rs @@ -46,7 +46,3 @@ pub trait AsyncApplicationSpawner: AsyncSpawable { tracing::info!("Terminating the application and connected services..."); } } - -#[cfg(feature = "cli")] -#[cfg(not(feature = "wasm"))] -pub trait CommandLineInterface: clap::Parser {} diff --git a/crates/core/src/specs/cli.rs b/crates/core/src/specs/cli.rs new file mode 100644 index 00000000..d2aaaf65 --- /dev/null +++ b/crates/core/src/specs/cli.rs @@ -0,0 +1,43 @@ +/* + Appellation: cli + Contrib: FL03 + Description: ... summary ... +*/ +use super::{Handler, AsyncHandler}; +use scsys::AsyncResult; + +/// +pub trait CLISpec: clap::Parser { + type Cmds: Handler + clap::Subcommand; + + fn command(&self) -> Option + where + Self: Sized; + fn handler(&self) -> scsys::Result<&Self> + where + Self: Sized, + { + if let Some(cmd) = self.command() { + cmd.handler()?; + } + Ok(self) + } +} +/// +#[async_trait::async_trait] +pub trait AsyncCLISpec: clap::Parser { + type Cmds: AsyncHandler + clap::Subcommand; + + fn command(&self) -> Option + where + Self: Sized; + async fn handler(&self) -> AsyncResult<&Self> + where + Self: Sized, + { + if let Some(cmd) = self.command().clone() { + cmd.handler().await?; + } + Ok(self) + } +} \ No newline at end of file diff --git a/crates/core/src/specs/mod.rs b/crates/core/src/specs/mod.rs index 1d30e076..0ef117e6 100644 --- a/crates/core/src/specs/mod.rs +++ b/crates/core/src/specs/mod.rs @@ -3,17 +3,36 @@ Contrib: FL03 Description: ... summary ... */ -pub use self::apps::*; +pub use self::{apps::*, cli::*}; pub(crate) mod apps; +#[cfg(feature = "cli")] +pub(crate) mod cli; +use async_trait::async_trait; + +/// +#[async_trait] +pub trait AsyncHandler: Clone + Send + Sync { + type Error: std::error::Error + Send + Sync + 'static; + + async fn handler(&self) -> Result<&Self, Self::Error> + where + Self: Sized; +} +/// +#[async_trait] +pub trait AsyncSpawable { + async fn spawn(&mut self) -> scsys::AsyncResult<&Self>; +} +/// pub trait BaseApplication: BaseObject + Versionable { fn application(&self) -> &Self { self } fn namespace(&self) -> String; } - +/// pub trait BaseObject { fn count(&self) -> usize; fn name(&self) -> String; @@ -22,16 +41,19 @@ pub trait BaseObject { } fn symbol(&self) -> String; } +/// +pub trait Handler: Clone { + type Error: std::error::Error + 'static; + fn handler(&self) -> Result<&Self, Self::Error> + where + Self: Sized; +} +/// pub trait Spawnable { fn spawn(&mut self) -> scsys::Result<&Self>; } - -#[async_trait::async_trait] -pub trait AsyncSpawable { - async fn spawn(&mut self) -> scsys::AsyncResult<&Self>; -} - +/// pub trait Versionable { type Error;