1
//! The `--split-tag N:NAME=VALUE[:SIDE]` option text.
2

            
3
use forms::{LINK_CELL, Row};
4

            
5
pub(super) const SPLIT_TAG_HELP: &str = "Tag the Nth split (1-based, counting the --row options) \
6
    as NAME=VALUE, optionally for one leg: N:NAME=VALUE:from or :to. Name and value split on the \
7
    first '='. A value containing ':' must end in ':' (both legs) or ':from' / ':to'; the text \
8
    after the value's last ':' is always the leg. Repeat for more tags; on edit any --split-tag \
9
    replaces all loaded split tags.";
10

            
11
21
pub(super) fn parse_split_tag(spec: &str) -> Result<Row, String> {
12
21
    let (position, rest) = spec.split_once(':').ok_or("expected N:NAME=VALUE[:SIDE]")?;
13
20
    position
14
20
        .parse::<usize>()
15
1
        .map_err(|_| format!("'{position}' is not a row number"))?;
16
19
    let (name, tail) = rest.split_once('=').ok_or("expected NAME=VALUE after N:")?;
17
18
    if name.is_empty() {
18
1
        return Err("the tag name is empty".to_string());
19
17
    }
20
17
    let (value, side) = match tail.rsplit_once(':') {
21
5
        Some((value, side @ ("from" | "to" | ""))) => (value, side),
22
        Some((_, side)) => {
23
1
            return Err(format!("unknown side '{side}' (expected from, to or none)"));
24
        }
25
11
        None => (tail, ""),
26
    };
27
16
    let mut row = Row::from([
28
16
        (LINK_CELL.to_string(), position.to_string()),
29
16
        ("name".to_string(), name.to_string()),
30
16
        ("value".to_string(), value.to_string()),
31
16
    ]);
32
16
    if !side.is_empty() {
33
4
        row.insert("side".to_string(), side.to_string());
34
12
    }
35
16
    Ok(row)
36
21
}