use anyhow::{Context, Result};
use ctl_core::formatdoc;
use serde::{Deserialize, Serialize};
const OPEN: &str = "<!-- verctl:stop ";
const CLOSE: &str = " -->";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct Stop {
pub package: String,
pub version: String,
pub cause: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub retry_after: Option<String>,
}
impl Stop {
#[must_use]
pub fn new(package: &str, version: &str, cause: &str, retry_after: Option<String>) -> Self {
Self {
package: package.to_owned(),
version: version.to_owned(),
cause: cause.to_owned(),
retry_after,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
#[serde(tag = "state", rename_all = "kebab-case")]
pub enum Record {
#[default]
Absent,
Read(Stop),
Unreadable,
}
#[must_use]
pub fn read(body: &str) -> Record {
let Some(line) = body.lines().map(str::trim).find(|line| is_record(line)) else {
return Record::Absent;
};
line.strip_prefix(OPEN)
.and_then(|rest| rest.strip_suffix(CLOSE))
.and_then(|json| serde_json::from_str(json).ok())
.map_or(Record::Unreadable, Record::Read)
}
pub fn write(body: &str, stop: &Stop) -> Result<String> {
let json = serde_json::to_string(stop)
.context("encode the stop record")?
.replace("-->", "--\\u003e");
let notes = body
.lines()
.filter(|line| !is_record(line.trim()))
.collect::<Vec<_>>()
.join("\n");
Ok(formatdoc! {"
{notes}
{OPEN}{json}{CLOSE}
",
notes = notes.trim_end(),
})
}
fn is_record(line: &str) -> bool {
line.starts_with(OPEN.trim_end())
}
#[cfg(test)]
mod tests {
use indoc::indoc;
use super::{Record, Stop, read, write};
fn limited() -> Stop {
Stop::new(
"shrub-process",
"0.7.0",
"429 Too Many Requests",
Some("Fri, 13 Nov 2026 19:00:00 GMT".to_owned()),
)
}
#[test]
fn a_record_reads_back_as_written() {
let notes = indoc! {"
## shrub 0.7.0
- Notes.
"};
let body = write(notes, &limited()).unwrap();
assert_eq!(
body,
indoc! {r#"
## shrub 0.7.0
- Notes.
<!-- verctl:stop {"package":"shrub-process","version":"0.7.0","cause":"429 Too Many Requests","retry_after":"Fri, 13 Nov 2026 19:00:00 GMT"} -->
"#}
);
assert_eq!(read(&body), Record::Read(limited()));
}
#[test]
fn an_unknown_field_is_unreadable() {
let body = indoc! {r#"
- Notes.
<!-- verctl:stop {"package":"a","version":"1.0.0","cause":"x","extra":true} -->
"#};
assert_eq!(read(body), Record::Unreadable);
}
#[test]
fn a_comment_that_is_not_json_is_unreadable() {
assert_eq!(read("<!-- verctl:stop not json -->"), Record::Unreadable);
assert_eq!(read("<!-- verctl:stop"), Record::Unreadable);
}
#[test]
fn a_body_with_no_record_has_none() {
let body = indoc! {"
## demo 1.0.0
<!-- a comment -->
"};
assert_eq!(read(body), Record::Absent);
}
#[test]
fn writing_a_record_twice_keeps_one() {
let first = write("- Notes.", &Stop::new("a", "1.0.0", "exit 1", None)).unwrap();
let second = write(&first, &limited()).unwrap();
assert_eq!(second.matches("verctl:stop").count(), 1, "{second}");
assert!(second.starts_with("- Notes."), "{second}");
assert_eq!(read(&second), Record::Read(limited()));
}
#[test]
fn a_cause_cannot_close_the_comment() {
let stop = Stop::new("a", "1.0.0", "saw --> here", None);
let body = write("", &stop).unwrap();
assert_eq!(body.matches("-->").count(), 1, "{body}");
assert_eq!(read(&body), Record::Read(stop));
}
}