verctl_core/runtime/stop.rs
1//! The stop record a failed `release` run writes into its draft Release.
2//!
3//! When a registry upload fails, the run appends one HTML comment line to the
4//! draft's body, so the rendered page does not show it:
5//!
6//! ```text
7//! <!-- verctl:stop {"package":"shrub-process","version":"0.7.0","cause":"429 Too Many Requests","retry_after":"Fri, 13 Nov 2026 19:00:00 GMT"} -->
8//! ```
9//!
10//! `status` reads it from the draft it already looks up. Anyone who can edit
11//! the draft can edit the record, so it is display only: no decision reads
12//! it, and what is live always comes from the registry. Publishing the draft
13//! writes the notes alone, which drops the record.
14
15use anyhow::{Context, Result};
16use ctl_core::formatdoc;
17use serde::{Deserialize, Serialize};
18
19/// What opens a stop record's line.
20const OPEN: &str = "<!-- verctl:stop ";
21
22/// What closes a stop record's line.
23const CLOSE: &str = " -->";
24
25/// Why the last `release` run stopped, as it reported it.
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27#[serde(deny_unknown_fields)]
28#[non_exhaustive]
29pub struct Stop {
30 /// The package whose upload failed, such as a workspace member.
31 pub package: String,
32 /// The version it was uploading.
33 pub version: String,
34 /// What stopped it: `429 Too Many Requests` for a registry rate limit,
35 /// or the first line of the error otherwise.
36 pub cause: String,
37 /// The time the registry gave for the next attempt, as it wrote it.
38 /// `None` when it gave none.
39 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub retry_after: Option<String>,
41}
42
43impl Stop {
44 /// A stop of `package` at `version` for `cause`.
45 #[must_use]
46 pub fn new(package: &str, version: &str, cause: &str, retry_after: Option<String>) -> Self {
47 Self {
48 package: package.to_owned(),
49 version: version.to_owned(),
50 cause: cause.to_owned(),
51 retry_after,
52 }
53 }
54}
55
56/// The stop record a Release body contains.
57///
58/// Not `#[non_exhaustive]`: the binary prints each state, and an exhaustive
59/// match makes a new variant fail the build until it is printed.
60#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
61#[serde(tag = "state", rename_all = "kebab-case")]
62pub enum Record {
63 /// The body contains no record.
64 #[default]
65 Absent,
66 /// The record parsed.
67 Read(Stop),
68 /// A `verctl:stop` comment is there but does not parse. It is never
69 /// trusted in part.
70 Unreadable,
71}
72
73/// The stop record in `body`: the first `verctl:stop` line.
74#[must_use]
75pub fn read(body: &str) -> Record {
76 let Some(line) = body.lines().map(str::trim).find(|line| is_record(line)) else {
77 return Record::Absent;
78 };
79 line.strip_prefix(OPEN)
80 .and_then(|rest| rest.strip_suffix(CLOSE))
81 .and_then(|json| serde_json::from_str(json).ok())
82 .map_or(Record::Unreadable, Record::Read)
83}
84
85/// `body` with its stop records replaced by one for `stop`, on its own line
86/// after the notes.
87///
88/// `-->` inside a value is written as `--\u003e`, which JSON reads back the
89/// same, so a cause cannot end the comment early.
90///
91/// # Errors
92///
93/// Returns an error when `stop` cannot be encoded as JSON.
94pub fn write(body: &str, stop: &Stop) -> Result<String> {
95 let json = serde_json::to_string(stop)
96 .context("encode the stop record")?
97 .replace("-->", "--\\u003e");
98 let notes = body
99 .lines()
100 .filter(|line| !is_record(line.trim()))
101 .collect::<Vec<_>>()
102 .join("\n");
103 Ok(formatdoc! {"
104 {notes}
105
106 {OPEN}{json}{CLOSE}
107 ",
108 notes = notes.trim_end(),
109 })
110}
111
112fn is_record(line: &str) -> bool {
113 line.starts_with(OPEN.trim_end())
114}
115
116#[cfg(test)]
117mod tests;