From 32952dcb4c703f3204fbdb14659e8a3eb8482f53 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 17 Jan 2024 09:53:08 +0100 Subject: [PATCH] Use `BinRemapper::new` to construct `BinRemapper` in `BinRemapper::from_str` --- CHANGELOG.md | 2 ++ pineappl/src/bin.rs | 32 ++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb465779..dd9d3714 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - when calling `BinRemapper::new`, the limits are now checked for overlaps, in which case a new error is returned +- changed the type `ParseBinRemapperError` to allow capturing errors from + `BinRemapper::new` ## [0.6.3] - 12/12/2023 diff --git a/pineappl/src/bin.rs b/pineappl/src/bin.rs index 88be70eb..4d251d9e 100644 --- a/pineappl/src/bin.rs +++ b/pineappl/src/bin.rs @@ -95,8 +95,18 @@ pub struct BinInfo<'a> { /// Error type returned by [`BinRemapper::from_str`] #[derive(Debug, Error)] -#[error("{0}")] -pub struct ParseBinRemapperError(String); +pub enum ParseBinRemapperError { + /// An error that occured while parsing the string in [`BinRemapper::from_str`]. + #[error("{0}")] + Error(String), + /// An error that occured while constructing the remapper with [`BinRemapper::new`]. + #[error("{source}")] + BinRemapperNewError { + // TODO: enable #[backtrace] whenever the feature is stable + /// The error returned by [`BinRemapper::new`]. + source: BinRemapperNewError, + }, +} impl FromStr for BinRemapper { type Err = ParseBinRemapperError; @@ -112,7 +122,7 @@ impl FromStr for BinRemapper { .split_once(':') .map_or(Ok(string), |(lhs, rhs)| { match (lhs.trim().parse::(), rhs.trim().parse::()) { - (Err(lhs), Err(rhs)) => Err(ParseBinRemapperError(format!( + (Err(lhs), Err(rhs)) => Err(ParseBinRemapperError::Error(format!( "unable to parse 'N:M' syntax from: '{string}' (N: '{lhs}', M: '{rhs}')" ))), // skip :N specification @@ -130,7 +140,7 @@ impl FromStr for BinRemapper { None } else { Some(string.parse::().map_err(|err| { - ParseBinRemapperError(format!( + ParseBinRemapperError::Error(format!( "unable to parse limit '{string}': '{err}')" )) })) @@ -145,7 +155,7 @@ impl FromStr for BinRemapper { if let Some(first) = remaps.first() { if first.len() != 1 { - return Err(ParseBinRemapperError( + return Err(ParseBinRemapperError::Error( "'|' syntax not meaningful for first dimension".to_string(), )); } @@ -156,7 +166,7 @@ impl FromStr for BinRemapper { for i in 1..vec.len() { if vec[i].is_empty() { if vec[i - 1].is_empty() { - return Err(ParseBinRemapperError( + return Err(ParseBinRemapperError::Error( "empty repetition with '|'".to_string(), )); } @@ -187,7 +197,7 @@ impl FromStr for BinRemapper { } if vec.len() <= 1 { - return Err(ParseBinRemapperError( + return Err(ParseBinRemapperError::Error( "no limits due to ':' syntax".to_string(), )); } @@ -236,7 +246,7 @@ impl FromStr for BinRemapper { buffer.push((left, right)); normalization *= right - left; } else { - return Err(ParseBinRemapperError( + return Err(ParseBinRemapperError::Error( "missing '|' specification: number of variants too small".to_string(), )); } @@ -246,10 +256,8 @@ impl FromStr for BinRemapper { normalizations.push(normalization); } - Ok(Self { - normalizations, - limits, - }) + BinRemapper::new(normalizations, limits) + .map_err(|err| ParseBinRemapperError::BinRemapperNewError { source: err }) } }