1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::num::NonZeroU16;
use context_error::*;
use crate::{
chemistry::{ELEMENT_PARSE_LIST, Element, MolecularFormula},
helper_functions::{self, RangeExtension, explain_number_error},
};
impl MolecularFormula {
/// Parse a molecular formula from an XL-MOD formula.
/// # Errors
/// If the formula is not valid according to the XL-MOD molecular formula format, with some help
/// on what is going wrong.
/// ```rust
/// use mzcore::prelude::*;
/// assert_eq!(
/// MolecularFormula::xlmod("C7 D10 H2 N4").unwrap(),
/// molecular_formula!(C 7 [2 H 10] H 2 N 4)
/// );
/// assert_eq!(
/// MolecularFormula::xlmod("-C1 -H2 O1").unwrap(),
/// molecular_formula!(C -1 H -2 O 1)
/// );
/// assert_eq!(
/// MolecularFormula::xlmod("13C6 H6 O2").unwrap(),
/// molecular_formula!([13 C 6] H 6 O 2)
/// );
/// ```
pub fn xlmod(value: &str) -> Result<Self, BoxedError<'_, BasicKind>> {
Self::xlmod_inner(&Context::default().lines(0, value), value, 0..value.len())
}
/// This parses a substring of the given string as an XL-MOD molecular formula definition.
/// Additionally, this allows passing a base context to allow to set the line index and source
/// and other properties. Note that the base context is assumed to contain the full line at
/// line index 0.
///
/// # Errors
/// It fails when the string is not a valid XL-MOD molecular formula string.
#[expect(clippy::missing_panics_doc)] // 2 is not zero, but that cannot be proven at compile time
pub fn xlmod_inner<'a>(
base_context: &Context<'a>,
value: &'a str,
range: std::ops::Range<usize>,
) -> Result<Self, BoxedError<'a, BasicKind>> {
let (start, end) = range.bounds(value.len());
let mut formula = Self::default();
for (offset, block) in helper_functions::split_ascii_whitespace(&value[start..=end]) {
let negative = block.starts_with('-');
let isotope_len = block
.chars()
.skip(usize::from(negative))
.take_while(char::is_ascii_digit)
.count();
let number_length = block.chars().rev().take_while(char::is_ascii_digit).count();
if number_length + isotope_len + usize::from(negative) >= block.len() {
return Err(BoxedError::new(
BasicKind::Error,
"Invalid Xlmod molecular formula",
"No element is defined",
base_context.clone().add_highlight((0, offset, block.len())),
));
}
let element_len = block.len() - number_length - isotope_len - usize::from(negative);
let element = &block[isotope_len + usize::from(negative)..block.len() - number_length];
let (mut isotope, element) = if element == "D" {
if isotope_len == 0 {
(Some(NonZeroU16::new(2).unwrap()), Element::H)
} else {
return Err(BoxedError::new(
BasicKind::Error,
"Invalid Xlmod molecular formula",
"A deuterium cannot have a defined isotope as deuterium is by definition always isotope 2 of hydrogen",
base_context.clone().add_highlight((
0,
offset + usize::from(negative),
isotope_len,
)),
));
}
} else {
let mut found = None;
for possible in ELEMENT_PARSE_LIST {
if element == possible.0 {
found = Some(possible.1);
break;
}
}
if let Some(element) = found {
(None, element)
} else {
return Err(BoxedError::new(
BasicKind::Error,
"Invalid Xlmod molecular formula",
"Not a valid character in formula",
base_context.clone().add_highlight((
0,
offset + usize::from(negative) + isotope_len,
element_len,
)),
));
}
};
if isotope.is_none() && isotope_len > 0 {
isotope = Some(
block[usize::from(negative)..usize::from(negative) + isotope_len]
.parse::<NonZeroU16>()
.map_err(|err| {
BoxedError::new(
BasicKind::Error,
"Invalid Xlmod molecular formula",
format!("The isotope number {}", explain_number_error(&err)),
base_context.clone().add_highlight((
0,
offset + usize::from(negative),
isotope_len,
)),
)
})?,
);
}
let number = if negative { -1 } else { 1 }
* if number_length == 0 {
1
} else {
block[block.len() - number_length..].parse::<i32>().map_err(|err| {
BoxedError::new(
BasicKind::Error,
"Invalid Xlmod molecular formula",
format!("The element count {}", explain_number_error(&err)),
base_context.clone().add_highlight((
0,
offset + usize::from(negative) + isotope_len + element_len,
number_length,
)),
)
})?
};
if let Err(err) = Self::add(&mut formula, (element, isotope, number)) {
return Err(BoxedError::new(
BasicKind::Error,
"Invalid Xlmod molecular formula",
err.reason(),
base_context.clone().add_highlight((
0,
offset + usize::from(negative),
isotope_len + element_len,
)),
));
}
}
Ok(formula)
}
}