use {
crate::{commands::*, types::*},
argh::FromArgs,
async_trait::async_trait,
};
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
pub enum SubCommand {
List(ListCommand),
ListAccessors(ListAccessorsCommand),
Selectors(SelectorsCommand),
Show(ShowCommand),
}
#[derive(FromArgs, PartialEq, Debug)]
pub struct CommandLine {
#[argh(option, default = "Format::Text", short = 'f')]
pub format: Format,
#[argh(subcommand)]
pub command: SubCommand,
}
macro_rules! execute_and_format {
($self:ident, $provider:ident, [$($command:ident),*]) => {
match &$self.command {
$(
SubCommand::$command(command) => {
let result = command.execute($provider).await?;
match $self.format {
Format::Json => {
serde_json::to_string_pretty(&result)
.map_err(|e| Error::InvalidCommandResponse(e))
}
Format::Text => {
Ok(result.to_string())
}
}
}
)*
}
}
}
#[async_trait]
impl Command for CommandLine {
type Result = String;
async fn execute<P: DiagnosticsProvider>(&self, provider: &P) -> Result<Self::Result, Error> {
execute_and_format!(self, provider, [List, ListAccessors, Selectors, Show])
}
}