Skip to main content

cli/transaction/
rows_args.rs

1//! Option names for `rows` fields: `splits` keeps `--row`, others get
2//! `--<singular>` and `--clear-<key>`. A nested tag field is optioned too; any
3//! other nested field has no option.
4
5use clap::{Arg, ArgAction};
6use forms::{Field, Kind, SPLIT_TAGS_KEY, SPLITS_KEY as SPLITS};
7
8const TAG_CELLS: [&str; 3] = ["name", "value", "side"];
9
10pub(super) fn is_free_rows(field: &Field) -> bool {
11    matches!(field.kind, Kind::Rows { .. }) && !field.is_nested() && field.key != SPLITS
12}
13
14/// The split tags field: the one nested field the linker handles, whose
15/// cells are name, value and the leg they apply to.
16pub(super) fn is_nested_tags(field: &Field) -> bool {
17    match field.kind {
18        Kind::Rows { fields, .. }
19            if field.key == SPLIT_TAGS_KEY && field.nested_under() == Some(SPLITS) =>
20        {
21            TAG_CELLS
22                .iter()
23                .all(|cell| fields.iter().any(|f| f.key == *cell))
24        }
25        _ => false,
26    }
27}
28
29/// Nested fields the commands carry from the loaded form without an option.
30pub(super) fn is_carried(field: &Field) -> bool {
31    field.is_nested() && !is_nested_tags(field)
32}
33
34pub(super) fn has_clear(field: &Field) -> bool {
35    is_free_rows(field) || is_nested_tags(field)
36}
37
38/// The row option's long name: `row` for splits, else the key minus one trailing `s`.
39pub(super) fn row_long(field: &Field) -> &'static str {
40    match field.key {
41        SPLITS => "row",
42        key => key.strip_suffix('s').unwrap_or(key),
43    }
44}
45
46pub(super) fn clear_id(field: &Field) -> String {
47    format!("clear-{}", field.key)
48}
49
50pub(super) fn clear_arg(field: &Field) -> Arg {
51    let id = clear_id(field);
52    Arg::new(id.clone())
53        .long(id)
54        .action(ArgAction::SetTrue)
55        .conflicts_with(field.key)
56        .help(format!("Send an empty {} list", field.label))
57}