1use thiserror::Error;
2
3pub type LadduPhysicsResult<T> = Result<T, LadduPhysicsError>;
5
6#[derive(Error, Debug, Clone)]
7pub enum LadduPhysicsError {
9 #[error("Failed to convert value to \"{0}\"")]
11 ConversionError(&'static str),
12 #[error("Failed to parse string: \"{name}\" does not correspond to a valid \"{object}\"!")]
15 ParseError {
16 name: String,
18 object: String,
20 },
21 #[error("Particle is missing the requested property \"{property}\"")]
23 MissingParticleProperty {
24 property: &'static str,
26 },
27 #[error("Invalid value for {name}: expected {expected}, got {actual}")]
29 InvalidValue {
30 name: String,
32 expected: String,
34 actual: String,
36 },
37 #[error("Invalid length for {name}: expected {expected}, got {actual}")]
39 InvalidLength {
40 name: String,
42 expected: String,
44 actual: String,
46 },
47 #[error("Invalid relation: {relation}")]
49 InvalidRelation {
50 relation: String,
52 },
53
54 #[error("Unsupported value for {name}: supported {supported}, got {actual}")]
56 UnsupportedValue {
57 name: String,
59 supported: String,
61 actual: String,
63 },
64
65 #[error("Numeric overflow while computing {operation}")]
67 NumericOverflow {
68 operation: String,
70 },
71 #[error("{0}")]
73 Custom(String),
74}
75
76impl LadduPhysicsError {
77 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 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 pub fn invalid_relation(relation: impl Into<String>) -> Self {
105 Self::InvalidRelation {
106 relation: relation.into(),
107 }
108 }
109
110 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 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}