aoc_toolkit/application/run/
progress_report.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Deserialize)]
4#[serde(deny_unknown_fields)]
5pub struct ProgressReport {
6 pub schema_version: u32,
7 pub days: Vec<DayProgress>,
8}
9
10#[derive(Debug, Deserialize)]
11#[serde(deny_unknown_fields)]
12pub struct DayProgress {
13 pub year: usize,
14 pub day: usize,
15 pub parts: Vec<PartProgress>,
16 pub day_error: Option<DayFailure>,
17}
18
19#[derive(Debug, Deserialize)]
20#[serde(deny_unknown_fields)]
21pub struct PartProgress {
22 pub part: u8,
23 pub status: PartStatus,
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
27#[serde(rename_all = "snake_case")]
28pub enum PartStatus {
29 Succeeded,
30 Panicked,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
34#[serde(rename_all = "snake_case")]
35pub enum DayFailure {
36 Input,
37 Panic,
38}
39
40#[derive(Debug, thiserror::Error)]
41pub enum ProgressReportError {
42 #[error("Cannot decode runner progress report: {0}")]
43 Decode(#[from] serde_json::Error),
44
45 #[error("Unsupported runner progress report schema version: {0}")]
46 UnsupportedVersion(u32),
47
48 #[error("Runner progress report days do not match the execution plan")]
49 UnexpectedDays,
50
51 #[error("Invalid part outcomes for {year} / Day {day:02}")]
52 InvalidParts { year: usize, day: usize },
53}
54
55impl ProgressReport {
56 pub(super) fn decode(
57 bytes: &[u8],
58 planned_days: &[(usize, usize)],
59 selected_part: Option<u8>,
60 ) -> Result<Self, ProgressReportError> {
61 #[derive(Deserialize)]
62 struct Header {
63 schema_version: u32,
64 }
65
66 let header: Header = serde_json::from_slice(bytes)?;
67 if header.schema_version != 1 {
68 return Err(ProgressReportError::UnsupportedVersion(
69 header.schema_version,
70 ));
71 }
72
73 let report: Self = serde_json::from_slice(bytes)?;
74 if report
75 .days
76 .iter()
77 .map(|day| (day.year, day.day))
78 .ne(planned_days.iter().copied())
79 {
80 return Err(ProgressReportError::UnexpectedDays);
81 }
82
83 let expected = selected_part
84 .as_ref()
85 .map_or(&[1, 2][..], std::slice::from_ref);
86 for day in &report.days {
87 if !day.has_valid_parts(expected) {
88 return Err(ProgressReportError::InvalidParts {
89 year: day.year,
90 day: day.day,
91 });
92 }
93 }
94
95 Ok(report)
96 }
97}
98
99impl DayProgress {
100 fn has_valid_parts(&self, expected: &[u8]) -> bool {
101 if self.parts.len() > expected.len()
102 || self
103 .parts
104 .iter()
105 .zip(expected)
106 .any(|(actual, expected)| actual.part != *expected)
107 {
108 return false;
109 }
110
111 let panicked = self
112 .parts
113 .last()
114 .is_some_and(|part| part.status == PartStatus::Panicked);
115 let earlier_parts = &self.parts[..self.parts.len().saturating_sub(1)];
116 if earlier_parts
117 .iter()
118 .any(|part| part.status == PartStatus::Panicked)
119 {
120 return false;
121 }
122
123 match self.day_error {
124 Some(DayFailure::Input) => self.parts.is_empty(),
125 Some(DayFailure::Panic) => {
126 !panicked && (self.parts.is_empty() || self.parts.len() == expected.len())
127 }
128 None => panicked || self.parts.len() == expected.len(),
129 }
130 }
131}
132
133#[cfg(test)]
134mod tests {
135 use serde_json::json;
136
137 use super::{ProgressReport, ProgressReportError};
138
139 #[test]
140 fn validates_report_against_plan_and_part_selection() {
141 let planned_days = [(2025, 3)];
142 let valid = json!({
143 "schema_version": 1,
144 "days": [{
145 "year": 2025,
146 "day": 3,
147 "parts": [
148 { "part": 1, "status": "succeeded" },
149 { "part": 2, "status": "panicked" }
150 ],
151 "day_error": null
152 }]
153 });
154
155 let bytes = valid.to_string();
156 assert!(ProgressReport::decode(bytes.as_bytes(), &planned_days, None).is_ok());
157 assert!(matches!(
158 ProgressReport::decode(bytes.as_bytes(), &planned_days, Some(2)),
159 Err(ProgressReportError::InvalidParts { .. }),
160 ));
161 assert!(matches!(
162 ProgressReport::decode(bytes.as_bytes(), &[(2025, 4)], None),
163 Err(ProgressReportError::UnexpectedDays),
164 ));
165
166 let mut invalid = valid.clone();
167 invalid["days"][0]["parts"][1]["part"] = json!(1);
168 assert!(matches!(
169 ProgressReport::decode(invalid.to_string().as_bytes(), &planned_days, None),
170 Err(ProgressReportError::InvalidParts { .. }),
171 ));
172
173 let mut parse_panic = valid;
174 parse_panic["days"][0]["parts"] = json!([]);
175 parse_panic["days"][0]["day_error"] = json!("panic");
176 assert!(
177 ProgressReport::decode(parse_panic.to_string().as_bytes(), &planned_days, None).is_ok(),
178 );
179 }
180
181 #[test]
182 fn rejects_an_unknown_schema_before_decoding_its_payload() {
183 assert!(matches!(
184 ProgressReport::decode(br#"{"schema_version":2,"results":[]}"#, &[], None),
185 Err(ProgressReportError::UnsupportedVersion(2)),
186 ));
187 }
188}