From 7e5bb2ef4a231517ffeca288b247cab3161f77d3 Mon Sep 17 00:00:00 2001 From: Jeroen Van Goey Date: Fri, 19 Jun 2026 21:58:54 +0200 Subject: [PATCH] Handle empty ProForma formulas --- mzcore/src/chemistry/formula/formula.rs | 5 +++++ mzcore/src/chemistry/formula/pro_forma.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/mzcore/src/chemistry/formula/formula.rs b/mzcore/src/chemistry/formula/formula.rs index c6a039e6..c2b19e1f 100644 --- a/mzcore/src/chemistry/formula/formula.rs +++ b/mzcore/src/chemistry/formula/formula.rs @@ -352,5 +352,10 @@ mod tests { MolecularFormula::pro_forma::("(empty)"), MolecularFormula::pro_forma::("H0") ); + assert_eq!( + MolecularFormula::pro_forma::(""), + MolecularFormula::pro_forma::("H0") + ); + assert!(MolecularFormula::pro_forma::("").is_err()); } } diff --git a/mzcore/src/chemistry/formula/pro_forma.rs b/mzcore/src/chemistry/formula/pro_forma.rs index 6b92d8a0..f5d66788 100644 --- a/mzcore/src/chemistry/formula/pro_forma.rs +++ b/mzcore/src/chemistry/formula/pro_forma.rs @@ -75,7 +75,7 @@ impl MolecularFormula { range: impl RangeBounds, ) -> Result> { let (mut index, end) = range.bounds(value.len().saturating_sub(1)); - if index > end || &value[index..=end] == "(empty)" { + if value.is_empty() || index > end || value.get(index..=end) == Some("(empty)") { return if ALLOW_EMPTY { Ok(Self::default()) } else {