unreql/cmd/args/
one_two_opt.rs

1use serde::Serialize;
2
3use crate::{cmd::options::Index, Command};
4
5use super::{Args, ArgsWithOpt, WithOpts};
6
7/// Required first argument and optional second
8///
9/// Variants:
10///
11/// ```rust,ignore
12/// // first required argument
13/// r.example(arg);
14///
15/// // with both arguments as array of two elements
16/// r.example(r.args([arg1, arg2]));
17///
18/// // with both arguments as tuple of two elements
19/// r.example(r.args((arg1, arg2)));
20///
21/// // first required argument with command options
22/// r.example(r.with_opt(arg, opt));
23///
24/// // with both arguments as array of two elements with command options
25/// r.example(r.with_opt(r.args([arg1, arg2]), opt));
26///
27/// // with both arguments as tuple of two elements with command options
28/// r.example(r.with_opt(r.args((arg1, arg2)), opt));
29/// ```
30pub trait OneAndSecondOptionalArg<P> {
31    fn with_cmd(self, cmd: Command) -> Command;
32}
33
34impl<T, P> OneAndSecondOptionalArg<P> for T
35where
36    T: Serialize,
37{
38    fn with_cmd(self, cmd: Command) -> Command {
39        cmd.with_arg(Command::from_json(self))
40    }
41}
42
43impl<T, P> OneAndSecondOptionalArg<P> for Args<[T; 2]>
44where
45    T: Serialize,
46{
47    fn with_cmd(self, cmd: Command) -> Command {
48        self.0
49            .iter()
50            .fold(cmd, |cmd, arg| cmd.with_arg(Command::from_json(arg)))
51    }
52}
53
54impl<T1, T2, P> OneAndSecondOptionalArg<P> for Args<(T1, T2)>
55where
56    T1: Serialize,
57    T2: Serialize,
58{
59    fn with_cmd(self, cmd: Command) -> Command {
60        cmd.with_arg(Command::from_json(self.0 .0))
61            .with_arg(Command::from_json(self.0 .1))
62    }
63}
64
65impl<T, P> OneAndSecondOptionalArg<P> for ArgsWithOpt<T, P>
66where
67    T: Serialize,
68    P: WithOpts,
69{
70    fn with_cmd(self, cmd: Command) -> Command {
71        let cmd = cmd.with_arg(Command::from_json(self.0));
72        self.1.with_opts(cmd)
73    }
74}
75
76impl<T1, T2, P> OneAndSecondOptionalArg<P> for ArgsWithOpt<Args<(T1, T2)>, P>
77where
78    T1: Serialize,
79    T2: Serialize,
80    P: WithOpts,
81{
82    fn with_cmd(self, cmd: Command) -> Command {
83        let cmd = cmd
84            .with_arg(Command::from_json(self.0 .0 .0))
85            .with_arg(Command::from_json(self.0 .0 .1));
86        self.1.with_opts(cmd)
87    }
88}
89
90impl<T, P> OneAndSecondOptionalArg<P> for ArgsWithOpt<Args<[T; 2]>, P>
91where
92    T: Serialize,
93    P: WithOpts,
94{
95    fn with_cmd(self, cmd: Command) -> Command {
96        let cmd = self
97            .0
98             .0
99            .iter()
100            .fold(cmd, |cmd, arg| cmd.with_arg(Command::from_json(arg)));
101        self.1.with_opts(cmd)
102    }
103}
104
105impl OneAndSecondOptionalArg<Index> for Index {
106    fn with_cmd(self, cmd: Command) -> Command {
107        self.with_opts(cmd)
108    }
109}