Skip to content

Commit ffe3d05

Browse files
committed
adjust error handling traits
1 parent 8db3a6f commit ffe3d05

File tree

6 files changed

+27
-33
lines changed

6 files changed

+27
-33
lines changed

src/errors/line_error.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ use pyo3::DowncastIntoError;
55

66
use jiter::JsonValue;
77

8+
use crate::input::BorrowInput;
89
use crate::input::Input;
910

1011
use super::location::{LocItem, Location};
1112
use super::types::ErrorType;
1213

1314
pub type ValResult<T> = Result<T, ValError>;
1415

15-
pub trait AsErrorValue {
16-
fn as_error_value(&self) -> InputValue;
16+
pub trait ToErrorValue {
17+
fn to_error_value(&self) -> InputValue;
1718
}
1819

19-
impl<'a, T: Input<'a>> AsErrorValue for T {
20-
fn as_error_value(&self) -> InputValue {
21-
Input::as_error_value(self)
20+
impl<'a, T: BorrowInput<'a>> ToErrorValue for T {
21+
fn to_error_value(&self) -> InputValue {
22+
Input::as_error_value(self.borrow_input())
2223
}
2324
}
2425

@@ -55,11 +56,11 @@ impl From<Vec<ValLineError>> for ValError {
5556
}
5657

5758
impl ValError {
58-
pub fn new(error_type: ErrorType, input: &impl AsErrorValue) -> ValError {
59+
pub fn new(error_type: ErrorType, input: impl ToErrorValue) -> ValError {
5960
Self::LineErrors(vec![ValLineError::new(error_type, input)])
6061
}
6162

62-
pub fn new_with_loc(error_type: ErrorType, input: &impl AsErrorValue, loc: impl Into<LocItem>) -> ValError {
63+
pub fn new_with_loc(error_type: ErrorType, input: impl ToErrorValue, loc: impl Into<LocItem>) -> ValError {
6364
Self::LineErrors(vec![ValLineError::new_with_loc(error_type, input, loc)])
6465
}
6566

@@ -94,26 +95,26 @@ pub struct ValLineError {
9495
}
9596

9697
impl ValLineError {
97-
pub fn new(error_type: ErrorType, input: &impl AsErrorValue) -> ValLineError {
98+
pub fn new(error_type: ErrorType, input: impl ToErrorValue) -> ValLineError {
9899
Self {
99100
error_type,
100-
input_value: input.as_error_value(),
101+
input_value: input.to_error_value(),
101102
location: Location::default(),
102103
}
103104
}
104105

105-
pub fn new_with_loc(error_type: ErrorType, input: &impl AsErrorValue, loc: impl Into<LocItem>) -> ValLineError {
106+
pub fn new_with_loc(error_type: ErrorType, input: impl ToErrorValue, loc: impl Into<LocItem>) -> ValLineError {
106107
Self {
107108
error_type,
108-
input_value: input.as_error_value(),
109+
input_value: input.to_error_value(),
109110
location: Location::new_some(loc.into()),
110111
}
111112
}
112113

113-
pub fn new_with_full_loc(error_type: ErrorType, input: &impl AsErrorValue, location: Location) -> ValLineError {
114+
pub fn new_with_full_loc(error_type: ErrorType, input: impl ToErrorValue, location: Location) -> ValLineError {
114115
Self {
115116
error_type,
116-
input_value: input.as_error_value(),
117+
input_value: input.to_error_value(),
117118
location,
118119
}
119120
}

src/errors/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod types;
66
mod validation_exception;
77
mod value_exception;
88

9-
pub use self::line_error::{AsErrorValue, InputValue, ValError, ValLineError, ValResult};
9+
pub use self::line_error::{InputValue, ToErrorValue, ValError, ValLineError, ValResult};
1010
pub use self::location::LocItem;
1111
pub use self::types::{list_all_errors, ErrorType, ErrorTypeDefaults, Number};
1212
pub use self::validation_exception::ValidationError;

src/errors/value_exception.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use pyo3::types::{PyDict, PyString};
55
use crate::input::InputType;
66
use crate::tools::extract_i64;
77

8-
use super::line_error::AsErrorValue;
8+
use super::line_error::ToErrorValue;
99
use super::{ErrorType, ValError};
1010

1111
#[pyclass(extends=PyException, module="pydantic_core._pydantic_core")]
@@ -106,7 +106,7 @@ impl PydanticCustomError {
106106
}
107107

108108
impl PydanticCustomError {
109-
pub fn into_val_error(self, input: &impl AsErrorValue) -> ValError {
109+
pub fn into_val_error(self, input: impl ToErrorValue) -> ValError {
110110
let error_type = ErrorType::CustomError {
111111
error_type: self.error_type,
112112
message_template: self.message_template,
@@ -181,7 +181,7 @@ impl PydanticKnownError {
181181
}
182182

183183
impl PydanticKnownError {
184-
pub fn into_val_error(self, input: &impl AsErrorValue) -> ValError {
184+
pub fn into_val_error(self, input: impl ToErrorValue) -> ValError {
185185
ValError::new(self.error_type, input)
186186
}
187187
}

src/input/input_abstract.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,10 @@ pub trait BorrowInput<'py> {
231231
type Input: Input<'py>;
232232
fn borrow_input(&self) -> &Self::Input;
233233
}
234+
235+
impl<'py, T: Input<'py>> BorrowInput<'py> for &'_ T {
236+
type Input = T;
237+
fn borrow_input(&self) -> &Self::Input {
238+
self
239+
}
240+
}

src/input/input_json.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,6 @@ impl<'py> Input<'py> for JsonValue {
308308
}
309309
}
310310

311-
impl BorrowInput<'_> for &'_ JsonValue {
312-
type Input = JsonValue;
313-
fn borrow_input(&self) -> &Self::Input {
314-
self
315-
}
316-
}
317-
318311
/// Required for JSON Object keys so the string can behave like an Input
319312
impl<'py> Input<'py> for String {
320313
fn as_error_value(&self) -> InputValue {
@@ -439,13 +432,6 @@ impl<'py> Input<'py> for String {
439432
}
440433
}
441434

442-
impl BorrowInput<'_> for &'_ String {
443-
type Input = String;
444-
fn borrow_input(&self) -> &Self::Input {
445-
self
446-
}
447-
}
448-
449435
impl BorrowInput<'_> for String {
450436
type Input = String;
451437
fn borrow_input(&self) -> &Self::Input {

src/validators/custom_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pyo3::prelude::*;
33
use pyo3::types::PyDict;
44

55
use crate::build_tools::py_schema_err;
6-
use crate::errors::AsErrorValue;
6+
use crate::errors::ToErrorValue;
77
use crate::errors::{ErrorType, PydanticCustomError, PydanticKnownError, ValError, ValResult};
88
use crate::input::Input;
99
use crate::tools::SchemaDict;
@@ -49,7 +49,7 @@ impl CustomError {
4949
}
5050
}
5151

52-
pub fn as_val_error(&self, input: &impl AsErrorValue) -> ValError {
52+
pub fn as_val_error(&self, input: impl ToErrorValue) -> ValError {
5353
match self {
5454
CustomError::KnownError(ref known_error) => known_error.clone().into_val_error(input),
5555
CustomError::Custom(ref custom_error) => custom_error.clone().into_val_error(input),

0 commit comments

Comments
 (0)