use thiserror::Error;
#[derive(Debug, Error, Clone, PartialEq)]
pub enum SdkError {
#[error("invalid residual score: {0}")]
InvalidScore(String),
#[error("invalid energy weight: {0}")]
InvalidWeight(String),
#[error("invalid gate parameter: {0}")]
InvalidGate(String),
#[error("invalid stability claim: {0}")]
InvalidStability(String),
#[error("spectral computation error: {0}")]
Spectral(String),
#[error("srbn kernel error: {0}")]
Kernel(String),
#[error("domain error: {0}")]
Domain(String),
}
impl From<srbn::Error> for SdkError {
fn from(err: srbn::Error) -> Self {
SdkError::Kernel(err.to_string())
}
}
pub type Result<T> = std::result::Result<T, SdkError>;
pub(crate) fn check_non_negative_finite(value: f64, what: &str) -> Result<()> {
if !value.is_finite() {
return Err(SdkError::InvalidScore(format!(
"{what} is not finite: {value}"
)));
}
if value < 0.0 {
return Err(SdkError::InvalidScore(format!(
"{what} is negative: {value}"
)));
}
Ok(())
}
pub(crate) fn check_positive_finite(value: f64, what: &str) -> Result<()> {
if !value.is_finite() {
return Err(SdkError::InvalidWeight(format!(
"{what} is not finite: {value}"
)));
}
if value <= 0.0 {
return Err(SdkError::InvalidWeight(format!(
"{what} must be strictly positive: {value}"
)));
}
Ok(())
}