Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/input/input_abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub trait Input<'py>: fmt::Debug {

fn validate_dataclass_args<'a>(&'a self, dataclass_name: &str) -> ValResult<Self::Arguments<'a>>;

fn validate_str(&self, strict: bool, coerce_numbers_to_str: bool) -> ValMatch<EitherString<'_>>;
fn validate_str(&self, strict: bool, coerce_numbers_to_str: bool) -> ValMatch<EitherString<'_, 'py>>;

fn validate_bytes<'a>(&'a self, strict: bool, mode: ValBytesMode) -> ValMatch<EitherBytes<'a, 'py>>;

Expand All @@ -103,7 +103,7 @@ pub trait Input<'py>: fmt::Debug {

/// Extract a String from the input, only allowing exact
/// matches for a String (no subclasses)
fn exact_str(&self) -> ValResult<EitherString<'_>> {
fn exact_str(&self) -> ValResult<EitherString<'_, 'py>> {
self.validate_str(true, false).and_then(|val_match| {
val_match
.require_exact()
Expand Down
10 changes: 7 additions & 3 deletions src/input/input_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
}
}

fn validate_str(&self, strict: bool, coerce_numbers_to_str: bool) -> ValResult<ValidationMatch<EitherString<'_>>> {
fn validate_str(
&self,
strict: bool,
coerce_numbers_to_str: bool,
) -> ValResult<ValidationMatch<EitherString<'_, 'py>>> {
// Justification for `strict` instead of `exact` is that in JSON strings can also
// represent other datatypes such as UUID and date more exactly, so string is a
// converting input
Expand Down Expand Up @@ -163,7 +167,7 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
}
}

fn exact_str(&self) -> ValResult<EitherString<'_>> {
fn exact_str(&self) -> ValResult<EitherString<'_, 'py>> {
match self {
JsonValue::Str(s) => Ok(s.as_ref().into()),
_ => Err(ValError::new(ErrorTypeDefaults::StringType, self)),
Expand Down Expand Up @@ -414,7 +418,7 @@ impl<'py> Input<'py> for str {
&self,
_strict: bool,
_coerce_numbers_to_str: bool,
) -> ValResult<ValidationMatch<EitherString<'_>>> {
) -> ValResult<ValidationMatch<EitherString<'_, 'py>>> {
// Justification for `strict` instead of `exact` is that in JSON strings can also
// represent other datatypes such as UUID and date more exactly, so string is a
// converting input
Expand Down
8 changes: 6 additions & 2 deletions src/input/input_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
}
}

fn validate_str(&self, strict: bool, coerce_numbers_to_str: bool) -> ValResult<ValidationMatch<EitherString<'_>>> {
fn validate_str(
&self,
strict: bool,
coerce_numbers_to_str: bool,
) -> ValResult<ValidationMatch<EitherString<'_, 'py>>> {
if let Ok(py_str) = self.downcast_exact::<PyString>() {
return Ok(ValidationMatch::exact(py_str.clone().into()));
} else if let Ok(py_str) = self.downcast::<PyString>() {
Expand Down Expand Up @@ -310,7 +314,7 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
}
}

fn exact_str(&self) -> ValResult<EitherString<'_>> {
fn exact_str(&self) -> ValResult<EitherString<'_, 'py>> {
if let Ok(py_str) = self.downcast_exact() {
Ok(EitherString::Py(py_str.clone()))
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/input/input_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<'py> Input<'py> for StringMapping<'py> {
&self,
_strict: bool,
_coerce_numbers_to_str: bool,
) -> ValResult<ValidationMatch<EitherString<'_>>> {
) -> ValResult<ValidationMatch<EitherString<'_, 'py>>> {
match self {
Self::String(s) => Ok(ValidationMatch::strict(s.clone().into())),
Self::Mapping(_) => Err(ValError::new(ErrorTypeDefaults::StringType, self)),
Expand Down
18 changes: 9 additions & 9 deletions src/input/return_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,41 +468,41 @@ impl<'data> GenericJsonIterator<'data> {
}

#[cfg_attr(debug_assertions, derive(Debug))]
pub enum EitherString<'a> {
pub enum EitherString<'a, 'py> {
Cow(Cow<'a, str>),
Py(Bound<'a, PyString>),
Py(Bound<'py, PyString>),
}

impl<'a> EitherString<'a> {
impl<'py> EitherString<'_, 'py> {
pub fn as_cow(&self) -> ValResult<Cow<'_, str>> {
match self {
Self::Cow(data) => Ok(data.clone()),
Self::Cow(data) => Ok(Cow::Borrowed(data)),
Self::Py(py_str) => Ok(Cow::Borrowed(py_string_str(py_str)?)),
}
}

pub fn as_py_string(&'a self, py: Python<'a>, cache_str: StringCacheMode) -> Bound<'a, PyString> {
pub fn as_py_string(&self, py: Python<'py>, cache_str: StringCacheMode) -> Bound<'py, PyString> {
match self {
Self::Cow(cow) => new_py_string(py, cow.as_ref(), cache_str),
Self::Py(py_string) => py_string.clone(),
}
}
}

impl<'a> From<&'a str> for EitherString<'a> {
impl<'a> From<&'a str> for EitherString<'a, '_> {
fn from(data: &'a str) -> Self {
Self::Cow(Cow::Borrowed(data))
}
}

impl From<String> for EitherString<'_> {
impl From<String> for EitherString<'_, '_> {
fn from(data: String) -> Self {
Self::Cow(Cow::Owned(data))
}
}

impl<'a> From<Bound<'a, PyString>> for EitherString<'a> {
fn from(date: Bound<'a, PyString>) -> Self {
impl<'py> From<Bound<'py, PyString>> for EitherString<'_, 'py> {
fn from(date: Bound<'py, PyString>) -> Self {
Self::Py(date)
}
}
Expand Down
Loading