Skip to content

Commit dee2e0b

Browse files
committed
Added backwards compatible json parser for custom models and custom modification json files
1 parent 890a392 commit dee2e0b

21 files changed

Lines changed: 1317 additions & 214 deletions

fuzz/fuzz.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::time::Duration;
1616

1717
use std::thread;
1818

19-
#[derive(Parser, Debug)]
19+
#[derive(Debug, Parser)]
2020
#[clap(version)]
2121
struct Args {
2222
#[clap(help = "Name of the target")]

rustyms/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ rand = { workspace = true, optional = true }
2626
rayon = { workspace = true, optional = true }
2727
regex = { workspace = true }
2828
serde = { workspace = true }
29+
serde_json = { workspace = true }
2930
similar = { workspace = true }
3031
swash = { workspace = true, optional = true }
3132
thin-vec = { workspace = true }
@@ -35,7 +36,6 @@ zeno = { workspace = true, optional = true }
3536
[dev-dependencies]
3637
base64 = { workspace = true }
3738
png = { workspace = true }
38-
serde_json = { workspace = true }
3939
directories = { workspace = true }
4040

4141
[features]

rustyms/src/annotation/model/built_in.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -584,21 +584,3 @@ pub(super) static GLYCAN_LOSSES: LazyLock<Vec<(MonoSaccharide, bool, Vec<Neutral
584584
),
585585
]
586586
});
587-
588-
#[test]
589-
#[expect(clippy::missing_panics_doc)]
590-
fn test_reading_custom_models_json_2024() {
591-
use serde_json;
592-
let data = include_str!("custom_model_2024.json");
593-
let mods: Vec<(String, FragmentationModel)> = serde_json::from_str(data).unwrap();
594-
assert!(mods.len() > 1);
595-
}
596-
597-
#[test]
598-
#[expect(clippy::missing_panics_doc)]
599-
fn test_reading_custom_models_json_2025() {
600-
use serde_json;
601-
let data = include_str!("custom_model_20250528.json");
602-
let mods: Vec<(String, FragmentationModel)> = serde_json::from_str(data).unwrap();
603-
assert!(mods.len() > 1);
604-
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

rustyms/src/annotation/model/glycan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ impl GlycanModel {
152152
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
153153
pub struct GlycanPeptideFragment {
154154
/// The full glycan stays attached
155-
full: bool,
155+
pub(super) full: bool,
156156
/// The glycan fragments and at any number of monosaccharides within the range (min, max, inclusive) stay attached (any fucoses on these fragments are always included and do not count towards the limit)
157-
core: Option<(u8, u8)>,
157+
pub(super) core: Option<(u8, u8)>,
158158
}
159159

160160
impl std::ops::Add<&Self> for GlycanPeptideFragment {

0 commit comments

Comments
 (0)