1use anyhow::{Context, Result, ensure};
3use csv::{ReaderBuilder, Trim, Writer};
4use indexmap::IndexSet;
5use std::fs;
6use std::path::{Path, PathBuf};
7
8pub struct ModelPatch {
10 base_model_dir: PathBuf,
12 file_patches: Vec<FilePatch>,
14 toml_patch: Option<toml::value::Table>,
16}
17
18impl ModelPatch {
19 pub fn new<P: Into<PathBuf>>(base_model_dir: P) -> Self {
21 ModelPatch {
22 base_model_dir: base_model_dir.into(),
23 file_patches: Vec::new(),
24 toml_patch: None,
25 }
26 }
27
28 pub fn from_example(name: &str) -> Self {
30 let base_model_dir = PathBuf::from("examples").join(name);
31 ModelPatch::new(base_model_dir)
32 }
33
34 pub fn with_file_patch(mut self, patch: FilePatch) -> Self {
36 self.file_patches.push(patch);
37 self
38 }
39
40 pub fn with_file_patches<I>(mut self, patches: I) -> Self
42 where
43 I: IntoIterator<Item = FilePatch>,
44 {
45 self.file_patches.extend(patches);
46 self
47 }
48
49 pub fn with_toml_patch(mut self, patch_str: impl AsRef<str>) -> Self {
52 assert!(
53 self.toml_patch.is_none(),
54 "TOML patch already set for this ModelPatch"
55 );
56 let s = patch_str.as_ref();
57 let patch: toml::value::Table =
58 toml::from_str(s).expect("Failed to parse string passed to with_toml_patch");
59 self.toml_patch = Some(patch);
60 self
61 }
62
63 pub fn build<O: AsRef<Path>>(&self, out_dir: O) -> Result<()> {
65 let base_dir = self.base_model_dir.as_path();
66 let out_path = out_dir.as_ref();
67
68 let base_toml_path = base_dir.join("model.toml");
70 let out_toml_path = out_path.join("model.toml");
71 if let Some(toml_patch) = &self.toml_patch {
72 let toml_content = fs::read_to_string(&base_toml_path)?;
73 let merged_toml = merge_model_toml(&toml_content, toml_patch)?;
74 fs::write(&out_toml_path, merged_toml)?;
75 } else {
76 fs::copy(&base_toml_path, &out_toml_path)?;
77 }
78
79 for entry in fs::read_dir(base_dir)? {
82 let entry = entry?;
83 let src_path = entry.path();
84 if src_path.is_file()
85 && src_path
86 .extension()
87 .and_then(|e| e.to_str())
88 .is_some_and(|ext| ext.eq_ignore_ascii_case("csv"))
89 {
90 let dst_path = out_path.join(entry.file_name());
91 fs::copy(&src_path, &dst_path)?;
92 }
93 }
94
95 for patch in &self.file_patches {
97 patch.apply_and_save(base_dir, out_path)?;
98 }
99
100 Ok(())
101 }
102
103 pub fn build_to_tempdir(&self) -> Result<tempfile::TempDir> {
105 let temp_dir = tempfile::tempdir()?;
106 self.build(temp_dir.path())?;
107 Ok(temp_dir)
108 }
109}
110
111type CSVTable = IndexSet<Vec<String>>;
115
116#[derive(Clone)]
118pub struct FilePatch {
119 filename: String,
121 header_row: Option<Vec<String>>,
123 replacement_content: Option<String>,
125 to_delete: CSVTable,
127 to_add: CSVTable,
129}
130
131impl FilePatch {
132 pub fn new(filename: impl Into<String>) -> Self {
134 FilePatch {
135 filename: filename.into(),
136 header_row: None,
137 replacement_content: None,
138 to_delete: IndexSet::new(),
139 to_add: IndexSet::new(),
140 }
141 }
142
143 pub fn with_header(mut self, header: impl Into<String>) -> Self {
145 assert!(
146 self.replacement_content.is_none(),
147 "Cannot set header when replacement content is set for this FilePatch",
148 );
149 assert!(
150 self.header_row.is_none(),
151 "Header already set for this FilePatch",
152 );
153 let s = header.into();
154 let v = s.split(',').map(|s| s.trim().to_string()).collect();
155 self.header_row = Some(v);
156 self
157 }
158
159 pub fn with_replacement(mut self, lines: &[&str]) -> Self {
165 assert!(
166 self.header_row.is_none(),
167 "Cannot set replacement content when header is set for this FilePatch",
168 );
169 assert!(
170 self.to_delete.is_empty() && self.to_add.is_empty(),
171 "Cannot set replacement content when additions/deletions are set for this FilePatch",
172 );
173 assert!(
174 self.replacement_content.is_none(),
175 "Replacement content already set for this FilePatch",
176 );
177
178 if !lines.is_empty() {
180 let first_col_count = lines[0].matches(',').count() + 1;
181 for (idx, line) in lines.iter().enumerate() {
182 let col_count = line.matches(',').count() + 1;
183 assert_eq!(
184 col_count, first_col_count,
185 "Line {idx} has {col_count} columns but line 0 has {first_col_count}: {line:?}"
186 );
187 }
188 }
189
190 let content = lines.join("\n") + "\n";
191 self.replacement_content = Some(content);
192 self
193 }
194
195 pub fn with_addition(mut self, row: impl Into<String>) -> Self {
197 assert!(
198 self.replacement_content.is_none(),
199 "Cannot add rows when replacement content is set for this FilePatch",
200 );
201 let s = row.into();
202 let v = s.split(',').map(|s| s.trim().to_string()).collect();
203 self.to_add.insert(v);
204 self
205 }
206
207 pub fn with_deletion(mut self, row: impl Into<String>) -> Self {
209 assert!(
210 self.replacement_content.is_none(),
211 "Cannot delete rows when replacement content is set for this FilePatch",
212 );
213 let s = row.into();
214 let v = s.split(',').map(|s| s.trim().to_string()).collect();
215 self.to_delete.insert(v);
216 self
217 }
218
219 fn apply(&self, base_model_dir: &Path) -> Result<String> {
221 let base_path = base_model_dir.join(&self.filename);
223
224 if let Some(content) = &self.replacement_content {
226 return Ok(content.clone());
227 }
228
229 let base = fs::read_to_string(&base_path)?;
231
232 let modified = modify_base_with_patch(&base, self)
234 .with_context(|| format!("Error applying patch to file: {}", self.filename))?;
235 Ok(modified)
236 }
237
238 pub fn apply_and_save(&self, base_model_dir: &Path, out_model_dir: &Path) -> Result<()> {
240 let modified = self.apply(base_model_dir)?;
241 let new_path = out_model_dir.join(&self.filename);
242 fs::write(&new_path, modified)?;
243 Ok(())
244 }
245}
246
247fn merge_model_toml(base_toml: &str, patch: &toml::value::Table) -> Result<String> {
249 let mut base_val: toml::Value = toml::from_str(base_toml)?;
251 let base_tbl = base_val
252 .as_table_mut()
253 .context("Base model TOML must be a table")?;
254
255 for (k, v) in patch {
257 base_tbl.insert(k.clone(), v.clone());
258 }
259
260 let out = toml::to_string_pretty(&base_val)?;
262 Ok(out)
263}
264
265fn modify_base_with_patch(base: &str, patch: &FilePatch) -> Result<String> {
268 let mut reader = ReaderBuilder::new()
270 .trim(Trim::All)
271 .from_reader(base.as_bytes());
272
273 let base_header = reader
275 .headers()
276 .context("Failed to read base file header")?;
277 let base_header_vec: Vec<String> = base_header.iter().map(ToString::to_string).collect();
278
279 if let Some(ref header_row_vec) = patch.header_row {
281 ensure!(
282 base_header_vec == *header_row_vec,
283 "Header mismatch: base file has [{}], patch has [{}]",
284 base_header_vec.join(", "),
285 header_row_vec.join(", ")
286 );
287 }
288 let mut base_rows: CSVTable = CSVTable::new();
290 for result in reader.records() {
291 let record = result?;
292
293 let row_vec = record
295 .iter()
296 .map(|s| s.trim().to_string())
297 .collect::<Vec<_>>();
298
299 ensure!(
301 base_rows.insert(row_vec.clone()),
302 "Duplicate row in base file: {row_vec:?}",
303 );
304 }
305
306 for del_row in &patch.to_delete {
308 ensure!(
309 !patch.to_add.contains(del_row),
310 "Row appears in both deletions and additions: {del_row:?}",
311 );
312 }
313
314 for del_row in &patch.to_delete {
316 ensure!(
317 base_rows.contains(del_row),
318 "Row to delete not present in base file: {del_row:?}"
319 );
320 }
321
322 base_rows.retain(|row| !patch.to_delete.contains(row));
324
325 for add_row in &patch.to_add {
327 ensure!(
328 base_rows.insert(add_row.clone()),
329 "Addition already present in base file: {add_row:?}"
330 );
331 }
332
333 let expected_len = base_header_vec.len();
335 for row in &base_rows {
336 ensure!(
337 row.len() == expected_len,
338 "Row has {} columns but header has {expected_len}: {row:?}",
339 row.len(),
340 );
341 }
342
343 let mut wtr = Writer::from_writer(vec![]);
345 wtr.write_record(base_header_vec.iter())?;
346 for row in &base_rows {
347 let row_iter = row.iter().map(String::as_str);
348 wtr.write_record(row_iter)?;
349 }
350 wtr.flush()?;
351 let inner = wtr.into_inner()?;
352 let output = String::from_utf8(inner)?;
353 Ok(output)
354}
355
356#[cfg(test)]
357mod tests {
358 use super::*;
359 use crate::fixture::assert_error;
360 use crate::input::read_toml;
361 use crate::model::ModelParameters;
362 use crate::patch::{FilePatch, ModelPatch};
363
364 #[test]
365 fn modify_base_with_patch_works() {
366 let base = "col1,col2\nvalue1,value2\nvalue3,value4\nvalue5,value6\n";
367
368 let patch = FilePatch::new("test.csv")
370 .with_header("col1,col2")
371 .with_deletion("value3,value4")
372 .with_addition("value7,value8");
373
374 let modified = modify_base_with_patch(base, &patch).unwrap();
375
376 let lines: Vec<&str> = modified.lines().collect();
377 assert_eq!(lines[0], "col1,col2"); assert_eq!(lines[1], "value1,value2"); assert_eq!(lines[2], "value5,value6"); assert_eq!(lines[3], "value7,value8"); assert!(!modified.contains("value3,value4")); }
383
384 #[test]
385 fn modify_base_with_patch_mismatched_header() {
386 let base = "col1,col2\nvalue1,value2\n";
387
388 let patch = FilePatch::new("test.csv").with_header("col1,col3");
390
391 assert_error!(
392 modify_base_with_patch(base, &patch),
393 "Header mismatch: base file has [col1, col2], patch has [col1, col3]"
394 );
395 }
396
397 #[test]
398 fn merge_model_toml_basic() {
399 let base = r#"
400 field = "data"
401 [section]
402 a = 1
403 "#;
404
405 let mut patch = toml::value::Table::new();
407 patch.insert(
408 "field".to_string(),
409 toml::Value::String("patched".to_string()),
410 );
411 patch.insert(
412 "new_field".to_string(),
413 toml::Value::String("added".to_string()),
414 );
415
416 let merged = merge_model_toml(base, &patch).unwrap();
419 assert!(merged.contains("field = \"patched\""));
420 assert!(merged.contains("[section]"));
421 assert!(merged.contains("new_field = \"added\""));
422 }
423
424 #[test]
425 fn file_patch() {
426 let assets_patch = FilePatch::new("assets.csv")
428 .with_deletion("GASDRV,GBR,A0_GEX,4002.26,2020")
429 .with_addition("GASDRV,GBR,A0_GEX,4003.26,2020");
430
431 let model_dir = ModelPatch::from_example("simple")
433 .with_file_patch(assets_patch)
434 .build_to_tempdir()
435 .unwrap();
436
437 let assets_path = model_dir.path().join("assets.csv");
439 let assets_content = std::fs::read_to_string(assets_path).unwrap();
440 assert!(!assets_content.contains("GASDRV,GBR,A0_GEX,4002.26,2020"));
441 assert!(assets_content.contains("GASDRV,GBR,A0_GEX,4003.26,2020"));
442 }
443
444 #[test]
445 fn file_patch_with_replacement() {
446 let expected = "col1,col2\nnew1,new2\n";
447
448 let model_dir = ModelPatch::from_example("simple")
449 .with_file_patch(
450 FilePatch::new("assets.csv").with_replacement(&["col1,col2", "new1,new2"]),
451 )
452 .build_to_tempdir()
453 .unwrap();
454
455 let assets_path = model_dir.path().join("assets.csv");
456 let assets_content = std::fs::read_to_string(assets_path).unwrap();
457 assert_eq!(assets_content, expected);
458 }
459
460 #[test]
461 #[should_panic(
462 expected = "Cannot set replacement content when header is set for this FilePatch"
463 )]
464 fn file_patch_replacement_after_header_panics() {
465 let _ = FilePatch::new("assets.csv")
466 .with_header("col1,col2")
467 .with_replacement(&["col1,col2", "a,b"]);
468 }
469
470 #[test]
471 #[should_panic(
472 expected = "Cannot set replacement content when additions/deletions are set for this FilePatch"
473 )]
474 fn file_patch_replacement_after_addition_panics() {
475 let _ = FilePatch::new("assets.csv")
476 .with_addition("a,b")
477 .with_replacement(&["col1,col2", "a,b"]);
478 }
479
480 #[test]
481 #[should_panic(expected = "Cannot add rows when replacement content is set for this FilePatch")]
482 fn file_patch_addition_after_replacement_panics() {
483 let _ = FilePatch::new("assets.csv")
484 .with_replacement(&["col1,col2", "a,b"])
485 .with_addition("c,d");
486 }
487
488 #[test]
489 #[should_panic(expected = "Line 1 has 2 columns but line 0 has 3")]
490 fn file_patch_replacement_column_count_mismatch_panics() {
491 let _ = FilePatch::new("test.csv").with_replacement(&["col1,col2,col3", "a,b"]);
492 }
493
494 #[test]
495 fn toml_patch() {
496 let toml_patch = "milestone_years = [2020, 2030, 2040, 2050]\n";
498
499 let model_dir = ModelPatch::from_example("simple")
501 .with_toml_patch(toml_patch)
502 .build_to_tempdir()
503 .unwrap();
504
505 let toml: ModelParameters = read_toml(&model_dir.path().join("model.toml")).unwrap();
507 assert_eq!(toml.milestone_years, vec![2020, 2030, 2040, 2050]);
508 }
509}