Lines
100 %
Functions
50 %
Branches
//! The `--split-tag N:NAME=VALUE[:SIDE]` option text.
use forms::{LINK_CELL, Row};
pub(super) const SPLIT_TAG_HELP: &str = "Tag the Nth split (1-based, counting the --row options) \
as NAME=VALUE, optionally for one leg: N:NAME=VALUE:from or :to. Name and value split on the \
first '='. A value containing ':' must end in ':' (both legs) or ':from' / ':to'; the text \
after the value's last ':' is always the leg. Repeat for more tags; on edit any --split-tag \
replaces all loaded split tags.";
pub(super) fn parse_split_tag(spec: &str) -> Result<Row, String> {
let (position, rest) = spec.split_once(':').ok_or("expected N:NAME=VALUE[:SIDE]")?;
position
.parse::<usize>()
.map_err(|_| format!("'{position}' is not a row number"))?;
let (name, tail) = rest.split_once('=').ok_or("expected NAME=VALUE after N:")?;
if name.is_empty() {
return Err("the tag name is empty".to_string());
}
let (value, side) = match tail.rsplit_once(':') {
Some((value, side @ ("from" | "to" | ""))) => (value, side),
Some((_, side)) => {
return Err(format!("unknown side '{side}' (expected from, to or none)"));
None => (tail, ""),
};
let mut row = Row::from([
(LINK_CELL.to_string(), position.to_string()),
("name".to_string(), name.to_string()),
("value".to_string(), value.to_string()),
]);
if !side.is_empty() {
row.insert("side".to_string(), side.to_string());
Ok(row)