Skip to main content

cli/transaction/
split_tag.rs

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