1use thiserror::Error;
9
10#[derive(Debug, Error, Clone, PartialEq)]
12pub enum SdkError {
13 #[error("invalid residual score: {0}")]
15 InvalidScore(String),
16
17 #[error("invalid energy weight: {0}")]
19 InvalidWeight(String),
20
21 #[error("invalid gate parameter: {0}")]
23 InvalidGate(String),
24
25 #[error("invalid stability claim: {0}")]
27 InvalidStability(String),
28
29 #[error("spectral computation error: {0}")]
31 Spectral(String),
32
33 #[error("srbn kernel error: {0}")]
35 Kernel(String),
36
37 #[error("domain error: {0}")]
39 Domain(String),
40}
41
42impl From<srbn::Error> for SdkError {
43 fn from(err: srbn::Error) -> Self {
44 SdkError::Kernel(err.to_string())
45 }
46}
47
48pub type Result<T> = std::result::Result<T, SdkError>;
50
51pub(crate) fn check_non_negative_finite(value: f64, what: &str) -> Result<()> {
53 if !value.is_finite() {
54 return Err(SdkError::InvalidScore(format!(
55 "{what} is not finite: {value}"
56 )));
57 }
58 if value < 0.0 {
59 return Err(SdkError::InvalidScore(format!(
60 "{what} is negative: {value}"
61 )));
62 }
63 Ok(())
64}
65
66pub(crate) fn check_positive_finite(value: f64, what: &str) -> Result<()> {
68 if !value.is_finite() {
69 return Err(SdkError::InvalidWeight(format!(
70 "{what} is not finite: {value}"
71 )));
72 }
73 if value <= 0.0 {
74 return Err(SdkError::InvalidWeight(format!(
75 "{what} must be strictly positive: {value}"
76 )));
77 }
78 Ok(())
79}