Skip to content

Commit dbd6d66

Browse files
committed
Remove anyhow
Signed-off-by: Javier G. Sogo <[email protected]>
1 parent fd13826 commit dbd6d66

File tree

2 files changed

+54
-50
lines changed

2 files changed

+54
-50
lines changed

src/errors.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub enum Error {
5454
}
5555

5656
#[derive(Debug, Error)]
57-
#[error("sdaf '{0}'")]
57+
#[error(transparent)]
5858
pub struct EntityEvaluationError(pub(crate) SegmentEvaluationError);
5959

6060
impl From<SegmentEvaluationError> for Error {
@@ -110,51 +110,49 @@ impl<T> From<PoisonError<T>> for ConfigurationAccessError {
110110
}
111111
}
112112

113-
#[derive(Debug, Error)]
114-
pub(crate) enum CheckOperatorErrorDetail {
115-
#[error("Entity attribute is not a string.")]
116-
StringExpected,
117-
118-
#[error("Entity attribute has unexpected type: Boolean.")]
119-
BooleanExpected(#[from] std::str::ParseBoolError),
120-
121-
#[error("Entity attribute has unexpected type: Number.")]
122-
NumberExpected(#[from] std::num::ParseFloatError),
123-
124-
#[error("Entity attribute is not a number.")]
125-
EntityAttrNotANumber,
126-
127-
#[error("Operator not implemented.")]
128-
OperatorNotImplemented,
129-
}
130-
131113
#[derive(Debug, Error)]
132114
pub(crate) enum SegmentEvaluationError {
133-
#[error("Operation")]
115+
#[error(transparent)]
134116
SegmentEvaluationFailed(#[from] SegmentEvaluationErrorKind),
135117

136118
#[error("Segment ID '{0}' not found")]
137119
SegmentIdNotFound(String),
138120
}
139121
#[derive(Debug, Error)]
140-
#[error("Operation")]
122+
#[error("Operation '{}' '{}' '{}' failed to evaluate: {}", segment_rule.attribute_name, segment_rule.operator, value, source)]
141123
pub(crate) struct SegmentEvaluationErrorKind {
142124
pub(crate) segment: Segment,
143125
pub(crate) segment_rule: SegmentRule,
144-
pub(crate) attr_value: AttrValue,
126+
pub(crate) value: String,
145127
pub(crate) source: CheckOperatorErrorDetail,
146128
}
147129

148-
impl From<(CheckOperatorErrorDetail, &Segment, &SegmentRule, &AttrValue)>
149-
for SegmentEvaluationError
150-
{
151-
fn from(value: (CheckOperatorErrorDetail, &Segment, &SegmentRule, &AttrValue)) -> Self {
152-
let (source, segment, segment_rule, attr_value) = value;
130+
impl From<(CheckOperatorErrorDetail, &Segment, &SegmentRule, &String)> for SegmentEvaluationError {
131+
fn from(value: (CheckOperatorErrorDetail, &Segment, &SegmentRule, &String)) -> Self {
132+
let (source, segment, segment_rule, value) = value;
153133
Self::SegmentEvaluationFailed(SegmentEvaluationErrorKind {
154134
segment: segment.clone(),
155135
segment_rule: segment_rule.clone(),
156-
attr_value: attr_value.clone(),
136+
value: value.clone(),
157137
source,
158138
})
159139
}
160140
}
141+
142+
#[derive(Debug, Error)]
143+
pub(crate) enum CheckOperatorErrorDetail {
144+
#[error("Entity attribute is not a string.")]
145+
StringExpected,
146+
147+
#[error("Entity attribute has unexpected type: Boolean.")]
148+
BooleanExpected(#[from] std::str::ParseBoolError),
149+
150+
#[error("Entity attribute has unexpected type: Number.")]
151+
NumberExpected(#[from] std::num::ParseFloatError),
152+
153+
#[error("Entity attribute is not a number.")]
154+
EntityAttrNotANumber,
155+
156+
#[error("Operator not implemented.")]
157+
OperatorNotImplemented,
158+
}

src/segment_evaluation.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ fn belong_to_segment(
9696
.values
9797
.iter()
9898
.find_map(|value| match check_operator(attr_value, operator, value) {
99-
Ok(true) => Some(Ok(())),
99+
Ok(true) => Some(Ok::<_, SegmentEvaluationError>(())),
100100
Ok(false) => None,
101-
Err(e) => Some(Err(e)),
101+
Err(e) => Some(Err((e, segment, rule, value).into())),
102102
})
103-
.transpose()
104-
.map_err(|e| (e, segment, rule, attr_value))?;
103+
.transpose()?;
105104
// check if the candidate is good, or if the operator failed:
106105
candidate.is_some()
107106
}
@@ -165,11 +164,11 @@ fn check_operator(
165164
#[cfg(test)]
166165
pub mod tests {
167166
use super::*;
167+
use crate::errors::{EntityEvaluationError, Error, SegmentEvaluationErrorKind};
168168
use crate::{
169169
models::{ConfigValue, Segment, SegmentRule, Segments, TargetingRule},
170170
AttrValue,
171171
};
172-
use crate::errors::Error;
173172
use rstest::*;
174173

175174
#[fixture]
@@ -245,11 +244,20 @@ pub mod tests {
245244
// Failed to evaluate entity: Failed to evaluate entity 'a2' against targeting rule '0'.
246245
// Caused by: Segment 'non_existing_segment_id' not found.
247246
// We are checking here that the parts are present to allow debugging of config by the user:
248-
let msg = rule.unwrap_err().to_string();
249-
assert!(msg.contains("'a2'"));
250-
assert!(msg.contains("'0'"));
251-
assert!(msg.contains("'non_existing_segment_id'"));
252-
assert!(msg.contains("not found"));
247+
let e = rule.unwrap_err();
248+
assert!(matches!(e, Error::EntityEvaluationError(_)));
249+
let Error::EntityEvaluationError(EntityEvaluationError(
250+
SegmentEvaluationError::SegmentIdNotFound(ref segment_id),
251+
)) = e
252+
else {
253+
panic!("Error type mismatch!");
254+
};
255+
assert_eq!(segment_id, "non_existing_segment_id");
256+
// let msg = rule.unwrap_err().to_string();
257+
// assert!(msg.contains("'a2'"));
258+
// assert!(msg.contains("'0'"));
259+
// assert!(msg.contains("'non_existing_segment_id'"));
260+
// assert!(msg.contains("not found"));
253261
}
254262

255263
// SCENARIO - evaluating an operator fails. Meaning, [for example] user has added a numeric value(int/float) in appconfig segment attribute, but in their application they pass the attribute with a boolean value.
@@ -270,17 +278,15 @@ pub mod tests {
270278
// We are checking here that the parts are present to allow debugging of config by the user:
271279

272280
let e = rule.unwrap_err();
273-
assert!(matches!(
274-
e,
275-
Error::EntityEvaluationError(ref v)
276-
));
277-
278-
let msg = e.to_string();
279-
assert_eq!(msg, "lol");
280-
assert!(msg.contains("'a2'"));
281-
assert!(msg.contains("'0'"));
282-
assert!(msg.contains("'some_segment_id_1'"));
283-
assert!(msg.contains("'name' 'is' 'heinz'"));
284-
assert!(msg.contains("Entity attribute has unexpected type: Number"));
281+
assert!(matches!(e, Error::EntityEvaluationError(_)));
282+
let Error::EntityEvaluationError(EntityEvaluationError(
283+
SegmentEvaluationError::SegmentEvaluationFailed(ref error),
284+
)) = e
285+
else {
286+
panic!("Error type mismatch!");
287+
};
288+
assert_eq!(error.segment.name, "");
289+
assert_eq!(error.segment_rule.attribute_name, "name");
290+
assert_eq!(error.value, "heinz");
285291
}
286292
}

0 commit comments

Comments
 (0)