@@ -21,19 +21,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
2121
2222use crate :: { static_str:: StaticStr , type_str, type_str:: TypeStr } ;
2323
24- type_str ! (
25- pub DefaultHelp = "see the report for more information"
26- => "Represents the default help message."
27- ) ;
28-
29- /// Literal space string.
30- pub const SPACE : StaticStr = " " ;
31-
32- /// Literal `(` string.
33- pub const OPEN : StaticStr = "(" ;
34-
35- /// Literal `)` string.
36- pub const CLOSE : StaticStr = ")" ;
24+ type_str ! ( pub NoContext = "no context" => "Represents the abscence of context." ) ;
3725
3826/// Literal `expected` string.
3927pub const EXPECTED : StaticStr = "expected" ;
@@ -131,88 +119,88 @@ impl<T: ?Sized, P: Predicate<T> + ?Sized> fmt::Display for ExpectedCode<T, P> {
131119///
132120/// Values of this type are guaranteed to contain values of type `T`
133121/// that satisfy the predicate `P`.
134- pub struct Refinement < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized = DefaultHelp > {
122+ pub struct Refinement < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized = NoContext > {
135123 value : T ,
136124 predicate : PhantomData < P > ,
137- help : PhantomData < H > ,
125+ context : PhantomData < C > ,
138126}
139127
140- impl < T : fmt:: Debug , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > fmt:: Debug
141- for Refinement < T , P , H >
128+ impl < T : fmt:: Debug , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > fmt:: Debug
129+ for Refinement < T , P , C >
142130{
143131 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
144132 self . get ( ) . fmt ( formatter)
145133 }
146134}
147135
148- impl < T : fmt:: Display , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > fmt:: Display
149- for Refinement < T , P , H >
136+ impl < T : fmt:: Display , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > fmt:: Display
137+ for Refinement < T , P , C >
150138{
151139 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
152140 self . get ( ) . fmt ( formatter)
153141 }
154142}
155143
156- impl < T : Clone , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Clone for Refinement < T , P , H > {
144+ impl < T : Clone , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Clone for Refinement < T , P , C > {
157145 fn clone ( & self ) -> Self {
158146 // SAFETY: by construction, the value satisfies the predicate
159147 unsafe { Self :: unchecked ( self . get ( ) . clone ( ) ) }
160148 }
161149}
162150
163- impl < T : Copy , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Copy for Refinement < T , P , H > { }
151+ impl < T : Copy , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Copy for Refinement < T , P , C > { }
164152
165- impl < T : PartialEq , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > PartialEq
166- for Refinement < T , P , H >
153+ impl < T : PartialEq , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > PartialEq
154+ for Refinement < T , P , C >
167155{
168156 fn eq ( & self , other : & Self ) -> bool {
169157 self . get ( ) . eq ( other. get ( ) )
170158 }
171159}
172160
173- impl < T : Eq , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Eq for Refinement < T , P , H > { }
161+ impl < T : Eq , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Eq for Refinement < T , P , C > { }
174162
175- impl < T : PartialOrd , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > PartialOrd
176- for Refinement < T , P , H >
163+ impl < T : PartialOrd , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > PartialOrd
164+ for Refinement < T , P , C >
177165{
178166 fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
179167 self . get ( ) . partial_cmp ( other. get ( ) )
180168 }
181169}
182170
183- impl < T : Ord , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Ord for Refinement < T , P , H > {
171+ impl < T : Ord , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Ord for Refinement < T , P , C > {
184172 fn cmp ( & self , other : & Self ) -> Ordering {
185173 self . get ( ) . cmp ( other. get ( ) )
186174 }
187175}
188176
189- impl < T : Hash , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Hash for Refinement < T , P , H > {
190- fn hash < G : Hasher > ( & self , hasher : & mut G ) {
177+ impl < T : Hash , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Hash for Refinement < T , P , C > {
178+ fn hash < H : Hasher > ( & self , hasher : & mut H ) {
191179 self . get ( ) . hash ( hasher) ;
192180 }
193181}
194182
195- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > AsRef < T > for Refinement < T , P , H > {
183+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > AsRef < T > for Refinement < T , P , C > {
196184 fn as_ref ( & self ) -> & T {
197185 self . get ( )
198186 }
199187}
200188
201- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Deref for Refinement < T , P , H > {
189+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Deref for Refinement < T , P , C > {
202190 type Target = T ;
203191
204192 fn deref ( & self ) -> & Self :: Target {
205193 self . get ( )
206194 }
207195}
208196
209- impl < T : Default , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Refinement < T , P , H > {
197+ impl < T : Default , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Refinement < T , P , C > {
210198 /// Refines the default value of type `T`.
211199 ///
212200 /// # Errors
213201 ///
214202 /// Returns [`struct@Error`] if the default value does not satisfy the predicate.
215- pub fn try_default ( ) -> Result < Self , Error < T , P , H > > {
203+ pub fn try_default ( ) -> Result < Self , Error < T , P , C > > {
216204 Self :: refine ( T :: default ( ) )
217205 }
218206
@@ -228,17 +216,17 @@ impl<T: Default, P: Predicate<T> + ?Sized, H: TypeStr + ?Sized> Refinement<T, P,
228216}
229217
230218#[ cfg( feature = "serde" ) ]
231- impl < T : Serialize , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Serialize
232- for Refinement < T , P , H >
219+ impl < T : Serialize , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Serialize
220+ for Refinement < T , P , C >
233221{
234222 fn serialize < S : Serializer > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error > {
235223 self . get ( ) . serialize ( serializer)
236224 }
237225}
238226
239227#[ cfg( feature = "serde" ) ]
240- impl < ' de , T : Deserialize < ' de > , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Deserialize < ' de >
241- for Refinement < T , P , H >
228+ impl < ' de , T : Deserialize < ' de > , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Deserialize < ' de >
229+ for Refinement < T , P , C >
242230{
243231 fn deserialize < D : Deserializer < ' de > > ( deserializer : D ) -> Result < Self , D :: Error > {
244232 let value = T :: deserialize ( deserializer) ?;
@@ -247,7 +235,7 @@ impl<'de, T: Deserialize<'de>, P: Predicate<T> + ?Sized, H: TypeStr + ?Sized> De
247235 }
248236}
249237
250- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Refinement < T , P , H > {
238+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Refinement < T , P , C > {
251239 /// Constructs [`Self`] without checking the value.
252240 ///
253241 /// # Safety
@@ -259,7 +247,7 @@ impl<T, P: Predicate<T> + ?Sized, H: TypeStr + ?Sized> Refinement<T, P, H> {
259247 Self {
260248 value,
261249 predicate : PhantomData ,
262- help : PhantomData ,
250+ context : PhantomData ,
263251 }
264252 }
265253}
@@ -268,35 +256,36 @@ impl<T, P: Predicate<T> + ?Sized, H: TypeStr + ?Sized> Refinement<T, P, H> {
268256///
269257/// This error is constructed from the value that failed to satisfy the predicate
270258/// and the error produced by the predicate.
271- pub struct Error < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized = DefaultHelp > {
259+ pub struct Error < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized = NoContext > {
272260 /// The value that failed to satisfy the predicate.
273261 pub value : T ,
274262
275263 /// The error produced by the predicate.
276264 pub error : P :: Error ,
277265
278- /// The help message of the refinement.
279- pub help : PhantomData < H > ,
266+ /// The context of the refinement.
267+ pub context : PhantomData < C > ,
280268}
281269
282- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > fmt:: Display for Error < T , P , H > {
270+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > fmt:: Display for Error < T , P , C > {
283271 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
284272 write ! (
285273 formatter,
286- "{EXPECTED} {expected} (code `{code}`)" ,
274+ "{EXPECTED} {expected} (code `{code}`) [{context}] " ,
287275 expected = P :: expected( ) ,
288- code = P :: expected_code( )
276+ code = P :: expected_code( ) ,
277+ context = Self :: context( ) ,
289278 )
290279 }
291280}
292281
293- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > fmt:: Debug for Error < T , P , H > {
282+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > fmt:: Debug for Error < T , P , C > {
294283 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
295284 fmt:: Display :: fmt ( self , formatter)
296285 }
297286}
298287
299- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > ErrorCore for Error < T , P , H >
288+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > ErrorCore for Error < T , P , C >
300289where
301290 P :: Error : ErrorCore + ' static ,
302291{
@@ -306,7 +295,7 @@ where
306295}
307296
308297#[ cfg( feature = "diagnostics" ) ]
309- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Diagnostic for Error < T , P , H >
298+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Diagnostic for Error < T , P , C >
310299where
311300 P :: Error : Diagnostic + ' static ,
312301{
@@ -315,21 +304,21 @@ where
315304 }
316305
317306 fn help ( & self ) -> Option < Box < dyn fmt:: Display + ' _ > > {
318- Some ( Box :: new ( Self :: help ( ) ) )
307+ Some ( Box :: new ( Self :: context ( ) ) )
319308 }
320309
321310 fn diagnostic_source ( & self ) -> Option < & dyn Diagnostic > {
322311 Some ( self . error ( ) )
323312 }
324313}
325314
326- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Error < T , P , H > {
315+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Error < T , P , C > {
327316 /// Constructs [`Self`].
328317 pub const fn new ( value : T , error : P :: Error ) -> Self {
329318 Self {
330319 value,
331320 error,
332- help : PhantomData ,
321+ context : PhantomData ,
333322 }
334323 }
335324
@@ -343,38 +332,38 @@ impl<T, P: Predicate<T> + ?Sized, H: TypeStr + ?Sized> Error<T, P, H> {
343332 & self . error
344333 }
345334
346- /// Returns the help message of the refinement.
347- pub const fn help ( ) -> StaticStr {
348- H :: VALUE
335+ /// Returns the context of the refinement.
336+ pub const fn context ( ) -> StaticStr {
337+ C :: VALUE
349338 }
350339}
351340
352- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Error < T , P , H > {
341+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Error < T , P , C > {
353342 /// Returns the contained value and the received error.
354343 pub fn into_parts ( self ) -> ( T , P :: Error ) {
355344 ( self . value , self . error )
356345 }
357346}
358347
359- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > From < ( T , P :: Error ) > for Error < T , P , H > {
348+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > From < ( T , P :: Error ) > for Error < T , P , C > {
360349 fn from ( ( value, error) : ( T , P :: Error ) ) -> Self {
361350 Self :: new ( value, error)
362351 }
363352}
364353
365- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > From < Error < T , P , H > > for ( T , P :: Error ) {
366- fn from ( error : Error < T , P , H > ) -> Self {
354+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > From < Error < T , P , C > > for ( T , P :: Error ) {
355+ fn from ( error : Error < T , P , C > ) -> Self {
367356 error. into_parts ( )
368357 }
369358}
370359
371- impl < T , P : Predicate < T > + ?Sized , H : TypeStr + ?Sized > Refinement < T , P , H > {
360+ impl < T , P : Predicate < T > + ?Sized , C : TypeStr + ?Sized > Refinement < T , P , C > {
372361 /// Refines the given value.
373362 ///
374363 /// # Errors
375364 ///
376365 /// Returns [`struct@Error`] if the value does not satisfy the predicate.
377- pub fn refine ( value : T ) -> Result < Self , Error < T , P , H > > {
366+ pub fn refine ( value : T ) -> Result < Self , Error < T , P , C > > {
378367 match Self :: check ( & value) {
379368 // SAFETY: the value satisfies the predicate if the check is successful
380369 Ok ( ( ) ) => Ok ( unsafe { Self :: unchecked ( value) } ) ,
@@ -409,7 +398,7 @@ impl<T, P: Predicate<T> + ?Sized, H: TypeStr + ?Sized> Refinement<T, P, H> {
409398 /// # Errors
410399 ///
411400 /// Returns [`struct@Error`] if the resulting value does not satisfy the predicate.
412- pub fn map < F : FnOnce ( T ) -> T > ( self , function : F ) -> Result < Self , Error < T , P , H > > {
401+ pub fn map < F : FnOnce ( T ) -> T > ( self , function : F ) -> Result < Self , Error < T , P , C > > {
413402 Self :: refine ( function ( self . take ( ) ) )
414403 }
415404
@@ -428,7 +417,7 @@ impl<T, P: Predicate<T> + ?Sized, H: TypeStr + ?Sized> Refinement<T, P, H> {
428417 /// # Errors
429418 ///
430419 /// Returns [`struct@Error`] if the new value does not satisfy the predicate.
431- pub fn replace ( self , value : T ) -> Result < Self , Error < T , P , H > > {
420+ pub fn replace ( self , value : T ) -> Result < Self , Error < T , P , C > > {
432421 Self :: refine ( value)
433422 }
434423
0 commit comments