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 to_delete: CSVTable,
125 to_add: CSVTable,
127}
128
129impl FilePatch {
130 pub fn new(filename: impl Into<String>) -> Self {
132 FilePatch {
133 filename: filename.into(),
134 header_row: None,
135 to_delete: IndexSet::new(),
136 to_add: IndexSet::new(),
137 }
138 }
139
140 pub fn with_header(mut self, header: impl Into<String>) -> Self {
142 assert!(
143 self.header_row.is_none(),
144 "Header already set for this FilePatch",
145 );
146 let s = header.into();
147 let v = s.split(',').map(|s| s.trim().to_string()).collect();
148 self.header_row = Some(v);
149 self
150 }
151
152 pub fn with_addition(mut self, row: impl Into<String>) -> Self {
154 let s = row.into();
155 let v = s.split(',').map(|s| s.trim().to_string()).collect();
156 self.to_add.insert(v);
157 self
158 }
159
160 pub fn with_deletion(mut self, row: impl Into<String>) -> Self {
162 let s = row.into();
163 let v = s.split(',').map(|s| s.trim().to_string()).collect();
164 self.to_delete.insert(v);
165 self
166 }
167
168 fn apply(&self, base_model_dir: &Path) -> Result<String> {
170 let base_path = base_model_dir.join(&self.filename);
172 ensure!(
173 base_path.exists() && base_path.is_file(),
174 "Base file for patching does not exist: {}",
175 base_path.display()
176 );
177 let base = fs::read_to_string(&base_path)?;
178
179 let modified = modify_base_with_patch(&base, self)
181 .with_context(|| format!("Error applying patch to file: {}", self.filename))?;
182 Ok(modified)
183 }
184
185 pub fn apply_and_save(&self, base_model_dir: &Path, out_model_dir: &Path) -> Result<()> {
187 let modified = self.apply(base_model_dir)?;
188 let new_path = out_model_dir.join(&self.filename);
189 fs::write(&new_path, modified)?;
190 Ok(())
191 }
192}
193
194fn merge_model_toml(base_toml: &str, patch: &toml::value::Table) -> Result<String> {
196 let mut base_val: toml::Value = toml::from_str(base_toml)?;
198 let base_tbl = base_val
199 .as_table_mut()
200 .context("Base model TOML must be a table")?;
201
202 for (k, v) in patch {
204 base_tbl.insert(k.clone(), v.clone());
205 }
206
207 let out = toml::to_string_pretty(&base_val)?;
209 Ok(out)
210}
211
212fn modify_base_with_patch(base: &str, patch: &FilePatch) -> Result<String> {
215 let mut reader = ReaderBuilder::new()
217 .trim(Trim::All)
218 .from_reader(base.as_bytes());
219
220 let base_header = reader
222 .headers()
223 .context("Failed to read base file header")?;
224 let base_header_vec: Vec<String> = base_header.iter().map(ToString::to_string).collect();
225
226 if let Some(ref header_row_vec) = patch.header_row {
228 ensure!(
229 base_header_vec == *header_row_vec,
230 "Header mismatch: base file has [{}], patch has [{}]",
231 base_header_vec.join(", "),
232 header_row_vec.join(", ")
233 );
234 }
235
236 let mut base_rows: CSVTable = CSVTable::new();
238 for result in reader.records() {
239 let record = result?;
240
241 let row_vec = record
243 .iter()
244 .map(|s| s.trim().to_string())
245 .collect::<Vec<_>>();
246
247 ensure!(
249 base_rows.insert(row_vec.clone()),
250 "Duplicate row in base file: {row_vec:?}",
251 );
252 }
253
254 for del_row in &patch.to_delete {
256 ensure!(
257 !patch.to_add.contains(del_row),
258 "Row appears in both deletions and additions: {del_row:?}",
259 );
260 }
261
262 for del_row in &patch.to_delete {
264 ensure!(
265 base_rows.contains(del_row),
266 "Row to delete not present in base file: {del_row:?}"
267 );
268 }
269
270 base_rows.retain(|row| !patch.to_delete.contains(row));
272
273 for add_row in &patch.to_add {
275 ensure!(
276 base_rows.insert(add_row.clone()),
277 "Addition already present in base file: {add_row:?}"
278 );
279 }
280
281 let mut wtr = Writer::from_writer(vec![]);
283 wtr.write_record(base_header_vec.iter())?;
284 for row in &base_rows {
285 let row_iter = row.iter().map(String::as_str);
286 wtr.write_record(row_iter)?;
287 }
288 wtr.flush()?;
289 let inner = wtr.into_inner()?;
290 let output = String::from_utf8(inner)?;
291 Ok(output)
292}
293
294#[cfg(test)]
295mod tests {
296 use super::*;
297 use crate::fixture::assert_error;
298 use crate::input::read_toml;
299 use crate::model::ModelParameters;
300 use crate::patch::{FilePatch, ModelPatch};
301
302 #[test]
303 fn modify_base_with_patch_works() {
304 let base = "col1,col2\nvalue1,value2\nvalue3,value4\nvalue5,value6\n";
305
306 let patch = FilePatch::new("test.csv")
308 .with_header("col1,col2")
309 .with_deletion("value3,value4")
310 .with_addition("value7,value8");
311
312 let modified = modify_base_with_patch(base, &patch).unwrap();
313
314 let lines: Vec<&str> = modified.lines().collect();
315 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")); }
321
322 #[test]
323 fn modify_base_with_patch_mismatched_header() {
324 let base = "col1,col2\nvalue1,value2\n";
325
326 let patch = FilePatch::new("test.csv").with_header("col1,col3");
328
329 assert_error!(
330 modify_base_with_patch(base, &patch),
331 "Header mismatch: base file has [col1, col2], patch has [col1, col3]"
332 );
333 }
334
335 #[test]
336 fn merge_model_toml_basic() {
337 let base = r#"
338 field = "data"
339 [section]
340 a = 1
341 "#;
342
343 let mut patch = toml::value::Table::new();
345 patch.insert(
346 "field".to_string(),
347 toml::Value::String("patched".to_string()),
348 );
349 patch.insert(
350 "new_field".to_string(),
351 toml::Value::String("added".to_string()),
352 );
353
354 let merged = merge_model_toml(base, &patch).unwrap();
357 assert!(merged.contains("field = \"patched\""));
358 assert!(merged.contains("[section]"));
359 assert!(merged.contains("new_field = \"added\""));
360 }
361
362 #[test]
363 fn file_patch() {
364 let assets_patch = FilePatch::new("assets.csv")
366 .with_deletion("GASDRV,GBR,A0_GEX,4002.26,2020")
367 .with_addition("GASDRV,GBR,A0_GEX,4003.26,2020");
368
369 let model_dir = ModelPatch::from_example("simple")
371 .with_file_patch(assets_patch)
372 .build_to_tempdir()
373 .unwrap();
374
375 let assets_path = model_dir.path().join("assets.csv");
377 let assets_content = std::fs::read_to_string(assets_path).unwrap();
378 assert!(!assets_content.contains("GASDRV,GBR,A0_GEX,4002.26,2020"));
379 assert!(assets_content.contains("GASDRV,GBR,A0_GEX,4003.26,2020"));
380 }
381
382 #[test]
383 fn toml_patch() {
384 let toml_patch = "milestone_years = [2020, 2030, 2040, 2050]\n";
386
387 let model_dir = ModelPatch::from_example("simple")
389 .with_toml_patch(toml_patch)
390 .build_to_tempdir()
391 .unwrap();
392
393 let toml: ModelParameters = read_toml(&model_dir.path().join("model.toml")).unwrap();
395 assert_eq!(toml.milestone_years, vec![2020, 2030, 2040, 2050]);
396 }
397}