//! The head of a list whose last write was refused (architect, 14 Oct 2026). //! //! A write the MCP admission refuses files ONE open Feedback row per cause in //! the owner's database, with a typed `context.refused_write` naming the //! table it was for (`cotton-server`'s `RefusedWrite`). A list over that table //! says so at its head, so a sampler refused every five minutes for hours is //! seen where its rows stopped, not in a log nobody reads. //! //! The engine fills two typed readings — the refusal's `reason` and the //! moment it began, `since` — and the `feedback` definition's //! `refused_reason` and `refused_since` columns word them: every word of the //! line is the definition's, so a rewording is data. use chrono::{DateTime, Utc}; use crate::definitions::TableDefinition; use crate::device::words::Clock; /// One open refused write, as the list head reads it. #[derive(Debug, Clone, PartialEq, Eq)] pub struct RefusedSample { /// The admission's own sentence for the refusal. pub reason: String, /// When this cause was first refused (RFC 3339). pub since: String, } /// Of the open refused writes for one table, the one the head says: the /// newest, by when it began. `None` when nothing is refused. pub fn newest(samples: impl IntoIterator) -> Option { samples .into_iter() .max_by_key(|sample| parse(&sample.since).map(|at| at.timestamp_millis()).unwrap_or(i64::MIN)) } /// The head line for a refused write, worded by the `feedback` definition: /// `refused_reason`'s `reading` with the reason, then `refused_since`'s with /// the day and clock time it began in the reader's zone. `None` when the /// definition does not word it. pub fn head_line(definition: &TableDefinition, sample: &RefusedSample, clock: &Clock) -> Option { let reading = |key: &str| definition.column(key).and_then(|column| column.reading.as_deref()); let mut line = reading("refused_reason")?.replace("{value}", &sample.reason); if let (Some(since), Some(template)) = (parse(&sample.since), reading("refused_since")) { let at = since.with_timezone(&clock.zone).format("%-d %b %H:%M").to_string(); line.push_str(&template.replace("{value}", &at)); } Some(line) } fn parse(at: &str) -> Option> { DateTime::parse_from_rfc3339(at).ok().map(|at| at.with_timezone(&Utc)) } #[cfg(test)] #[path = "refused_write_tests.rs"] mod tests;