isothrum_runner/error/
timed_out.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
13#[serde(tag = "kind", rename_all = "snake_case")]
14#[non_exhaustive]
15pub enum TimedOut {
16 NoProgress {
18 after_s: u64,
20 #[serde(default, skip_serializing_if = "Option::is_none")]
23 last_line: Option<String>,
24 },
25 Ceiling {
28 after_s: u64,
30 #[serde(default, skip_serializing_if = "Option::is_none")]
33 last_line: Option<String>,
34 },
35}
36
37pub(crate) fn kept_line(line: &str) -> String {
39 let mut end = line.len().min(300);
40 while !line.is_char_boundary(end) {
41 end -= 1;
42 }
43 line.get(..end).unwrap_or_default().to_owned()
44}
45
46impl TimedOut {
47 #[must_use]
49 pub fn after_s(&self) -> u64 {
50 match self {
51 TimedOut::NoProgress { after_s, .. } | TimedOut::Ceiling { after_s, .. } => *after_s,
52 }
53 }
54
55 pub(crate) fn masked(self, mask: impl FnOnce(&str) -> String) -> TimedOut {
57 match self {
58 TimedOut::NoProgress { after_s, last_line } => TimedOut::NoProgress {
59 after_s,
60 last_line: last_line.map(|l| mask(&l)),
61 },
62 TimedOut::Ceiling { after_s, last_line } => TimedOut::Ceiling {
63 after_s,
64 last_line: last_line.map(|l| mask(&l)),
65 },
66 }
67 }
68}
69
70impl std::fmt::Display for TimedOut {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 match self {
73 TimedOut::NoProgress {
74 after_s,
75 last_line: Some(line),
76 } => {
77 write!(f, "showed no progress for {after_s}s and was stopped; its last line: `{line}`")
78 }
79 TimedOut::NoProgress { after_s, last_line: None } => write!(f, "printed nothing for {after_s}s and was stopped"),
80 TimedOut::Ceiling { after_s, last_line: None } => {
81 write!(f, "was stopped after {after_s}s, the most a step may run, though it kept making progress")
82 }
83 TimedOut::Ceiling {
84 after_s,
85 last_line: Some(line),
86 } => write!(
87 f,
88 "was still running after {after_s}s, the most it may run, and was stopped; its last line: `{line}`"
89 ),
90 }
91 }
92}