(5a46b59) Say campaign where a campaign write fails, and bump to 0.32.7
This commit is contained in:
+39
-11
@@ -1514,6 +1514,17 @@ const METADATA_ORDER: [&str; 13] = [
|
||||
"Status",
|
||||
];
|
||||
|
||||
/// What is being written, for the messages a failure carries.
|
||||
///
|
||||
/// A `&str` naming the thing rather than two copies of every message that names its
|
||||
/// object: a campaign file is a ticket file in every way that matters here,
|
||||
/// `<uuid>_<slug>.md` in a directory, and the only difference is the word a failure has
|
||||
/// to use. Every message below puts it mid-sentence so there is no capital to case.
|
||||
/// Lived in the handlers until the writers here started carrying the noun too; both
|
||||
/// sides read these through one definition so the words cannot drift apart.
|
||||
pub const TICKET: &str = "ticket";
|
||||
pub const CAMPAIGN: &str = "campaign";
|
||||
|
||||
/// Rewrite a metadata field, adding the line when the file has no such field yet.
|
||||
///
|
||||
/// [`set_metadata_field`] refuses a field that is not already there, which is right for
|
||||
@@ -1526,6 +1537,7 @@ pub fn set_or_insert_metadata_field(
|
||||
content: &str,
|
||||
field: &str,
|
||||
value: &str,
|
||||
noun: &str,
|
||||
title: &str,
|
||||
) -> Result<String> {
|
||||
let prefix = format!("**{}**:", field);
|
||||
@@ -1534,7 +1546,7 @@ pub fn set_or_insert_metadata_field(
|
||||
metadata_field_body(line).starts_with(&prefix)
|
||||
}) {
|
||||
Some(updated) => Ok(updated),
|
||||
None => insert_metadata_field(content, field, value, title),
|
||||
None => insert_metadata_field(content, field, value, noun, title),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1543,14 +1555,26 @@ pub fn set_or_insert_metadata_field(
|
||||
/// A file with no Metadata section is an error rather than an insertion. There is no
|
||||
/// format to guess at there, and per 641e3d5 such a file has no fields at all, so
|
||||
/// writing one above the prose would be inventing a shape nothing else reads.
|
||||
fn insert_metadata_field(content: &str, field: &str, value: &str, title: &str) -> Result<String> {
|
||||
///
|
||||
/// The noun travels with the call rather than being spelled here, because both a ticket
|
||||
/// and a campaign reach this: `campaign set --codename` inserts into a campaign, and
|
||||
/// "Ticket 'v1 release' has no Metadata section" would be wrong about the one file it
|
||||
/// names.
|
||||
fn insert_metadata_field(
|
||||
content: &str,
|
||||
field: &str,
|
||||
value: &str,
|
||||
noun: &str,
|
||||
title: &str,
|
||||
) -> Result<String> {
|
||||
let lines: Vec<&str> = content.lines().collect();
|
||||
let heading = lines
|
||||
.iter()
|
||||
.position(|line| is_metadata_heading(line))
|
||||
.ok_or_else(|| {
|
||||
anyhow!(
|
||||
"Ticket '{}' has no Metadata section, so its {} field cannot be written",
|
||||
"There is no Metadata section on {} '{}', so its {} field cannot be written",
|
||||
noun,
|
||||
title,
|
||||
field
|
||||
)
|
||||
@@ -1640,7 +1664,7 @@ pub fn set_status_stamp(content: &str, target: Status, now: &str, title: &str) -
|
||||
match target.stamp_field() {
|
||||
// Inserted on demand, the way Campaign is: every ticket written before these
|
||||
// fields existed carries none of them.
|
||||
Some(field) => set_or_insert_metadata_field(&content, field, now, title),
|
||||
Some(field) => set_or_insert_metadata_field(&content, field, now, TICKET, title),
|
||||
None => Ok(content),
|
||||
}
|
||||
}
|
||||
@@ -1648,7 +1672,10 @@ pub fn set_status_stamp(content: &str, target: Status, now: &str, title: &str) -
|
||||
/// Rewrite the title heading, the first `# ` line in the file.
|
||||
///
|
||||
/// `## ` headings do not match: the second character has to be a space.
|
||||
pub fn set_title(content: &str, title: &str) -> Result<String> {
|
||||
///
|
||||
/// The noun travels with the call for the same reason [`insert_metadata_field`] carries
|
||||
/// one: `campaign set --title` rewrites a campaign's heading too.
|
||||
pub fn set_title(content: &str, title: &str, noun: &str) -> Result<String> {
|
||||
let mut updated = String::with_capacity(content.len());
|
||||
let mut found = false;
|
||||
|
||||
@@ -1664,7 +1691,7 @@ pub fn set_title(content: &str, title: &str) -> Result<String> {
|
||||
}
|
||||
|
||||
if !found {
|
||||
return Err(anyhow!("Ticket '{}' has no title heading", title));
|
||||
return Err(anyhow!("There is no title heading on {} '{}'", noun, title));
|
||||
}
|
||||
|
||||
Ok(updated)
|
||||
@@ -4207,7 +4234,7 @@ mod tests {
|
||||
let content = with_metadata("- **ID**: abc\n- **Owner**: alice\n- **Status**: backlog\n");
|
||||
|
||||
let updated =
|
||||
set_or_insert_metadata_field(&content, CAMPAIGN_FIELD, "c-uuid", "T").unwrap();
|
||||
set_or_insert_metadata_field(&content, CAMPAIGN_FIELD, "c-uuid", TICKET, "T").unwrap();
|
||||
|
||||
// Against METADATA_ORDER, so Status stays last where every file keeps it.
|
||||
assert!(
|
||||
@@ -4224,7 +4251,7 @@ mod tests {
|
||||
let content = with_metadata("- **ID**: abc\n- **Owner**: alice\n\n## Notes\nAfter.\n");
|
||||
|
||||
let updated =
|
||||
set_or_insert_metadata_field(&content, CAMPAIGN_FIELD, "c-uuid", "T").unwrap();
|
||||
set_or_insert_metadata_field(&content, CAMPAIGN_FIELD, "c-uuid", TICKET, "T").unwrap();
|
||||
|
||||
assert!(
|
||||
updated.contains("- **Owner**: alice\n- **Campaign**: c-uuid\n\n## Notes\nAfter.\n"),
|
||||
@@ -4240,7 +4267,7 @@ mod tests {
|
||||
with_metadata("- **ID**: abc\n- **Campaign**: first\n- **Status**: backlog\n");
|
||||
|
||||
let updated =
|
||||
set_or_insert_metadata_field(&content, CAMPAIGN_FIELD, "second", "T").unwrap();
|
||||
set_or_insert_metadata_field(&content, CAMPAIGN_FIELD, "second", TICKET, "T").unwrap();
|
||||
|
||||
assert_eq!(updated.matches("**Campaign**:").count(), 1, "{updated}");
|
||||
assert!(updated.contains("- **Campaign**: second"), "{updated}");
|
||||
@@ -4253,7 +4280,8 @@ mod tests {
|
||||
let content = "# T\n\n## Description\n- **Campaign**: not a field\n\n\
|
||||
## Metadata\n\n- **ID**: abc\n- **Status**: backlog\n";
|
||||
|
||||
let updated = set_or_insert_metadata_field(content, CAMPAIGN_FIELD, "c-uuid", "T").unwrap();
|
||||
let updated =
|
||||
set_or_insert_metadata_field(content, CAMPAIGN_FIELD, "c-uuid", TICKET, "T").unwrap();
|
||||
|
||||
assert!(updated.contains("- **Campaign**: not a field"), "{updated}");
|
||||
assert!(
|
||||
@@ -4268,7 +4296,7 @@ mod tests {
|
||||
fn a_file_with_no_metadata_section_is_an_error_rather_than_an_insertion() {
|
||||
let content = "# T\n\n## Description\nBody.\n";
|
||||
|
||||
let error = set_or_insert_metadata_field(content, CAMPAIGN_FIELD, "c-uuid", "T")
|
||||
let error = set_or_insert_metadata_field(content, CAMPAIGN_FIELD, "c-uuid", TICKET, "T")
|
||||
.expect_err("a file with no Metadata section gained a field");
|
||||
|
||||
assert!(error.to_string().contains("no Metadata section"), "{error}");
|
||||
|
||||
Reference in New Issue
Block a user