Skip to main content

twinleaf_tools/cli/
rpc.rs

1use clap::{
2    builder::{PossibleValuesParser, TypedValueParser, ValueHint},
3    Args, Subcommand,
4};
5use twinleaf::device::{util, RpcValueType};
6
7use crate::TioOpts;
8
9#[derive(Args, Debug)]
10#[command(args_conflicts_with_subcommands = true, arg_required_else_help = true)]
11pub struct RpcCli {
12    #[command(flatten)]
13    pub tio: TioOpts,
14
15    #[command(subcommand)]
16    pub subcommands: Option<RPCSubcommands>,
17
18    /// RPC name to execute
19    #[arg(value_hint = ValueHint::Other)]
20    pub rpc_name: Option<String>,
21
22    /// RPC argument value
23    #[arg(
24        allow_negative_numbers = true,
25        value_name = "ARG",
26        value_hint = ValueHint::Other,
27        help_heading = "RPC Arguments"
28    )]
29    pub rpc_arg: Option<String>,
30
31    /// RPC request type
32    #[arg(
33        short = 't',
34        long = "req-type",
35        value_parser = PossibleValuesParser::new(util::RPC_TYPE_NAMES)
36            .map(|s: String| util::parse_rpc_type(&s).expect("validated by possible-values")),
37        help_heading = "Type Options",
38    )]
39    pub req_type: Option<RpcValueType>,
40
41    /// RPC reply type
42    #[arg(
43        short = 'T',
44        long = "rep-type",
45        value_parser = PossibleValuesParser::new(util::RPC_TYPE_NAMES)
46            .map(|s: String| util::parse_rpc_type(&s).expect("validated by possible-values")),
47        help_heading = "Type Options",
48    )]
49    pub rep_type: Option<RpcValueType>,
50
51    /// Enable debug output
52    #[arg(short = 'd', long)]
53    pub debug: bool,
54}
55
56#[derive(Subcommand, Debug)]
57pub enum RPCSubcommands {
58    /// List available RPCs on the device
59    List {
60        #[command(flatten)]
61        tio: TioOpts,
62    },
63    /// Dump RPC data from the device
64    Dump {
65        #[command(flatten)]
66        tio: TioOpts,
67
68        /// RPC name to dump
69        #[arg(value_hint = ValueHint::Other)]
70        rpc_name: String,
71
72        /// Trigger a capture before dumping
73        #[arg(long)]
74        capture: bool,
75    },
76}