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 #[error("grant signature error: {0}")]
43 Signature(String),
44}
45
46impl From<srbn::Error> for SdkError {
47 fn from(err: srbn::Error) -> Self {
48 SdkError::Kernel(err.to_string())
49 }
50}
51
52pub type Result<T> = std::result::Result<T, SdkError>;
54
55pub(crate) fn check_non_negative_finite(value: f64, what: &str) -> Result<()> {
57 if !value.is_finite() {
58 return Err(SdkError::InvalidScore(format!(
59 "{what} is not finite: {value}"
60 )));
61 }
62 if value < 0.0 {
63 return Err(SdkError::InvalidScore(format!(
64 "{what} is negative: {value}"
65 )));
66 }
67 Ok(())
68}
69
70pub(crate) fn check_positive_finite(value: f64, what: &str) -> Result<()> {
72 if !value.is_finite() {
73 return Err(SdkError::InvalidWeight(format!(
74 "{what} is not finite: {value}"
75 )));
76 }
77 if value <= 0.0 {
78 return Err(SdkError::InvalidWeight(format!(
79 "{what} must be strictly positive: {value}"
80 )));
81 }
82 Ok(())
83}