remove the two remaining as_str-style wrappers (DeliveryKind::parse, Wanted::parse)

This commit is contained in:
damocles 2026-09-11 22:08:03 +02:00
commit 7773f10646
2 changed files with 11 additions and 26 deletions

View file

@ -38,13 +38,6 @@ pub enum Wanted {
}
impl Wanted {
/// Derived (`strum::EnumString`, the same `snake_case` convention
/// `as_str` uses) rather than a hand-written match kept in sync with
/// it by hand.
fn parse(s: &str) -> Option<Self> {
s.parse().ok()
}
/// Seed value from an observed running state (first boot after
/// this store lands, or an agent spawned outside the normal path).
pub fn from_running(running: bool) -> Self {
@ -121,7 +114,7 @@ impl PowerStore {
.context("select agent_power")?;
match row {
None => Ok(None),
Some(stored) => Wanted::parse(&stored).map(Some).with_context(|| {
Some(stored) => stored.parse::<Wanted>().ok().map(Some).with_context(|| {
format!("agent_power row for {agent} holds an unknown intent {stored:?}")
}),
}

View file

@ -218,17 +218,6 @@ impl DeliveryKind {
<&str>::from(self)
)
}
/// Parse the `{kind}` path segment. Unknown values are rejected rather
/// than accepted-and-ignored: a typo in a registered `target_url` must
/// be *observable*, and a 200 for an unrecognised path is exactly the
/// silence this issue exists to remove. Derived (`strum::EnumString`,
/// the same per-variant `#[strum(serialize = "...")]` spellings
/// `as_str` uses) rather than a hand-written match kept in sync with
/// that one by hand.
fn parse(segment: &str) -> Option<Self> {
segment.parse().ok()
}
}
/// Why a delivery was refused.
@ -359,7 +348,10 @@ pub(super) async fn post_webhook_forge(
return (refusal.status(), message).into_response();
}
let Some(kind) = DeliveryKind::parse(&kind) else {
// Unknown values are rejected rather than accepted-and-ignored: a typo in
// a registered `target_url` must be *observable*, and a 200 for an
// unrecognised path is exactly the silence this issue exists to remove.
let Some(kind) = kind.parse::<DeliveryKind>().ok() else {
tracing::warn!(%kind, "webhook: delivery on an unknown hook kind");
return (StatusCode::NOT_FOUND, "unknown hook kind").into_response();
};
@ -547,20 +539,20 @@ mod tests {
#[test]
fn only_the_registered_hook_kinds_parse() {
assert_eq!(
DeliveryKind::parse("knowledge"),
"knowledge".parse::<DeliveryKind>().ok(),
Some(DeliveryKind::Knowledge)
);
assert_eq!(
DeliveryKind::parse("config-pr"),
"config-pr".parse::<DeliveryKind>().ok(),
Some(DeliveryKind::ConfigPr)
);
assert_eq!(
DeliveryKind::parse("config_pr"),
"config_pr".parse::<DeliveryKind>().ok(),
None,
"underscore is not the registered spelling; accepting both would make a journal line ambiguous"
);
assert_eq!(DeliveryKind::parse(""), None);
assert_eq!(DeliveryKind::parse("../knowledge"), None);
assert_eq!("".parse::<DeliveryKind>().ok(), None);
assert_eq!("../knowledge".parse::<DeliveryKind>().ok(), None);
}
/// A malformed stored secret is replaced rather than fatal — and the
@ -635,7 +627,7 @@ mod tests {
let url = kind.target_url("https://swarm.example");
let segment = url.rsplit('/').next().expect("a last segment");
assert_eq!(
DeliveryKind::parse(segment),
segment.parse::<DeliveryKind>().ok(),
Some(kind),
"the last path segment of a registered URL must parse back \
to the kind that built it"