Skip to main content

laddu_physics/
error.rs

1use thiserror::Error;
2
3/// Result type used throughout the physics crate.
4pub type LadduPhysicsResult<T> = Result<T, LadduPhysicsError>;
5
6#[derive(Error, Debug, Clone)]
7/// Errors raised while constructing or evaluating physics objects.
8pub enum LadduPhysicsError {
9    /// An error that should be used to convert [`TryFrom`] to [`LadduPhysicsError`].
10    #[error("Failed to convert value to \"{0}\"")]
11    ConversionError(&'static str),
12    /// An error which occurs when the user tries to parse an invalid string of text, typically
13    /// into an enum variant.
14    #[error("Failed to parse string: \"{name}\" does not correspond to a valid \"{object}\"!")]
15    ParseError {
16        /// The string which was parsed
17        name: String,
18        /// The name of the object it failed to parse into
19        object: String,
20    },
21    /// A particle is missing the requested property
22    #[error("Particle is missing the requested property \"{property}\"")]
23    MissingParticleProperty {
24        /// The name of the missing property
25        property: &'static str,
26    },
27    /// A single value violates a domain constraint.
28    #[error("Invalid value for {name}: expected {expected}, got {actual}")]
29    InvalidValue {
30        /// Name of the invalid input.
31        name: String,
32        /// Description of the accepted domain.
33        expected: String,
34        /// Supplied value.
35        actual: String,
36    },
37    /// A collection length or shape is invalid.
38    #[error("Invalid length for {name}: expected {expected}, got {actual}")]
39    InvalidLength {
40        /// Name of the invalid collection.
41        name: String,
42        /// Description of the required length or shape.
43        expected: String,
44        /// Supplied length or shape.
45        actual: String,
46    },
47    /// A relation between multiple values, quantum numbers, or particle properties is invalid.
48    #[error("Invalid relation: {relation}")]
49    InvalidRelation {
50        /// Description of the violated relation.
51        relation: String,
52    },
53
54    /// A value is valid in principle but not implemented/supported here.
55    #[error("Unsupported value for {name}: supported {supported}, got {actual}")]
56    UnsupportedValue {
57        /// Name of the unsupported input.
58        name: String,
59        /// Description of the supported alternatives.
60        supported: String,
61        /// Supplied value.
62        actual: String,
63    },
64
65    /// An integer operation overflowed.
66    #[error("Numeric overflow while computing {operation}")]
67    NumericOverflow {
68        /// Operation that overflowed.
69        operation: String,
70    },
71    /// A free-form error message for internal compatibility paths.
72    #[error("{0}")]
73    Custom(String),
74}
75
76impl LadduPhysicsError {
77    /// Construct an invalid-value error.
78    pub fn invalid_value(
79        name: impl Into<String>,
80        expected: impl Into<String>,
81        actual: impl ToString,
82    ) -> Self {
83        Self::InvalidValue {
84            name: name.into(),
85            expected: expected.into(),
86            actual: actual.to_string(),
87        }
88    }
89
90    /// Construct an invalid-length or shape error.
91    pub fn invalid_length(
92        name: impl Into<String>,
93        expected: impl Into<String>,
94        actual: impl ToString,
95    ) -> Self {
96        Self::InvalidLength {
97            name: name.into(),
98            expected: expected.into(),
99            actual: actual.to_string(),
100        }
101    }
102
103    /// Construct an invalid-relation error.
104    pub fn invalid_relation(relation: impl Into<String>) -> Self {
105        Self::InvalidRelation {
106            relation: relation.into(),
107        }
108    }
109
110    /// Construct an unsupported-value error.
111    pub fn unsupported_value(
112        name: impl Into<String>,
113        supported: impl Into<String>,
114        actual: impl ToString,
115    ) -> Self {
116        Self::UnsupportedValue {
117            name: name.into(),
118            supported: supported.into(),
119            actual: actual.to_string(),
120        }
121    }
122
123    /// Construct a numeric-overflow error.
124    pub fn numeric_overflow(operation: impl Into<String>) -> Self {
125        Self::NumericOverflow {
126            operation: operation.into(),
127        }
128    }
129
130    pub(crate) fn custom(text: impl Into<String>) -> LadduPhysicsError {
131        Self::Custom(text.into())
132    }
133}