congen 0.2.1

congen helps you build configuration systems that support partial updates from structured changes and CLI input
Documentation
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Load a [CongenChange] using [clap].
//!
//! See [CongenClap]

use std::ffi::OsString;

use clap::{Arg, ArgMatches, Args, Command, FromArgMatches, parser::MatchesError};

use crate::{
    Configuration,
    internal::{
        ChangeVerb, CongenChange, Description, FieldDescription, ListDescription, ListKey,
        ListKeyType, ListValue, ListVerb,
    },
};

/// provides a [clap::Args] implementation for a [Configuration] for clap-derive.
///
/// Clap will parse a [CongenChange] for the [Configuration] which can
/// then be applied to an existing config.
///
/// # Example using clap-derive
///
/// ```rust
/// use congen::{Configuration, CongenClap};
/// use clap::{Parser, Subcommand};
///
/// #[derive(Configuration, Debug)]
/// struct Config {
///     retries: u32,
///     #[congen(default)]
///     token: Option<String>,
/// }
///
/// #[derive(Parser)]
/// struct Cli {
///     #[command(subcommand)]
///     command: Commands,
/// }
///
/// #[derive(Subcommand)]
/// enum Commands {
///     Config(CongenClap<Config>),
///     Run,
/// }
/// ```
///
/// # Example using clap-builder
///
/// ```rust
/// # use congen::Configuration;
/// use congen::CongenClap;
/// use clap::FromArgMatches;
/// # #[derive(Configuration, Debug)]
/// # struct Config {
/// #     retries: u32,
/// #     #[congen(default)]
/// #     token: Option<String>,
/// # }
/// let matches = CongenClap::<Config>::create_cmd("config")
///     .get_matches_from(&["config", "retries", "set", "5"]);
/// let change = CongenClap::<Config>::from_arg_matches(&matches)
///     .unwrap()
///     .into_change();
/// ```
#[derive(Debug, Clone)]
pub struct CongenClap<T: Configuration> {
    change: T::CongenChange,
}

impl<T: Configuration> CongenClap<T> {
    /// Provides the [CongenChange]
    pub fn into_change(self) -> T::CongenChange {
        self.change
    }

    /// Create a [clap::Command] that will result in a [CongenClap]
    ///
    /// This can be used with [clap]s non-derive based builders.
    pub fn create_cmd(cmd_name: impl Into<clap::builder::Str>) -> Command {
        let for_update = false;
        Command::new(cmd_name)
            .subcommands(actionable_subcommands(
                &T::description("__clap"),
                for_update,
            ))
            .subcommand_required(true)
    }
}

fn field_value_arg(field: &FieldDescription) -> Arg {
    let mut arg = Arg::new("value").value_name(field.type_name.to_uppercase());
    if let Some(clap) = &field.clap_data {
        arg = arg.value_hint(clap.value_hint);
        if clap.allow_hyphen_values {
            arg = arg.allow_hyphen_values(true);
        }
        if clap.allow_negative_numbers {
            arg = arg.allow_negative_numbers(true);
        }
        if let Some(parser) = &clap.value_parser {
            arg = arg.value_parser(parser.clone());
        }
    }
    arg
}

