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

            
5
use clap::{Arg, ArgAction};
6
use forms::{Field, Kind, SPLIT_TAGS_KEY, SPLITS_KEY as SPLITS};
7

            
8
const TAG_CELLS: [&str; 3] = ["name", "value", "side"];
9

            
10
7997
pub(super) fn is_free_rows(field: &Field) -> bool {
11
7997
    matches!(field.kind, Kind::Rows { .. }) && !field.is_nested() && field.key != SPLITS
12
7997
}
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.
16
7900
pub(super) fn is_nested_tags(field: &Field) -> bool {
17
7900
    match field.kind {
18
        Kind::Rows { fields, .. }
19
2152
            if field.key == SPLIT_TAGS_KEY && field.nested_under() == Some(SPLITS) =>
20
        {
21
896
            TAG_CELLS
22
896
                .iter()
23
8957
                .all(|cell| fields.iter().any(|f| f.key == *cell))
24
        }
25
7004
        _ => false,
26
    }
27
7900
}
28

            
29
/// Nested fields the commands carry from the loaded form without an option.
30
7062
pub(super) fn is_carried(field: &Field) -> bool {
31
7062
    field.is_nested() && !is_nested_tags(field)
32
7062
}
33

            
34
7060
pub(super) fn has_clear(field: &Field) -> bool {
35
7060
    is_free_rows(field) || is_nested_tags(field)
36
7060
}
37

            
38
/// The row option's long name: `row` for splits, else the key minus one trailing `s`.
39
1217
pub(super) fn row_long(field: &Field) -> &'static str {
40
1217
    match field.key {
41
286
        SPLITS => "row",
42
931
        key => key.strip_suffix('s').unwrap_or(key),
43
    }
44
1217
}
45

            
46
995
pub(super) fn clear_id(field: &Field) -> String {
47
995
    format!("clear-{}", field.key)
48
995
}
49

            
50
931
pub(super) fn clear_arg(field: &Field) -> Arg {
51
931
    let id = clear_id(field);
52
931
    Arg::new(id.clone())
53
931
        .long(id)
54
931
        .action(ArgAction::SetTrue)
55
931
        .conflicts_with(field.key)
56
931
        .help(format!("Send an empty {} list", field.label))
57
931
}