|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + error::{Context, CustomError}, |
| 5 | + parse_json::ParseJson, |
| 6 | + prelude::FragmentationModel, |
| 7 | +}; |
| 8 | + |
| 9 | +/// Parse a custom models JSON string. The parser is guaranteed to be backwards compatible |
| 10 | +/// with any JSON made by the serde serialisation of the custom models in previous version of |
| 11 | +/// the library. |
| 12 | +/// # Errors |
| 13 | +/// If the file could not be opened or parsed. |
| 14 | +pub fn parse_custom_models(path: &Path) -> Result<Vec<(String, FragmentationModel)>, CustomError> { |
| 15 | + let string = std::fs::read_to_string(path).map_err(|err| { |
| 16 | + CustomError::error( |
| 17 | + "Could not parse custom models file", |
| 18 | + err, |
| 19 | + Context::show(path.display()), |
| 20 | + ) |
| 21 | + })?; |
| 22 | + Vec::from_json(&string) |
| 23 | +} |
| 24 | + |
| 25 | +/// Parse a custom models JSON string. The parser is guaranteed to be backwards compatible |
| 26 | +/// with any JSON made by the serde serialisation of the custom models in previous version of |
| 27 | +/// the library. |
| 28 | +/// # Errors |
| 29 | +/// If the string could not be parsed. |
| 30 | +pub fn parse_custom_models_str( |
| 31 | + value: &str, |
| 32 | +) -> Result<Vec<(String, FragmentationModel)>, CustomError> { |
| 33 | + Vec::from_json(value) |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +#[expect(clippy::missing_panics_doc)] |
| 38 | +fn test_reading_custom_models_json_2024() { |
| 39 | + let data = include_str!("custom_model_2024.json"); |
| 40 | + let mods = parse_custom_models_str(data).unwrap(); |
| 41 | + assert!(mods.len() > 1); |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +#[expect(clippy::missing_panics_doc)] |
| 46 | +fn test_reading_custom_models_json_2025() { |
| 47 | + let data = include_str!("custom_model_20250528.json"); |
| 48 | + let mods = parse_custom_models_str(data).unwrap(); |
| 49 | + assert!(mods.len() > 1); |
| 50 | +} |
0 commit comments