Skip to main content

sci_cream/
error.rs

1//! Error types for Sci-Cream.
2
3use thiserror::Error;
4
5#[cfg(feature = "wasm")]
6use wasm_bindgen::JsValue;
7
8use crate::specs::Unit;
9
10/// Error type for Sci-Cream.
11#[derive(Error, Debug)]
12pub enum Error {
13    /// Cannot compute energy due to unmet preconditions.
14    #[error("Preconditions for computing energy not met: {0}")]
15    CannotComputeEnergy(String),
16    /// Cannot compute POD due to unmet preconditions.
17    #[error("Preconditions for computing POD not met: {0}")]
18    CannotComputePOD(String),
19    /// Cannot compute PAC due to unmet preconditions.
20    #[error("Preconditions for computing PAC not met: {0}")]
21    CannotComputePAC(String),
22    /// Composition value is not within [0, 100]%.
23    #[error("Composition is not within [0, 100]%: {0}")]
24    CompositionNotWithin100Percent(f64),
25    /// Composition does not sum to 100%.
26    #[error("Composition does not sum to 100%: {0}")]
27    CompositionNot100Percent(f64),
28    /// Composition value is not positive.
29    #[error("Composition value is not positive: {0}")]
30    CompositionNotPositive(f64),
31    /// General invalid composition error.
32    #[error("Invalid composition: {0}")]
33    InvalidComposition(String),
34    /// [PAC](crate::docs#pac-afp-fpdf-se) value is negative.
35    #[error("PAC value cannot be negative: {0}")]
36    NegativePacValue(f64),
37    /// [FPD](crate::docs#pac-afp-fpdf-se) value is positive.
38    #[error("FPD value cannot be positive: {0}")]
39    PositiveFpdValue(f64),
40    /// Composition unit is not supported for this operation.
41    #[error("Composition unit is not supported for this operation: {0}")]
42    UnsupportedCompositionUnit(Unit),
43    /// Ingredient not found in database or embedded list.
44    #[error("Ingredient not found: {0}")]
45    IngredientNotFound(String),
46}
47
48#[cfg(feature = "wasm")]
49impl From<Error> for JsValue {
50    fn from(error: Error) -> Self {
51        Self::from_str(&error.to_string())
52    }
53}
54
55/// Convenience type alias for [`Result<T, sci_cream::error::Error>`].
56pub type Result<T> = std::result::Result<T, Error>;