Skip to main content

logit_loom_core/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Validation and deterministic-encoding failures.
4
5use thiserror::Error;
6
7/// Failures raised while constructing or validating public contracts.
8#[non_exhaustive]
9#[derive(Debug, Error)]
10pub enum CoreError {
11    /// A required field was empty.
12    #[error("{field} must not be empty")]
13    Empty {
14        /// Stable field name.
15        field: &'static str,
16    },
17    /// A floating-point field was NaN or infinite.
18    #[error("{field} must be finite")]
19    NonFinite {
20        /// Stable field name.
21        field: &'static str,
22    },
23    /// A field violated a bounded structural contract.
24    #[error("{field} is invalid: {reason}")]
25    Invalid {
26        /// Stable field name.
27        field: &'static str,
28        /// Bounded explanation.
29        reason: String,
30    },
31    /// Deterministic JSON encoding failed.
32    #[error("unable to encode contract: {0}")]
33    Serialization(#[from] serde_json::Error),
34}
35
36impl CoreError {
37    /// Creates a structural validation error.
38    pub fn invalid(field: &'static str, reason: impl Into<String>) -> Self {
39        Self::Invalid {
40            field,
41            reason: reason.into(),
42        }
43    }
44}