File tree 3 files changed +10
-3
lines changed
3 files changed +10
-3
lines changed Original file line number Diff line number Diff line change @@ -955,8 +955,8 @@ fn integer_too_big() {
955
955
let native = Foo { a_b : u64:: MAX } ;
956
956
let err = Table :: try_from ( native. clone ( ) ) . unwrap_err ( ) ;
957
957
snapbox:: assert_eq ( "u64 value was too large" , err. to_string ( ) ) ;
958
- let err = toml:: to_string ( & native) . unwrap ( ) ;
959
- snapbox:: assert_eq ( "a_b = -1 \n " , err. to_string ( ) ) ;
958
+ let err = toml:: to_string ( & native) . unwrap_err ( ) ;
959
+ snapbox:: assert_eq ( "out-of-range value for u64 type " , err. to_string ( ) ) ;
960
960
}
961
961
962
962
#[ test]
Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ use crate::visit_mut::VisitMut;
20
20
pub enum Error {
21
21
/// Type could not be serialized to TOML
22
22
UnsupportedType ( Option < & ' static str > ) ,
23
+ /// Value was out of range for the given type
24
+ OutOfRange ( Option < & ' static str > ) ,
23
25
/// `None` could not be serialized to TOML
24
26
UnsupportedNone ,
25
27
/// Key was not convertable to `String` for serializing to TOML
@@ -53,6 +55,8 @@ impl std::fmt::Display for Error {
53
55
match self {
54
56
Self :: UnsupportedType ( Some ( t) ) => write ! ( formatter, "unsupported {t} type" ) ,
55
57
Self :: UnsupportedType ( None ) => write ! ( formatter, "unsupported rust type" ) ,
58
+ Self :: OutOfRange ( Some ( t) ) => write ! ( formatter, "out-of-range value for {t} type" ) ,
59
+ Self :: OutOfRange ( None ) => write ! ( formatter, "out-of-range value" ) ,
56
60
Self :: UnsupportedNone => "unsupported None value" . fmt ( formatter) ,
57
61
Self :: KeyNotString => "map key was not a string" . fmt ( formatter) ,
58
62
Self :: DateInvalid => "a serialized date was invalid" . fmt ( formatter) ,
Original file line number Diff line number Diff line change @@ -98,7 +98,10 @@ impl serde::ser::Serializer for ValueSerializer {
98
98
}
99
99
100
100
fn serialize_u64 ( self , v : u64 ) -> Result < Self :: Ok , Self :: Error > {
101
- self . serialize_i64 ( v as i64 )
101
+ let v: i64 = v
102
+ . try_into ( )
103
+ . map_err ( |_err| Error :: OutOfRange ( Some ( "u64" ) ) ) ?;
104
+ self . serialize_i64 ( v)
102
105
}
103
106
104
107
fn serialize_f32 ( self , v : f32 ) -> Result < Self :: Ok , Self :: Error > {
You can’t perform that action at this time.
0 commit comments