Skip to content

Commit beffb8f

Browse files
refactor: simplify try_from_decimal_to_int function's return type
1 parent 6a393e4 commit beffb8f

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/validators/decimal.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,24 +292,23 @@ fn handle_decimal_new_error(input: impl ToErrorValue, error: PyErr, decimal_exce
292292
pub(crate) fn try_from_decimal_to_int<'a, 'py, I: Input<'py> + ?Sized>(
293293
py: Python<'py>,
294294
input: &'a I,
295-
) -> ValResult<Option<i64>> {
295+
) -> ValResult<i64> {
296296
let Some(py_input) = input.as_python() else {
297-
return Ok(None);
297+
return Err(ValError::new(ErrorTypeDefaults::DecimalType, input));
298298
};
299299

300300
if let Ok(false) = py_input.is_instance(get_decimal_type(py)) {
301-
return Ok(None);
301+
return Err(ValError::new(ErrorTypeDefaults::DecimalType, input));
302302
}
303303

304-
let Ok(EitherInt::Py(dec_value)) = decimal_as_int(input, py_input) else {
305-
return Ok(None);
304+
let dec_value = match decimal_as_int(input, py_input)? {
305+
EitherInt::Py(value) => value,
306+
_ => return Err(ValError::new(ErrorType::DecimalParsing { context: None }, input)),
306307
};
307308

308-
let Ok(either_int) = dec_value.exact_int() else {
309-
return Ok(None);
310-
};
309+
let either_int = dec_value.exact_int()?;
311310

312311
let int = either_int.into_i64(py)?;
313312

314-
Ok(Some(int))
313+
Ok(int)
315314
}

src/validators/literal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<T: Debug> LiteralLookup<T> {
126126
}
127127
}
128128
// if the input is a Decimal type, we need to check if its value is in the expected_ints
129-
if let Ok(Some(value)) = try_from_decimal_to_int(py, input) {
129+
if let Ok(value) = try_from_decimal_to_int(py, input) {
130130
let Some(id) = expected_ints.get(&value) else {
131131
return Ok(None);
132132
};
@@ -154,7 +154,7 @@ impl<T: Debug> LiteralLookup<T> {
154154
}
155155
if !strict {
156156
// if the input is a Decimal type, we need to check if its value is in the expected_ints
157-
if let Ok(Some(value)) = try_from_decimal_to_int(py, input) {
157+
if let Ok(value) = try_from_decimal_to_int(py, input) {
158158
let Some(id) = expected_strings.get(&value.to_string()) else {
159159
return Ok(None);
160160
};

0 commit comments

Comments
 (0)