(74904dc) Let a ticket field be cleared back to unset, and bump to 0.18.0
`set --clear <field>` is the one way to take a value away, repeatable, with the field as a `ValueEnum` value: an unknown one is a usage error listing the six that are not, and the shells complete them for free. Not a per-field `--clear-due`, which is a flag per field to keep in step with the one it undoes, and not an empty value, which is already a paragraph break for a section and would make an accidentally empty variable a silent delete. What goes back is what `add` writes for a field it was never given, through the new `NOT_SET` and `NONE` constants and each section's placeholder, so a cleared ticket and one filed without the field read the same. Sections are in scope and go back through `clear_section_body` rather than `set_section_body`: the latter runs its items through `Section::render`, which would have written `- [ ] _No acceptance criteria provided._`. Owner is `chown --clear <ticket>`, so the field keeps its one writer. The flag carries the ticket because the ticket is chown's second positional and a bare flag would land it in the user's slot for the handler to read back out, which is hand-parsing what clap can state. It writes `_Not set_` rather than the git user: `add` falls back to the git user because it is filing something new, and an owner taken away says nobody owns this. The empty-value error split in two rather than being reworded. `non_empty` says only that the value cannot be empty, for the arguments with no empty form; `clearable` names `--clear`, for the arguments whose field has one. One message for both would have lied about `ls --tag`. Replacing and clearing one field in a single call is refused rather than resolved by whichever was applied last, and cleared fields are reported as `Cleared due on '...'` beside `Updated priority on '...'`, since one list would say `Updated` of a field that was emptied. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
175c1d016d
commit
cc5f9b8a46
+209
-3
@@ -936,9 +936,22 @@ fn render_metadata_table(lines: &[&str], colored: bool, campaign: &str) -> Strin
|
||||
result
|
||||
}
|
||||
|
||||
/// What a scalar metadata field holds when it holds nothing.
|
||||
///
|
||||
/// A constant rather than a literal per site, because it is now written on purpose:
|
||||
/// clearing a field puts it back, and the value it goes back to has to be the one `add`
|
||||
/// wrote, or a cleared ticket and a never-set one would differ in the file.
|
||||
pub const NOT_SET: &str = "_Not set_";
|
||||
|
||||
/// The same for Tags, which is a list.
|
||||
///
|
||||
/// A list that holds nothing reads as none rather than as not set, which is the
|
||||
/// distinction `add` already makes and clearing has to keep.
|
||||
pub const NONE: &str = "_None_";
|
||||
|
||||
pub fn is_unknown_value(value: &str) -> bool {
|
||||
let trimmed = value.trim();
|
||||
trimmed == "_None_" || trimmed == "_Not set_" || trimmed == "-None-" || trimmed == "-Not set-"
|
||||
trimmed == NONE || trimmed == NOT_SET || trimmed == "-None-" || trimmed == "-Not set-"
|
||||
}
|
||||
|
||||
/// Replace `**bold**` with colour, or leave the line exactly as it is.
|
||||
@@ -1017,7 +1030,7 @@ pub fn parse_ticket_field(content: &str, field: &str) -> String {
|
||||
metadata_lines(content)
|
||||
.find_map(|line| metadata_field_body(line).strip_prefix(&prefix))
|
||||
.map(|value| value.trim().to_string())
|
||||
.unwrap_or_else(|| "_Not set_".to_string())
|
||||
.unwrap_or_else(|| NOT_SET.to_string())
|
||||
}
|
||||
|
||||
fn is_status_line(line: &str) -> bool {
|
||||
@@ -1371,6 +1384,12 @@ pub struct Section {
|
||||
/// appending to an empty section replaces the placeholder rather than following it.
|
||||
pub placeholder: &'static str,
|
||||
pub kind: SectionKind,
|
||||
/// The `--clear` value that empties this section, where `set` can reach it at all.
|
||||
///
|
||||
/// `None` for Relations, for the same reason it is absent from `section_args`: its
|
||||
/// only writer is `relate`, so `set` has no way to fill it and no business emptying
|
||||
/// it either.
|
||||
pub clear: Option<Clearable>,
|
||||
}
|
||||
|
||||
pub const DESCRIPTION: Section = Section {
|
||||
@@ -1378,6 +1397,7 @@ pub const DESCRIPTION: Section = Section {
|
||||
label: "description",
|
||||
placeholder: "_No description provided._",
|
||||
kind: SectionKind::Prose,
|
||||
clear: Some(Clearable::Description),
|
||||
};
|
||||
|
||||
pub const ACCEPTANCE: Section = Section {
|
||||
@@ -1385,6 +1405,7 @@ pub const ACCEPTANCE: Section = Section {
|
||||
label: "acceptance criteria",
|
||||
placeholder: "_No acceptance criteria provided._",
|
||||
kind: SectionKind::Checklist,
|
||||
clear: Some(Clearable::Acceptance),
|
||||
};
|
||||
|
||||
pub const PLAN: Section = Section {
|
||||
@@ -1392,6 +1413,7 @@ pub const PLAN: Section = Section {
|
||||
label: "implementation plan",
|
||||
placeholder: "_No implementation plan provided._",
|
||||
kind: SectionKind::Numbered,
|
||||
clear: Some(Clearable::Plan),
|
||||
};
|
||||
|
||||
/// The edges to other tickets, one per line.
|
||||
@@ -1405,6 +1427,7 @@ pub const RELATIONS: Section = Section {
|
||||
label: "relations",
|
||||
placeholder: "_No relations._",
|
||||
kind: SectionKind::Bullets,
|
||||
clear: None,
|
||||
};
|
||||
|
||||
impl Section {
|
||||
@@ -1438,6 +1461,108 @@ impl Section {
|
||||
}
|
||||
}
|
||||
|
||||
/// A field `set --clear` can return to the state a new ticket is born in.
|
||||
///
|
||||
/// The names are the flags they undo, so `--clear due` reads as the opposite of
|
||||
/// `--due`. Three fields a ticket carries are deliberately absent:
|
||||
///
|
||||
/// - **Title** has no unset state. It addresses the ticket, it names the file through
|
||||
/// its slug, and `add` requires it, so there is nothing to go back to.
|
||||
/// - **Owner** is `chown`'s, and clearing it through `set` would give the field a
|
||||
/// second writer, which is the rule the missing `set --owner` exists to keep.
|
||||
/// - **Status**, **Campaign** and the three stamps belong to `mv` and `assign` for the
|
||||
/// same reason.
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
|
||||
pub enum Clearable {
|
||||
Due,
|
||||
Tags,
|
||||
Priority,
|
||||
Description,
|
||||
Acceptance,
|
||||
Plan,
|
||||
}
|
||||
|
||||
/// What clearing a field writes back.
|
||||
pub enum Unset {
|
||||
/// A metadata line, back to the sentinel `add` writes when the flag is omitted.
|
||||
Field {
|
||||
field: &'static str,
|
||||
sentinel: &'static str,
|
||||
},
|
||||
/// A section body, back to the placeholder `add` writes for an empty one.
|
||||
Section(&'static Section),
|
||||
}
|
||||
|
||||
impl Clearable {
|
||||
/// Every field that can be cleared.
|
||||
///
|
||||
/// `#[cfg(test)]` because nothing in the binary walks the list: clap emits the
|
||||
/// values for `--clear` from the `ValueEnum` itself, so the only caller is the suite
|
||||
/// that checks each one against the ticket it empties.
|
||||
#[cfg(test)]
|
||||
pub const ALL: [Clearable; 6] = [
|
||||
Clearable::Due,
|
||||
Clearable::Tags,
|
||||
Clearable::Priority,
|
||||
Clearable::Description,
|
||||
Clearable::Acceptance,
|
||||
Clearable::Plan,
|
||||
];
|
||||
|
||||
/// What clearing the field amounts to.
|
||||
///
|
||||
/// The sentinels are [`NOT_SET`] and [`NONE`] rather than strings written out here,
|
||||
/// so a cleared field is byte-identical to one `add` left alone. That is the whole
|
||||
/// promise of the command: a ticket that has been cleared and one that was never
|
||||
/// given the field read the same.
|
||||
pub fn unset(self) -> Unset {
|
||||
match self {
|
||||
Clearable::Due => Unset::Field {
|
||||
field: "Due",
|
||||
sentinel: NOT_SET,
|
||||
},
|
||||
Clearable::Tags => Unset::Field {
|
||||
field: "Tags",
|
||||
sentinel: NONE,
|
||||
},
|
||||
Clearable::Priority => Unset::Field {
|
||||
field: "Priority",
|
||||
sentinel: NOT_SET,
|
||||
},
|
||||
Clearable::Description => Unset::Section(&DESCRIPTION),
|
||||
Clearable::Acceptance => Unset::Section(&ACCEPTANCE),
|
||||
Clearable::Plan => Unset::Section(&PLAN),
|
||||
}
|
||||
}
|
||||
|
||||
/// The value the flag takes, as clap spells it.
|
||||
///
|
||||
/// Written out rather than read off `to_possible_value`, because it is used in error
|
||||
/// messages that tell a caller what to type and a `&'static str` is what those want.
|
||||
/// `the_clear_values_are_the_names_clap_accepts` keeps the two in step.
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Clearable::Due => "due",
|
||||
Clearable::Tags => "tags",
|
||||
Clearable::Priority => "priority",
|
||||
Clearable::Description => "description",
|
||||
Clearable::Acceptance => "acceptance",
|
||||
Clearable::Plan => "plan",
|
||||
}
|
||||
}
|
||||
|
||||
/// How the change is named in output.
|
||||
///
|
||||
/// The label `set` already prints when it *writes* the field, so a cleared field is
|
||||
/// named the way a replaced one is rather than in the flag's own spelling.
|
||||
pub fn label(self) -> &'static str {
|
||||
match self.unset() {
|
||||
Unset::Field { .. } => self.name(),
|
||||
Unset::Section(section) => section.label,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the line opens with `N. `, the shape a numbered step is stored in.
|
||||
fn is_numbered_item(line: &str) -> bool {
|
||||
let digits = line.chars().take_while(|c| c.is_ascii_digit()).count();
|
||||
@@ -1481,12 +1606,32 @@ fn join_lines(lines: &[String]) -> String {
|
||||
|
||||
/// Replace a section's body with `items`, leaving every line outside it untouched.
|
||||
pub fn set_section_body(content: &str, section: &Section, items: &[String]) -> Result<String> {
|
||||
replace_section_body(content, section, section.render(items, 1))
|
||||
}
|
||||
|
||||
/// Return a section to the placeholder `add` writes for an empty one.
|
||||
///
|
||||
/// Not [`set_section_body`] with the placeholder as its one item: the placeholder is a
|
||||
/// line of the format rather than content, and `render` would dress it as `- [ ] _No
|
||||
/// acceptance criteria provided._` in every section but the description. It is the same
|
||||
/// line `append` already treats as no content, so a cleared section can be appended to
|
||||
/// without the placeholder being left above what follows it.
|
||||
pub fn clear_section_body(content: &str, section: &Section) -> Result<String> {
|
||||
replace_section_body(content, section, vec![section.placeholder.to_string()])
|
||||
}
|
||||
|
||||
/// The line surgery both of the above are, given a body that is already rendered.
|
||||
fn replace_section_body(
|
||||
content: &str,
|
||||
section: &Section,
|
||||
body_lines: Vec<String>,
|
||||
) -> Result<String> {
|
||||
let lines: Vec<&str> = content.lines().collect();
|
||||
let body = section_body(&lines, section.heading)
|
||||
.ok_or_else(|| anyhow!("Ticket has no {} section", section.label))?;
|
||||
|
||||
let mut out: Vec<String> = lines[..body.start].iter().map(|l| l.to_string()).collect();
|
||||
out.extend(section.render(items, 1));
|
||||
out.extend(body_lines);
|
||||
// One blank line before whatever section follows, matching how `add` writes it.
|
||||
if body.end < lines.len() {
|
||||
out.push(String::new());
|
||||
@@ -3791,6 +3936,67 @@ mod tests {
|
||||
assert!(parse_relation("- blocked by").unwrap_err().to_string().contains("no target"));
|
||||
}
|
||||
|
||||
/// The two directions of the pairing have to agree, or `--clear acceptance` would
|
||||
/// empty one section while the error message about an empty value named another.
|
||||
#[test]
|
||||
fn every_clearable_section_points_back_at_the_field_that_clears_it() {
|
||||
for field in Clearable::ALL {
|
||||
if let Unset::Section(section) = field.unset() {
|
||||
assert_eq!(
|
||||
section.clear,
|
||||
Some(field),
|
||||
"{} does not point back at --clear {}",
|
||||
section.heading,
|
||||
field.name()
|
||||
);
|
||||
}
|
||||
}
|
||||
assert_eq!(RELATIONS.clear, None, "set must not be able to empty Relations");
|
||||
}
|
||||
|
||||
/// The names are written out for the error messages, so this is what keeps them the
|
||||
/// names clap actually accepts.
|
||||
#[test]
|
||||
fn the_clear_values_are_the_names_clap_accepts() {
|
||||
for field in Clearable::ALL {
|
||||
let possible = clap::ValueEnum::to_possible_value(&field).expect("not hidden");
|
||||
assert_eq!(possible.get_name(), field.name());
|
||||
}
|
||||
}
|
||||
|
||||
/// The sentinel a cleared field goes back to is the one `add` writes for a field it
|
||||
/// was never given, which is the whole promise of the flag.
|
||||
#[test]
|
||||
fn a_cleared_scalar_holds_the_sentinel_add_writes() {
|
||||
for field in Clearable::ALL {
|
||||
let Unset::Field { field: name, sentinel } = field.unset() else {
|
||||
continue;
|
||||
};
|
||||
let expected = match name {
|
||||
"Tags" => NONE,
|
||||
_ => NOT_SET,
|
||||
};
|
||||
assert_eq!(sentinel, expected, "{name}");
|
||||
assert!(is_unknown_value(sentinel), "{name} clears to something readable as set");
|
||||
}
|
||||
}
|
||||
|
||||
/// The placeholder goes in as a line of the format, not as an item: dressed by
|
||||
/// `render` it would read `- [ ] _No acceptance criteria provided._`.
|
||||
#[test]
|
||||
fn clearing_a_section_writes_the_bare_placeholder() {
|
||||
let content = "# T\n\n## Acceptance Criteria\n- [x] Done\n- [ ] Not\n\n## Metadata\n\n- **ID**: x\n";
|
||||
let cleared = clear_section_body(content, &ACCEPTANCE).unwrap();
|
||||
assert!(
|
||||
cleared.contains(&format!("## Acceptance Criteria\n{}\n", ACCEPTANCE.placeholder)),
|
||||
"{cleared}"
|
||||
);
|
||||
// Everything outside the section is where it was.
|
||||
assert!(cleared.contains("- **ID**: x"), "{cleared}");
|
||||
// And a second clear is the same string, which is what makes the no-op free.
|
||||
assert_eq!(clear_section_body(&cleared, &ACCEPTANCE).unwrap(), cleared);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_placeholder_is_no_relations_rather_than_a_bad_line() {
|
||||
let content = format!(
|
||||
|
||||
Reference in New Issue
Block a user