Skip to content

Commit

Permalink
Fix enum value literals (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb authored Apr 15, 2023
1 parent 2e2bd34 commit 02ec941
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 326 deletions.
4 changes: 4 additions & 0 deletions src/input/input_abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub trait Input<'a>: fmt::Debug + ToPyObject {
self.strict_str()
}

fn as_str_strict(&self) -> Option<&str>;

fn validate_bytes(&'a self, strict: bool) -> ValResult<EitherBytes<'a>> {
if strict {
self.strict_bytes()
Expand Down Expand Up @@ -129,6 +131,8 @@ pub trait Input<'a>: fmt::Debug + ToPyObject {
self.strict_int()
}

fn as_int_strict(&self) -> Option<i64>;

fn validate_float(&self, strict: bool, ultra_strict: bool) -> ValResult<f64> {
if ultra_strict {
self.ultra_strict_float()
Expand Down
22 changes: 22 additions & 0 deletions src/input/input_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ impl<'a> Input<'a> for JsonInput {
}
}

fn as_str_strict(&self) -> Option<&str> {
match self {
JsonInput::String(s) => Some(s.as_str()),
_ => None,
}
}

fn validate_bytes(&'a self, _strict: bool) -> ValResult<EitherBytes<'a>> {
match self {
JsonInput::String(s) => Ok(s.as_bytes().into()),
Expand Down Expand Up @@ -141,6 +148,13 @@ impl<'a> Input<'a> for JsonInput {
}
}

fn as_int_strict(&self) -> Option<i64> {
match self {
JsonInput::Int(i) => Some(*i),
_ => None,
}
}

fn ultra_strict_float(&self) -> ValResult<f64> {
match self {
JsonInput::Float(f) => Ok(*f),
Expand Down Expand Up @@ -349,6 +363,10 @@ impl<'a> Input<'a> for String {
self.validate_str(false)
}

fn as_str_strict(&self) -> Option<&str> {
Some(self.as_str())
}

fn validate_bytes(&'a self, _strict: bool) -> ValResult<EitherBytes<'a>> {
Ok(self.as_bytes().into())
}
Expand All @@ -374,6 +392,10 @@ impl<'a> Input<'a> for String {
}
}

fn as_int_strict(&self) -> Option<i64> {
None
}

#[cfg_attr(has_no_coverage, no_coverage)]
fn ultra_strict_float(&self) -> ValResult<f64> {
self.strict_float()
Expand Down
28 changes: 28 additions & 0 deletions src/input/input_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ impl<'a> Input<'a> for PyAny {
}
}

fn as_str_strict(&self) -> Option<&str> {
if self.get_type().is(get_py_str_type(self.py())) {
self.extract().ok()
} else {
None
}
}

fn strict_bytes(&'a self) -> ValResult<EitherBytes<'a>> {
if let Ok(py_bytes) = self.downcast::<PyBytes>() {
Ok(py_bytes.into())
Expand Down Expand Up @@ -289,6 +297,14 @@ impl<'a> Input<'a> for PyAny {
}
}

fn as_int_strict(&self) -> Option<i64> {
if self.get_type().is(get_py_int_type(self.py())) {
self.extract().ok()
} else {
None
}
}

fn ultra_strict_float(&self) -> ValResult<f64> {
if matches!(self.is_instance_of::<PyInt>(), Ok(true)) {
Err(ValError::new(ErrorType::FloatType, self))
Expand Down Expand Up @@ -697,3 +713,15 @@ pub fn list_as_tuple(list: &PyList) -> &PyTuple {
};
py_tuple.into_ref(list.py())
}

static PY_INT_TYPE: GILOnceCell<PyObject> = GILOnceCell::new();

fn get_py_int_type(py: Python) -> &PyObject {
PY_INT_TYPE.get_or_init(py, || PyInt::type_object(py).into())
}

static PY_STR_TYPE: GILOnceCell<PyObject> = GILOnceCell::new();

fn get_py_str_type(py: Python) -> &PyObject {
PY_STR_TYPE.get_or_init(py, || PyString::type_object(py).into())
}
Loading

0 comments on commit 02ec941

Please sign in to comment.