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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::ffi::OsString;

use eyre::{bail, eyre, Report};

use crate::{
    command::Context,
    terminal::{self, Args, Help},
};

mod account;

#[derive(Debug)]
enum Command {
    Account,
}

impl TryFrom<&str> for Command {
    type Error = Report;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "account" => Ok(Self::Account),
            _ => eyre::bail!("unknown command: {}", value),
        }
    }
}

pub const HELP: Help = Help {
    name: "query",
    description: "Query node data, e.g. on-chain state",
    version: env!("CARGO_PKG_VERSION"),
    usage: r#"
Usage

    flt query <command>

Options
    "#,
};

pub struct Options {
    command: Command,
    args: Vec<OsString>,
}

impl Args for Options {
    #[allow(clippy::never_loop)]
    fn from_args(args: Vec<OsString>) -> eyre::Result<Self> {
        use lexopt::prelude::*;

        let mut parser = lexopt::Parser::from_args(args);
        let mut command: Option<Command> = None;

        while let Some(arg) = parser.next()? {
            match arg {
                Value(cmd) if command.is_none() => {
                    let cmd = cmd.to_str().ok_or(eyre!("command is not UTF-8"))?;
                    command = Some(Command::try_from(cmd)?);

                    break;
                },
                _ => bail!(arg.unexpected()),
            }
        }

        Ok(Self {
            command: command.unwrap(),
            args: parser.raw_args()?.collect::<Vec<_>>(),
        })
    }
}

pub fn run(ctx: Context, opts: Options) -> eyre::Result<()> {
    match opts.command {
        Command::Account => terminal::run::<account::Options, Context, _>(
            ctx,
            account::HELP,
            account::run,
            opts.args,
        ),
    }
}