Skip to main content

quoracle/
error.rs

1//! Error types for the Quoracle library
2
3use thiserror::Error;
4
5/// Result type alias for Quoracle operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur when working with quorum systems
9#[derive(Error, Debug, Clone, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12    /// Some write quorum does not intersect some read quorum.
13    #[error("not all read quorums intersect all write quorums")]
14    NonOverlappingQuorums,
15
16    /// The constraints are infeasible, or there are no f-resilient
17    /// quorums for the requested `f`.
18    #[error("no strategy found satisfying the constraints")]
19    NoStrategyFound,
20
21    /// Search found no quorum system satisfying the requirements.
22    #[error("no quorum system found satisfying the requirements")]
23    NoQuorumSystemFound,
24
25    /// Invalid read/write distribution.
26    #[error("invalid distribution: {0}")]
27    InvalidDistribution(String),
28
29    /// The LP solver failed for a reason other than infeasibility.
30    #[error("LP solver error: {0}")]
31    LpError(String),
32
33    /// Invalid arguments to a quorum system or strategy operation.
34    #[error("invalid quorum system: {0}")]
35    InvalidQuorumSystem(String),
36
37    /// Invalid expression or geometry arguments.
38    #[error("invalid expression: {0}")]
39    InvalidExpression(String),
40}
41
42impl From<good_lp::ResolutionError> for Error {
43    fn from(e: good_lp::ResolutionError) -> Self {
44        match e {
45            good_lp::ResolutionError::Infeasible => Self::NoStrategyFound,
46            other => Self::LpError(other.to_string()),
47        }
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn resolution_error_mapping() {
57        assert_eq!(
58            Error::from(good_lp::ResolutionError::Infeasible),
59            Error::NoStrategyFound
60        );
61        assert!(matches!(
62            Error::from(good_lp::ResolutionError::Unbounded),
63            Error::LpError(_)
64        ));
65        assert!(Error::NonOverlappingQuorums.to_string().contains("intersect"));
66    }
67}