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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crateFormat;
use Args;
use ;
use Deref;
/// An embeddable format argument struct for use in clap CLIs.
///
/// `FormatArg`implements `Deref<Target = Format>`, so you can access methods
/// from [`Format`] like [`Format::to_string`] ergonomically on the `FormatArg`
/// itself.
///
/// For an optional format argument, use [`FormatArgOpt`].
///
/// # Serialization
///
/// `FormatArg` implements `Serialize` and `Deserialize` and flattens its
/// internal `format` field such that `FormatArg` should always be transparent
/// to any callers or persistence.
///
/// # Examples
///
/// The following shows how to use this in a clap CLI struct as a required
/// argument:
///
/// ```
/// use clap::Parser;
/// use formattable::FormatArg;
/// use serde::Serialize;
///
/// /// Demonstrate how to use `formattable` in a `clap`-based CLI.
/// ///
/// /// This example just dumps the CLI arguments themselves as the selected format.
/// #[derive(Debug, Parser, Serialize)]
/// struct Cli {
/// #[clap(flatten)]
/// format: FormatArg,
/// }
///
/// // Replace this with `Cli::parse` to actually parse passed arguments.
/// let cli = Cli::parse_from(["", "-f", "json"]);
///
/// println!("{}", cli.format.to_string_pretty(&cli).unwrap());
/// ```
/// An embeddable optional format argument struct for use in clap CLIs.
///
/// `FormatArgOpt` implements `Deref<Target = Option<Format>>`, so you can access the
/// format ergonomically via destructuring. See the examples for details.
///
/// For a non-optional form of the argument, see [`FormatArg`].
///
/// # Serialization
///
/// `FormatArgOpt` implements `Serialize` and `Deserialize` and flattens its
/// internal `format` field such that `FormatArgOpt` should always be transparent
/// to any callers or persistence.
///
/// # Examples
///
/// ```
/// use clap::Parser;
/// use formattable::FormatArgOpt;
/// use serde::Serialize;
///
/// /// Demonstrate how to use `formattable::FormatArgOpt` in a `clap`-based CLI.
/// ///
/// /// This example just dumps the CLI arguments themselves in the selected format.
/// #[derive(Debug, Parser, Serialize)]
/// struct Cli {
/// #[clap(flatten)]
/// format: FormatArgOpt,
/// }
///
/// // Prints nothing if no format is specified.
/// let cli = Cli::parse_from([""]);
/// if let Some(format) = *cli.format {
/// println!("{}", format.to_string_pretty(&cli).unwrap());
/// }
///
/// // Prints JSON if JSON is specified.
/// let cli = Cli::parse_from(["", "-f", "json"]);
/// if let Some(format) = *cli.format {
/// println!("{}", format.to_string_pretty(&cli).unwrap());
/// }
/// ```