fn actionable_subcommands(
    description: &Description,
    for_update: bool,
) -> impl Iterator<Item = Command> {
    description
        .actionable_fields()
        .into_iter()
        .map(move |mut actionable| {
            let field_name = actionable.path.make_contiguous().join(".");
            let mut field_command = Command::new(&field_name).subcommand_required(!for_update);

            match &actionable.description {
                Description::Field(field) => {
                    let mut set = Command::new("set");
                    if !field.is_flag {
                        set = set.arg(field_value_arg(field).required(!for_update));
                    }
                    field_command = field_command.subcommand(set);
                }
                Description::List(list) => {
                    let key_arg = Arg::new("key").value_name("AT").required(!for_update);
                    let key_arg = match list.key_type {
                        ListKeyType::String => key_arg,
                        ListKeyType::Signed => key_arg
                            .value_parser(clap::value_parser!(i64))
                            .allow_negative_numbers(true),
                        ListKeyType::Unsigned => key_arg.value_parser(clap::value_parser!(u64)),
                    };

                    // append, update, remove, empty
                    field_command = field_command
                        .subcommand(Command::new("empty"))
                        .subcommand(Command::new("remove").arg(key_arg.clone()));

                    match list.inner_desc.as_ref() {
                        // TODO subcommand append-default, update-with-default
                        Description::Field(inner_field) => {
                            let value_arg = field_value_arg(inner_field).required(!for_update);
                            let append_command = if list.append_requires_key {
                                Command::new("append")
                                    .arg(key_arg.clone())
                                    .arg(value_arg.clone())
                            } else {
                                Command::new("append").arg(value_arg.clone())
                            };
                            field_command = field_command
                                .subcommand(append_command)
                                .subcommand(Command::new("update").arg(key_arg).arg(value_arg));
                        }
                        Description::Composit(_) | Description::List(_) => {
                            let append_command = if list.append_requires_key {
                                Command::new("append")
                                    .arg(key_arg.clone())
                                    .subcommands(actionable_subcommands(
                                        list.inner_desc.as_ref(),
                                        for_update,
                                    ))
                                    .subcommand_required(!for_update)
                            } else {
                                Command::new("append")
                                    .subcommands(actionable_subcommands(
                                        list.inner_desc.as_ref(),
                                        for_update,
                                    ))
                                    .subcommand_required(!for_update)
                            };
                            field_command = field_command.subcommand(append_command).subcommand(
                                Command::new("update")
                                    .arg(key_arg)
                                    .subcommands(actionable_subcommands(
                                        list.inner_desc.as_ref(),
                                        for_update,
                                    ))
                                    .subcommand_required(!for_update),
                            );
                        }
                    }
                }
                _ => (),
            }

            if actionable.description.has_default() {
                field_command = field_command.subcommand(Command::new("use-default"));
            }
            if actionable.description.allow_unset() {
                field_command = field_command.subcommand(Command::new("unset"));
            }

            field_command
        })
}

impl<T: Configuration> Args for CongenClap<T> {
    fn augment_args(cmd: Command) -> Command {
        let for_update = false;
        cmd.subcommands(actionable_subcommands(
            &T::description("__clap"),
            for_update,
        ))
        .subcommand_required(true)
    }

    fn augment_args_for_update(cmd: Command) -> Command {
        let for_update = true;
        cmd.subcommands(actionable_subcommands(
            &T::description("__clap"),
            for_update,
        ))
        .subcommand_required(true)
    }
}

impl<T: Configuration> FromArgMatches for CongenClap<T>
where
    T::CongenChange: Sized,
{
    fn from_arg_matches(matches: &clap::ArgMatches) -> Result<Self, clap::Error> {
        let mut res = Self {
            change: CongenChange::empty(),
        };
        res.update_from_arg_matches(matches)?;
        Ok(res)
    }

    fn update_from_arg_matches(&mut self, matches: &clap::ArgMatches) -> Result<(), clap::Error> {
        let description = T::description("");
        let (field_path, verb) = get_verb_from_cmd(matches, &description)?;

        let change = T::CongenChange::from_path_and_verb(field_path, verb)
            .expect("Failed to create change for path");

        self.change.apply_change(change);
        Ok(())
    }
}

fn get_verb_from_cmd<'a>(
    cmd_matches: &'a ArgMatches,
    description: &Description,
) -> Result<(impl Iterator<Item = &'a str>, ChangeVerb), clap::error::Error> {
    let Some((field_name, field_cmd)) = cmd_matches.subcommand() else {
        return Err(clap::Error::new(clap::error::ErrorKind::MissingSubcommand));
    };
    let field_path = field_name.split(".");
    let Some(field_desc) = description.actionable_field(field_path.clone()) else {
        return Err(clap::Error::raw(
            clap::error::ErrorKind::InvalidValue,
            "invalid path",
        ));
    };
    let Some((verb_name, verb_cmd)) = field_cmd.subcommand() else {
        return Err(clap::Error::raw(
            clap::error::ErrorKind::MissingSubcommand,
            "missing verb",
        ));
    };
    let verb = match (verb_name, field_desc) {
        ("unset", _) => ChangeVerb::Unset,
        ("use-default", _) => ChangeVerb::UseDefault,

        ("set", Description::Field(_)) | ("set", Description::Composit(_)) => {
            match verb_cmd.try_get_raw("value") {
                Ok(Some(mut raw_value)) => {
                    let Some(value) = raw_value.next() else {
                        return Err(clap::Error::raw(
                            clap::error::ErrorKind::TooFewValues,
                            "missing value",
                        ));
                    };
                    if raw_value.next().is_some() {
                        return Err(clap::Error::raw(
                            clap::error::ErrorKind::TooManyValues,
                            "to many values",
                        ));
                    }
                    ChangeVerb::Set(value.to_owned())
                }
                Err(MatchesError::UnknownArgument { .. }) => ChangeVerb::SetFlag,
                Ok(None) => {
                    return Err(clap::Error::raw(
                        clap::error::ErrorKind::TooFewValues,
                        "missing value",
                    ));
                }
                Err(_err) => return Err(clap::Error::new(clap::error::ErrorKind::InvalidValue)),
            }
        }

        (verb_name, Description::List(list_desc)) => {
            get_verb_for_list(verb_name, verb_cmd, &list_desc)?.into()
        }

        (verb, desc) => {
            return Err(clap::Error::raw(
                clap::error::ErrorKind::InvalidSubcommand,
                format!("invalid verb ({verb}) for {desc:?}"),
            ));
        }
    };
    Ok((field_path, verb))
}

