use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[non_exhaustive]
pub enum TimedOut {
NoProgress {
after_s: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
last_line: Option<String>,
},
Ceiling {
after_s: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
last_line: Option<String>,
},
}
pub(crate) fn kept_line(line: &str) -> String {
let mut end = line.len().min(300);
while !line.is_char_boundary(end) {
end -= 1;
}
line.get(..end).unwrap_or_default().to_owned()
}
impl TimedOut {
#[must_use]
pub fn after_s(&self) -> u64 {
match self {
TimedOut::NoProgress { after_s, .. } | TimedOut::Ceiling { after_s, .. } => *after_s,
}
}
pub(crate) fn masked(self, mask: impl FnOnce(&str) -> String) -> TimedOut {
match self {
TimedOut::NoProgress { after_s, last_line } => TimedOut::NoProgress {
after_s,
last_line: last_line.map(|l| mask(&l)),
},
TimedOut::Ceiling { after_s, last_line } => TimedOut::Ceiling {
after_s,
last_line: last_line.map(|l| mask(&l)),
},
}
}
}
impl std::fmt::Display for TimedOut {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TimedOut::NoProgress {
after_s,
last_line: Some(line),
} => {
write!(f, "showed no progress for {after_s}s and was stopped; its last line: `{line}`")
}
TimedOut::NoProgress { after_s, last_line: None } => write!(f, "printed nothing for {after_s}s and was stopped"),
TimedOut::Ceiling { after_s, last_line: None } => {
write!(f, "was stopped after {after_s}s, the most a step may run, though it kept making progress")
}
TimedOut::Ceiling {
after_s,
last_line: Some(line),
} => write!(
f,
"was still running after {after_s}s, the most it may run, and was stopped; its last line: `{line}`"
),
}
}
}