Skip to content

Commit ce9191d

Browse files
committed
consolidate
1 parent 14df075 commit ce9191d

File tree

4 files changed

+31
-55
lines changed

4 files changed

+31
-55
lines changed

src/input/input_abstract.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,7 @@ pub trait Input<'py>: fmt::Debug + ToPyObject {
9898

9999
fn validate_float(&self, strict: bool) -> ValMatch<EitherFloat<'_>>;
100100

101-
fn validate_decimal(&self, strict: bool, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
102-
if strict {
103-
self.strict_decimal(py)
104-
} else {
105-
self.lax_decimal(py)
106-
}
107-
}
108-
fn strict_decimal(&self, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>>;
109-
#[cfg_attr(has_coverage_attribute, coverage(off))]
110-
fn lax_decimal(&self, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
111-
self.strict_decimal(py)
112-
}
101+
fn validate_decimal(&self, strict: bool, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>>;
113102

114103
type Dict<'a>: ValidatedDict<'py>
115104
where

src/input/input_json.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
165165
}
166166
}
167167

168-
fn strict_decimal(&self, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
168+
fn validate_decimal(&self, _strict: bool, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
169169
match self {
170170
JsonValue::Float(f) => {
171171
create_decimal(&PyString::new_bound(py, &f.to_string()), self).map(ValidationMatch::strict)
@@ -374,8 +374,8 @@ impl<'py> Input<'py> for str {
374374
str_as_float(self, self).map(ValidationMatch::lax)
375375
}
376376

377-
fn strict_decimal(&self, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
378-
create_decimal(self.to_object(py).bind(py), self).map(ValidationMatch::strict)
377+
fn validate_decimal(&self, _strict: bool, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
378+
create_decimal(self.to_object(py).bind(py), self).map(ValidationMatch::lax)
379379
}
380380

381381
type Dict<'a> = Never;

src/input/input_python.rs

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
248248
str_as_int(self, s)
249249
} else if self.is_exact_instance_of::<PyFloat>() {
250250
float_as_int(self, self.extract::<f64>()?)
251-
} else if let Ok(decimal) = self.strict_decimal(self.py()) {
251+
} else if let Ok(decimal) = self.validate_decimal(strict, self.py()) {
252252
decimal_as_int(self, &decimal.into_inner())
253253
} else if let Ok(float) = self.extract::<f64>() {
254254
float_as_int(self, float)
@@ -307,51 +307,38 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
307307
Err(ValError::new(ErrorTypeDefaults::FloatType, self))
308308
}
309309

310-
fn strict_decimal(&self, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
310+
fn validate_decimal(&self, strict: bool, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
311311
let decimal_type = get_decimal_type(py);
312-
// Fast path for existing decimal objects
313-
if self.is_exact_instance(decimal_type) {
314-
return Ok(ValidationMatch::exact(self.to_owned()));
315-
}
316-
317-
// Try subclasses of decimals, they will be upcast to Decimal
318-
if self.is_instance(decimal_type)? {
319-
return create_decimal(self, self).map(ValidationMatch::strict);
320-
}
321-
322-
Err(ValError::new(
323-
ErrorType::IsInstanceOf {
324-
class: decimal_type
325-
.qualname()
326-
.and_then(|name| name.extract())
327-
.unwrap_or_else(|_| "Decimal".to_owned()),
328-
context: None,
329-
},
330-
self,
331-
))
332-
}
333312

334-
fn lax_decimal(&self, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
335-
let decimal_type = get_decimal_type(py);
336313
// Fast path for existing decimal objects
337314
if self.is_exact_instance(decimal_type) {
338-
return Ok(ValidationMatch::exact(self.to_owned().clone()));
339-
}
340-
341-
// TODO: I can see the case for int and float being strict - wdyt @davidhewitt?
342-
return if self.is_instance_of::<PyString>()
343-
|| (self.is_instance_of::<PyInt>() && !self.is_instance_of::<PyBool>())
344-
{
345-
// checking isinstance for str / int / bool is fast compared to decimal / float
346-
create_decimal(self, self).map(ValidationMatch::lax)
315+
Ok(ValidationMatch::exact(self.to_owned().clone()))
347316
} else if self.is_instance(decimal_type)? {
348-
// upcast subclasses to decimal
317+
// Upcast subclasses to decimal
349318
create_decimal(self, self).map(ValidationMatch::strict)
350-
} else if self.is_instance_of::<PyFloat>() {
351-
create_decimal(self.str()?.as_any(), self).map(ValidationMatch::lax)
352319
} else {
353-
Err(ValError::new(ErrorTypeDefaults::DecimalType, self))
354-
};
320+
if strict {
321+
return Err(ValError::new(
322+
ErrorType::IsInstanceOf {
323+
class: decimal_type
324+
.qualname()
325+
.and_then(|name| name.extract())
326+
.unwrap_or_else(|_| "Decimal".to_owned()),
327+
context: None,
328+
},
329+
self,
330+
));
331+
}
332+
if self.is_instance_of::<PyString>() || (self.is_instance_of::<PyInt>() && !self.is_instance_of::<PyBool>())
333+
{
334+
// Checking isinstance for str / int / bool is fast compared to decimal / float
335+
create_decimal(self, self).map(ValidationMatch::lax)
336+
} else if self.is_instance_of::<PyFloat>() {
337+
create_decimal(self.str()?.as_any(), self).map(ValidationMatch::lax)
338+
} else {
339+
Err(ValError::new(ErrorTypeDefaults::DecimalType, self))
340+
}
341+
}
355342
}
356343

357344
type Dict<'a> = GenericPyMapping<'a, 'py> where Self: 'a;

src/input/input_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl<'py> Input<'py> for StringMapping<'py> {
141141
}
142142
}
143143

144-
fn strict_decimal(&self, _py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
144+
fn validate_decimal(&self, _strict: bool, _py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
145145
match self {
146146
Self::String(s) => create_decimal(s, self).map(ValidationMatch::strict),
147147
Self::Mapping(_) => Err(ValError::new(ErrorTypeDefaults::DecimalType, self)),

0 commit comments

Comments
 (0)