iquery/
command_line.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::commands::*;
use crate::types::*;
use argh::FromArgs;
use serde::Serialize;

#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
pub enum SubCommand {
    List(ListCommand),
    ListAccessors(ListAccessorsCommand),
    Selectors(SelectorsCommand),
    Show(ShowCommand),
}

#[derive(FromArgs, PartialEq, Debug)]
/// Top-level command.
pub struct CommandLine {
    #[argh(option, default = "Format::Text", short = 'f')]
    /// the format to be used to display the results (json, text).
    pub format: Format,

    #[argh(subcommand)]
    pub command: SubCommand,
}

fn serialize<T: Serialize + ToString>(format: &Format, input: T) -> Result<String, Error> {
    match format {
        Format::Json => serde_json::to_string_pretty(&input).map_err(Error::InvalidCommandResponse),
        Format::Text => Ok(input.to_string()),
    }
}

impl Command for CommandLine {
    type Result = String;

    async fn execute<P: DiagnosticsProvider>(self, provider: &P) -> Result<Self::Result, Error> {
        match self.command {
            SubCommand::List(c) => serialize(&self.format, c.execute(provider).await?),
            SubCommand::ListAccessors(c) => serialize(&self.format, c.execute(provider).await?),
            SubCommand::Selectors(c) => serialize(&self.format, c.execute(provider).await?),
            SubCommand::Show(c) => serialize(&self.format, c.execute(provider).await?),
        }
    }
}