1use crate::commands::*;
6use crate::types::*;
7use argh::FromArgs;
8use serde::Serialize;
9
10#[derive(FromArgs, PartialEq, Debug)]
11#[argh(subcommand)]
12pub enum SubCommand {
13 List(ListCommand),
14 ListAccessors(ListAccessorsCommand),
15 Selectors(SelectorsCommand),
16 Show(ShowCommand),
17}
18
19#[derive(FromArgs, PartialEq, Debug)]
20pub struct CommandLine {
22 #[argh(option, default = "Format::Text", short = 'f')]
23 pub format: Format,
25
26 #[argh(subcommand)]
27 pub command: SubCommand,
28}
29
30fn serialize<T: Serialize + ToString>(format: &Format, input: T) -> Result<String, Error> {
31 match format {
32 Format::Json => serde_json::to_string_pretty(&input).map_err(Error::InvalidCommandResponse),
33 Format::Text => Ok(input.to_string()),
34 }
35}
36
37impl Command for CommandLine {
38 type Result = String;
39
40 async fn execute<P: DiagnosticsProvider>(self, provider: &P) -> Result<Self::Result, Error> {
41 match self.command {
42 SubCommand::List(c) => serialize(&self.format, c.execute(provider).await?),
43 SubCommand::ListAccessors(c) => serialize(&self.format, c.execute(provider).await?),
44 SubCommand::Selectors(c) => serialize(&self.format, c.execute(provider).await?),
45 SubCommand::Show(c) => serialize(&self.format, c.execute(provider).await?),
46 }
47 }
48}