1mod calendar_html;
2mod edition;
3mod readme;
4mod registry;
5mod report;
6mod stars;
7mod store;
8mod sync;
9
10pub use super::calendar::DayAvailability;
11pub use calendar_html::CalendarHtmlError;
12pub use edition::{ProgressDay, SolverRegistration, current_edition_year, edition_progress};
13pub use readme::{ReadmeSectionError, ReadmeWriteError, replace_progress_section, update_readme};
14pub use registry::{RegistryError, registered_days};
15pub use report::{EditionProgress, ProgressDayReport};
16pub use stars::{StarSnapshot, StarStore, StarStoreError, VerifiedStar};
17pub use store::{LocalProgressStore, LocalProgressStoreError};
18pub use sync::{StarSyncError, StarSyncOutcome, cached_stars, sync_stars};
19
20use std::collections::BTreeMap;
21
22use serde::{Deserialize, Serialize};
23
24use super::run::{PartStatus, ProgressReport};
25
26const LOCAL_PROGRESS_SCHEMA_VERSION: u32 = 1;
27
28#[derive(Debug, Default)]
30pub struct LocalProgress {
31 parts: BTreeMap<(usize, usize, u8), PartStatus>,
32}
33
34#[derive(Deserialize, Serialize)]
35#[serde(deny_unknown_fields)]
36struct LocalProgressFile {
37 schema_version: u32,
38 parts: Vec<StoredPart>,
39}
40
41#[derive(Deserialize, Serialize)]
42#[serde(deny_unknown_fields)]
43struct StoredPart {
44 year: usize,
45 day: usize,
46 part: u8,
47 status: PartStatus,
48}
49
50#[derive(Debug, thiserror::Error)]
51pub enum LocalProgressError {
52 #[error("Invalid local progress JSON: {0}")]
53 Json(#[from] serde_json::Error),
54
55 #[error("Unsupported local progress schema version: {0}")]
56 UnsupportedVersion(u32),
57
58 #[error("Invalid local progress entry for {year} / Day {day:02} / Part {part}")]
59 InvalidPart { year: usize, day: usize, part: u8 },
60
61 #[error("Duplicate local progress entry for {year} / Day {day:02} / Part {part}")]
62 DuplicatePart { year: usize, day: usize, part: u8 },
63}
64
65impl LocalProgress {
66 pub fn record(&mut self, report: &ProgressReport) {
68 for day in &report.days {
69 for part in &day.parts {
70 self.parts
71 .insert((day.year, day.day, part.part), part.status);
72 }
73 }
74 }
75
76 #[must_use]
78 pub fn part_status(&self, year: usize, day: usize, part: u8) -> Option<PartStatus> {
79 self.parts.get(&(year, day, part)).copied()
80 }
81
82 pub fn to_json(&self) -> Result<Vec<u8>, LocalProgressError> {
88 let parts = self
89 .parts
90 .iter()
91 .map(|(&(year, day, part), &status)| StoredPart {
92 year,
93 day,
94 part,
95 status,
96 })
97 .collect();
98
99 Ok(serde_json::to_vec_pretty(&LocalProgressFile {
100 schema_version: LOCAL_PROGRESS_SCHEMA_VERSION,
101 parts,
102 })?)
103 }
104
105 pub fn from_json(bytes: &[u8]) -> Result<Self, LocalProgressError> {
111 #[derive(Deserialize)]
112 struct Header {
113 schema_version: u32,
114 }
115
116 let header: Header = serde_json::from_slice(bytes)?;
117 if header.schema_version != LOCAL_PROGRESS_SCHEMA_VERSION {
118 return Err(LocalProgressError::UnsupportedVersion(
119 header.schema_version,
120 ));
121 }
122
123 let file: LocalProgressFile = serde_json::from_slice(bytes)?;
124 let mut progress = Self::default();
125
126 for StoredPart {
127 year,
128 day,
129 part,
130 status,
131 } in file.parts
132 {
133 if !(2015..=9999).contains(&year)
134 || !(1..=25).contains(&day)
135 || !(1..=2).contains(&part)
136 {
137 return Err(LocalProgressError::InvalidPart { year, day, part });
138 }
139
140 if progress.parts.insert((year, day, part), status).is_some() {
141 return Err(LocalProgressError::DuplicatePart { year, day, part });
142 }
143 }
144
145 Ok(progress)
146 }
147}
148
149#[cfg(test)]
150mod tests {
151 use serde_json::{Value, json};
152
153 use super::{LocalProgress, LocalProgressError, PartStatus, ProgressReport};
154
155 fn report(parts: &Value, day_error: &Value) -> ProgressReport {
156 serde_json::from_value(json!({
157 "schema_version": 1,
158 "days": [{
159 "year": 2025,
160 "day": 3,
161 "parts": parts,
162 "day_error": day_error,
163 }],
164 }))
165 .unwrap()
166 }
167
168 #[test]
169 fn updates_only_the_part_that_ran() {
170 let mut progress = LocalProgress::default();
171
172 progress.record(&report(
173 &json!([
174 { "part": 1, "status": "succeeded" },
175 { "part": 2, "status": "succeeded" }
176 ]),
177 &Value::Null,
178 ));
179 progress.record(&report(
180 &json!([{ "part": 2, "status": "panicked" }]),
181 &Value::Null,
182 ));
183
184 assert_eq!(
185 progress.part_status(2025, 3, 1),
186 Some(PartStatus::Succeeded)
187 );
188 assert_eq!(progress.part_status(2025, 3, 2), Some(PartStatus::Panicked));
189 assert_eq!(progress.part_status(2025, 4, 1), None);
190 }
191
192 #[test]
193 fn day_level_failure_preserves_previous_part_outcomes() {
194 let mut progress = LocalProgress::default();
195
196 progress.record(&report(
197 &json!([{ "part": 1, "status": "succeeded" }]),
198 &Value::Null,
199 ));
200 progress.record(&report(&json!([]), &json!("panic")));
201
202 assert_eq!(
203 progress.part_status(2025, 3, 1),
204 Some(PartStatus::Succeeded)
205 );
206 assert_eq!(progress.part_status(2025, 3, 2), None);
207 }
208
209 #[test]
210 fn round_trips_local_progress() {
211 let mut progress = LocalProgress::default();
212 progress.record(&report(
213 &json!([
214 { "part": 1, "status": "succeeded" },
215 { "part": 2, "status": "panicked" }
216 ]),
217 &Value::Null,
218 ));
219
220 let bytes = progress.to_json().unwrap();
221 let restored = LocalProgress::from_json(&bytes).unwrap();
222
223 assert_eq!(
224 restored.part_status(2025, 3, 1),
225 Some(PartStatus::Succeeded)
226 );
227 assert_eq!(restored.part_status(2025, 3, 2), Some(PartStatus::Panicked));
228 assert_eq!(restored.to_json().unwrap(), bytes);
229 }
230
231 #[test]
232 fn rejects_unsupported_local_progress_schema() {
233 assert!(matches!(
234 LocalProgress::from_json(br#"{"schema_version":2,"entries":[]}"#),
235 Err(LocalProgressError::UnsupportedVersion(2)),
236 ));
237 }
238
239 #[test]
240 fn rejects_duplicate_local_progress_entries() {
241 let entry = json!({
242 "year": 2025,
243 "day": 3,
244 "part": 1,
245 "status": "succeeded",
246 });
247 let bytes = serde_json::to_vec(&json!({
248 "schema_version": 1,
249 "parts": [&entry, &entry],
250 }))
251 .unwrap();
252
253 assert!(matches!(
254 LocalProgress::from_json(&bytes),
255 Err(LocalProgressError::DuplicatePart {
256 year: 2025,
257 day: 3,
258 part: 1,
259 }),
260 ));
261 }
262}