11//! Component range error
22
3- use core:: { fmt, hash } ;
3+ use core:: fmt;
44
55use crate :: error;
66
77/// An error type indicating that a component provided to a method was out of range, causing a
88/// failure.
99// i64 is the narrowest type fitting all use cases. This eliminates the need for a type parameter.
10- #[ derive( Debug , Clone , Copy , Eq ) ]
10+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
1111pub struct ComponentRange {
1212 /// Name of the component.
1313 pub ( crate ) name : & ' static str ,
14- /// Minimum allowed value, inclusive.
15- pub ( crate ) minimum : i64 ,
16- /// Maximum allowed value, inclusive.
17- pub ( crate ) maximum : i64 ,
18- /// Value that was provided.
19- pub ( crate ) value : i64 ,
20- /// The minimum and/or maximum value is conditional on the value of other
21- /// parameters.
22- pub ( crate ) conditional_message : Option < & ' static str > ,
14+ /// Whether an input with the same value could have succeeded if the values of other components
15+ /// were different.
16+ pub ( crate ) is_conditional : bool ,
2317}
2418
2519impl ComponentRange {
26- /// Obtain the name of the component whose value was out of range .
20+ /// Create a new `ComponentRange` error that is not conditional .
2721 #[ inline]
28- pub const fn name ( self ) -> & ' static str {
29- self . name
22+ pub ( crate ) const fn unconditional ( name : & ' static str ) -> Self {
23+ Self {
24+ name,
25+ is_conditional : false ,
26+ }
3027 }
3128
32- /// Whether the value's permitted range is conditional, i.e. whether an input with this
33- /// value could have succeeded if the values of other components were different.
29+ /// Create a new `ComponentRange` error that is conditional.
3430 #[ inline]
35- pub const fn is_conditional ( self ) -> bool {
36- self . conditional_message . is_some ( )
31+ pub ( crate ) const fn conditional ( name : & ' static str ) -> Self {
32+ Self {
33+ name,
34+ is_conditional : true ,
35+ }
3736 }
38- }
3937
40- impl PartialEq for ComponentRange {
38+ /// Obtain the name of the component whose value was out of range.
4139 #[ inline]
42- fn eq ( & self , other : & Self ) -> bool {
43- self . name == other. name
44- && self . minimum == other. minimum
45- && self . maximum == other. maximum
46- && self . value == other. value
47- // Skip the contents of the message when comparing for equality.
48- && self . conditional_message . is_some ( ) == other. conditional_message . is_some ( )
40+ pub const fn name ( self ) -> & ' static str {
41+ self . name
4942 }
50- }
5143
52- impl hash:: Hash for ComponentRange {
44+ /// Whether the value's permitted range is conditional, i.e. whether an input with this
45+ /// value could have succeeded if the values of other components were different.
5346 #[ inline]
54- fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
55- self . name . hash ( state) ;
56- self . minimum . hash ( state) ;
57- self . maximum . hash ( state) ;
58- self . value . hash ( state) ;
59- // Skip the contents of the message when comparing for equality.
60- self . conditional_message . is_some ( ) . hash ( state) ;
47+ pub const fn is_conditional ( self ) -> bool {
48+ self . is_conditional
6149 }
6250}
6351
6452impl fmt:: Display for ComponentRange {
6553 #[ inline]
6654 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
67- write ! (
68- f,
69- "{} must be in the range {}..={}" ,
70- self . name, self . minimum, self . maximum
71- ) ?;
72-
73- if let Some ( message) = self . conditional_message {
74- write ! ( f, " {message}" ) ?;
75- }
76-
77- Ok ( ( ) )
55+ write ! ( f, "{} was not in range" , self . name)
7856 }
7957}
8058
@@ -102,11 +80,7 @@ impl TryFrom<crate::Error> for ComponentRange {
10280impl serde_core:: de:: Expected for ComponentRange {
10381 #[ inline]
10482 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
105- write ! (
106- f,
107- "a value in the range {}..={}" ,
108- self . minimum, self . maximum
109- )
83+ f. write_str ( "an in-range value" )
11084 }
11185}
11286
@@ -115,7 +89,10 @@ impl ComponentRange {
11589 /// Convert the error to a deserialization error.
11690 #[ inline]
11791 pub ( crate ) fn into_de_error < E : serde_core:: de:: Error > ( self ) -> E {
118- E :: invalid_value ( serde_core:: de:: Unexpected :: Signed ( self . value ) , & self )
92+ serde_core:: de:: Error :: custom ( format_args ! (
93+ "invalid {}, expected an in-range value" ,
94+ self . name
95+ ) )
11996 }
12097}
12198
0 commit comments