Skip to content

Commit

Permalink
Refactor error handling in ConfigFile module
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Oct 16, 2024
1 parent 866d359 commit 129e675
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions crates/core/src/repofile/configfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ use crate::{
/// [`ConfigFileErrorKind`] describes the errors that can be returned for `ConfigFile`s
#[derive(Error, Debug, Display)]
pub enum ConfigFileErrorKind {
/// config version not supported!
ConfigVersionNotSupported,
/// Parsing Polynomial in config failed: `{0:?}`
ParsingFailedForPolynomial(#[from] ParseIntError),
/// config version not supported: {version}, compression: {compression:?}
ConfigVersionNotSupported {
/// The version of the config
version: u32,
/// The compression level
compression: Option<i32>,
},
/// Parsing failed for polynomial: {polynomial} : {source}
ParsingFailedForPolynomial {
polynomial: String,
source: ParseIntError,
},
}

pub(crate) type ConfigFileResult<T> = Result<T, ConfigFileErrorKind>;
Expand Down Expand Up @@ -147,10 +155,16 @@ impl ConfigFile {
///
/// * [`ConfigFileErrorKind::ParsingFailedForPolynomial`] - If the polynomial could not be parsed
///
/// [`ConfigFileErrorKind::ParsingFailedForPolynomial`]: crate::error::ConfigFileErrorKind::ParsingFailedForPolynomial
/// [`ConfigFileErrorKind::ParsingFailedForPolynomial`]: ConfigFileErrorKind::ParsingFailedForPolynomial
pub fn poly(&self) -> ConfigFileResult<u64> {
Ok(u64::from_str_radix(&self.chunker_polynomial, 16)
.map_err(ConfigFileErrorKind::ParsingFailedForPolynomial)?)
Ok(
u64::from_str_radix(&self.chunker_polynomial, 16).map_err(|err| {
ConfigFileErrorKind::ParsingFailedForPolynomial {
polynomial: self.chunker_polynomial.clone(),
source: err,
}
})?,
)
}

/// Get the compression level
Expand All @@ -159,13 +173,16 @@ impl ConfigFile {
///
/// * [`ConfigFileErrorKind::ConfigVersionNotSupported`] - If the version is not supported
///
/// [`ConfigFileErrorKind::ConfigVersionNotSupported`]: crate::error::ConfigFileErrorKind::ConfigVersionNotSupported
/// [`ConfigFileErrorKind::ConfigVersionNotSupported`]: ConfigFileErrorKind::ConfigVersionNotSupported
pub fn zstd(&self) -> ConfigFileResult<Option<i32>> {
match (self.version, self.compression) {
(1, _) | (2, Some(0)) => Ok(None),
(2, None) => Ok(Some(0)), // use default (=0) zstd compression
(2, Some(c)) => Ok(Some(c)),
_ => Err(ConfigFileErrorKind::ConfigVersionNotSupported.into()),
_ => Err(ConfigFileErrorKind::ConfigVersionNotSupported {
version: self.version,
compression: self.compression,
}),
}
}

Expand Down

0 comments on commit 129e675

Please sign in to comment.