fn get_value(verb_cmd: &ArgMatches) -> Result<OsString, clap::Error> {
    let missing_value_error =
        || clap::Error::raw(clap::error::ErrorKind::TooFewValues, "missing value");
    let mut raw_values = verb_cmd.get_raw("value").ok_or_else(missing_value_error)?;
    let raw = raw_values.next().ok_or_else(missing_value_error)?;

    if raw_values.next().is_some() {
        return Err(clap::Error::raw(
            clap::error::ErrorKind::TooManyValues,
            "too many values",
        ));
    }

    Ok(raw.to_owned())
}

fn get_verb_for_list(
    verb_name: &str,
    verb_cmd: &ArgMatches,
    list_desc: &ListDescription,
) -> Result<ListVerb, clap::Error> {
    match (verb_name, list_desc.inner_desc.as_ref()) {
        ("remove", _) => {
            let Some(key) = get_key(verb_cmd, list_desc) else {
                return Err(clap::Error::raw(
                    clap::error::ErrorKind::TooFewValues,
                    "missing key",
                ));
            };
            Ok(ListVerb::Remove { key })
        }
        ("empty", _) => Ok(ListVerb::Empty),

        ("append", Description::Field(_)) => {
            let key = if list_desc.append_requires_key {
                let Some(key) = get_key(verb_cmd, list_desc) else {
                    return Err(clap::Error::raw(
                        clap::error::ErrorKind::TooFewValues,
                        "missing key",
                    ));
                };
                Some(key)
            } else {
                None
            };
            let new_value = get_value(verb_cmd)?;
            Ok(ListVerb::Append {
                key,
                new_value: new_value.into(),
            })
        }
        ("update", Description::Field(_)) => {
            let Some(key) = get_key(verb_cmd, list_desc) else {
                return Err(clap::Error::raw(
                    clap::error::ErrorKind::TooFewValues,
                    "missing key",
                ));
            };
            let updated_value = get_value(verb_cmd)?;
            Ok(ListVerb::Update {
                key,
                updated_value: updated_value.into(),
            })
        }

        ("append", _) => {
            let key = if list_desc.append_requires_key {
                let Some(key) = get_key(verb_cmd, list_desc) else {
                    return Err(clap::Error::raw(
                        clap::error::ErrorKind::TooFewValues,
                        "missing key",
                    ));
                };
                Some(key)
            } else {
                None
            };
            let (path, verb) = get_verb_from_cmd(verb_cmd, list_desc.inner_desc.as_ref())?;
            let path: Vec<String> = path.map(|p| p.to_string()).collect();
            let verb = Box::new(verb);
            Ok(ListVerb::Append {
                key,
                new_value: ListValue::WithPath(path, verb),
            })
        }
        ("update", _) => {
            let Some(key) = get_key(verb_cmd, list_desc) else {
                return Err(clap::Error::raw(
                    clap::error::ErrorKind::TooFewValues,
                    "missing key",
                ));
            };
            let (path, verb) = get_verb_from_cmd(verb_cmd, list_desc.inner_desc.as_ref())?;
            let path: Vec<String> = path.map(|p| p.to_string()).collect();
            let verb = Box::new(verb);
            Ok(ListVerb::Update {
                key,
                updated_value: ListValue::WithPath(path, verb),
            })
        }

        (verb, _) => Err(clap::Error::raw(
            clap::error::ErrorKind::InvalidSubcommand,
            format!("invalid verb ({verb}) for {list_desc:?}"),
        )),
    }
}

fn get_key(arg_matches: &ArgMatches, field_desc: &ListDescription) -> Option<ListKey> {
    match field_desc.key_type {
        ListKeyType::String => arg_matches
            .get_one::<String>("key")
            .map(|key| ListKey::Stringy(key.clone())),
        ListKeyType::Signed => arg_matches
            .get_one::<i64>("key")
            .map(|key| ListKey::Signed(*key)),
        ListKeyType::Unsigned => arg_matches
            .get_one::<u64>("key")
            .map(|key| ListKey::Unsigned(*key)),
    }
}