1
- use std:: fmt;
2
-
3
1
use pyo3:: prelude:: * ;
4
- use pyo3:: types:: PyDict ;
5
2
6
- use crate :: input:: JsonInput ;
3
+ use crate :: input:: { Input , JsonInput } ;
7
4
8
5
use super :: kinds:: ErrorKind ;
9
6
use super :: location:: { LocItem , Location } ;
@@ -29,6 +26,16 @@ impl<'a> From<Vec<ValLineError<'a>>> for ValError<'a> {
29
26
}
30
27
}
31
28
29
+ impl < ' a > ValError < ' a > {
30
+ pub fn new ( kind : ErrorKind , input : & ' a impl Input < ' a > ) -> ValError < ' a > {
31
+ Self :: LineErrors ( vec ! [ ValLineError :: new( kind, input) ] )
32
+ }
33
+
34
+ pub fn new_with_loc ( kind : ErrorKind , input : & ' a impl Input < ' a > , loc : impl Into < LocItem > ) -> ValError < ' a > {
35
+ Self :: LineErrors ( vec ! [ ValLineError :: new_with_loc( kind, input, loc) ] )
36
+ }
37
+ }
38
+
32
39
// ValError used to implement Error, see #78 for removed code
33
40
34
41
// TODO, remove and replace with just .into()
@@ -44,20 +51,35 @@ pub fn pretty_line_errors(py: Python, line_errors: Vec<ValLineError>) -> String
44
51
/// A `ValLineError` is a single error that occurred during validation which is converted to a `PyLineError`
45
52
/// to eventually form a `ValidationError`.
46
53
/// I don't like the name `ValLineError`, but it's the best I could come up with (for now).
47
- #[ derive( Debug , Default ) ]
54
+ #[ derive( Debug ) ]
48
55
pub struct ValLineError < ' a > {
49
56
pub kind : ErrorKind ,
50
57
// location is reversed so that adding an "outer" location item is pushing, it's reversed before showing to the user
51
- pub reverse_location : Location ,
58
+ pub location : Location ,
52
59
pub input_value : InputValue < ' a > ,
53
- pub context : Context ,
54
60
}
55
61
56
62
impl < ' a > ValLineError < ' a > {
63
+ pub fn new ( kind : ErrorKind , input : & ' a impl Input < ' a > ) -> ValLineError < ' a > {
64
+ Self {
65
+ kind,
66
+ input_value : input. as_error_value ( ) ,
67
+ location : Location :: default ( ) ,
68
+ }
69
+ }
70
+
71
+ pub fn new_with_loc ( kind : ErrorKind , input : & ' a impl Input < ' a > , loc : impl Into < LocItem > ) -> ValLineError < ' a > {
72
+ Self {
73
+ kind,
74
+ input_value : input. as_error_value ( ) ,
75
+ location : Location :: new_some ( loc. into ( ) ) ,
76
+ }
77
+ }
78
+
57
79
/// location is stored reversed so it's quicker to add "outer" items as that's what we always do
58
80
/// hence `push` here instead of `insert`
59
81
pub fn with_outer_location ( mut self , loc_item : LocItem ) -> Self {
60
- self . reverse_location . push ( loc_item) ;
82
+ self . location . with_outer ( loc_item) ;
61
83
self
62
84
}
63
85
@@ -70,28 +92,20 @@ impl<'a> ValLineError<'a> {
70
92
pub fn into_new < ' b > ( self , py : Python ) -> ValLineError < ' b > {
71
93
ValLineError {
72
94
kind : self . kind ,
73
- reverse_location : self . reverse_location ,
95
+ location : self . location ,
74
96
input_value : self . input_value . to_object ( py) . into ( ) ,
75
- context : self . context ,
76
97
}
77
98
}
78
99
}
79
100
80
101
#[ derive( Debug ) ]
81
102
pub enum InputValue < ' a > {
82
- None ,
83
103
PyAny ( & ' a PyAny ) ,
84
104
JsonInput ( & ' a JsonInput ) ,
85
105
String ( & ' a str ) ,
86
106
PyObject ( PyObject ) ,
87
107
}
88
108
89
- impl Default for InputValue < ' _ > {
90
- fn default ( ) -> Self {
91
- Self :: None
92
- }
93
- }
94
-
95
109
impl < ' a > From < PyObject > for InputValue < ' a > {
96
110
fn from ( py_object : PyObject ) -> Self {
97
111
Self :: PyObject ( py_object)
@@ -101,112 +115,10 @@ impl<'a> From<PyObject> for InputValue<'a> {
101
115
impl < ' a > ToPyObject for InputValue < ' a > {
102
116
fn to_object ( & self , py : Python ) -> PyObject {
103
117
match self {
104
- Self :: None => py. None ( ) ,
105
118
Self :: PyAny ( input) => input. into_py ( py) ,
106
119
Self :: JsonInput ( input) => input. to_object ( py) ,
107
120
Self :: String ( input) => input. into_py ( py) ,
108
121
Self :: PyObject ( py_obj) => py_obj. into_py ( py) ,
109
122
}
110
123
}
111
124
}
112
-
113
- #[ derive( Debug , Clone , Default ) ]
114
- pub struct Context ( Vec < ( String , ContextValue ) > ) ;
115
-
116
- impl Context {
117
- pub fn new < I : IntoIterator < Item = ( String , ContextValue ) > > ( raw : I ) -> Self {
118
- Self ( raw. into_iter ( ) . collect ( ) )
119
- }
120
-
121
- pub fn is_empty ( & self ) -> bool {
122
- self . 0 . is_empty ( )
123
- }
124
-
125
- pub fn render ( & self , template : String ) -> String {
126
- let mut rendered = template;
127
- for ( key, value) in & self . 0 {
128
- rendered = rendered. replace ( & format ! ( "{{{}}}" , key) , & value. to_string ( ) ) ;
129
- }
130
- rendered
131
- }
132
- }
133
-
134
- impl fmt:: Display for Context {
135
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
136
- let loc = self
137
- . 0
138
- . iter ( )
139
- . map ( |( k, v) | format ! ( "{}: {}" , k, v) )
140
- . collect :: < Vec < String > > ( )
141
- . join ( ", " ) ;
142
- write ! ( f, "{{{}}}" , loc)
143
- }
144
- }
145
-
146
- // maybe this is overkill and we should just use fmt::Display an convert to string when creating Context?
147
- #[ derive( Debug , Clone ) ]
148
- pub enum ContextValue {
149
- S ( String ) ,
150
- I ( i64 ) ,
151
- F ( f64 ) ,
152
- }
153
-
154
- impl fmt:: Display for ContextValue {
155
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
156
- match self {
157
- ContextValue :: S ( v) => write ! ( f, "{}" , v) ,
158
- ContextValue :: I ( v) => write ! ( f, "{}" , v) ,
159
- ContextValue :: F ( v) => write ! ( f, "{}" , v) ,
160
- }
161
- }
162
- }
163
-
164
- impl From < String > for ContextValue {
165
- fn from ( str : String ) -> Self {
166
- Self :: S ( str)
167
- }
168
- }
169
-
170
- impl From < & str > for ContextValue {
171
- fn from ( str : & str ) -> Self {
172
- Self :: S ( str. to_string ( ) )
173
- }
174
- }
175
-
176
- impl From < i64 > for ContextValue {
177
- fn from ( int : i64 ) -> Self {
178
- Self :: I ( int)
179
- }
180
- }
181
-
182
- impl From < usize > for ContextValue {
183
- fn from ( u : usize ) -> Self {
184
- Self :: I ( u as i64 )
185
- }
186
- }
187
-
188
- impl From < f64 > for ContextValue {
189
- fn from ( f : f64 ) -> Self {
190
- Self :: F ( f)
191
- }
192
- }
193
-
194
- impl ToPyObject for ContextValue {
195
- fn to_object ( & self , py : Python ) -> PyObject {
196
- match self {
197
- ContextValue :: S ( v) => v. into_py ( py) ,
198
- ContextValue :: I ( v) => v. into_py ( py) ,
199
- ContextValue :: F ( v) => v. into_py ( py) ,
200
- }
201
- }
202
- }
203
-
204
- impl ToPyObject for Context {
205
- fn to_object ( & self , py : Python ) -> PyObject {
206
- let dict = PyDict :: new ( py) ;
207
- for ( key, value) in & self . 0 {
208
- dict. set_item ( key, value) . unwrap ( ) ;
209
- }
210
- dict. into_py ( py)
211
- }
212
- }
0 commit comments