File tree 4 files changed +27
-6
lines changed
4 files changed +27
-6
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ confique-macro = { version = "=0.0.9", path = "macro" }
30
30
json5 = { version = " 0.4.1" , optional = true }
31
31
serde = { version = " 1" , features = [" derive" ] }
32
32
serde_yaml = { version = " 0.9" , optional = true }
33
- toml = { version = " 0.5 " , optional = true }
33
+ toml = { version = " 0.8 " , optional = true }
34
34
35
35
[dev-dependencies ]
36
36
pretty_assertions = " 1.2.1"
Original file line number Diff line number Diff line change @@ -77,8 +77,10 @@ impl File {
77
77
78
78
match self . format {
79
79
#[ cfg( feature = "toml" ) ]
80
- FileFormat :: Toml => toml:: from_slice ( & file_content)
81
- . map_err ( |e| error ( Box :: new ( e) ) ) ,
80
+ FileFormat :: Toml => {
81
+ let s = std:: str:: from_utf8 ( & file_content) . map_err ( |e| error ( Box :: new ( e) ) ) ?;
82
+ toml:: from_str ( s) . map_err ( |e| error ( Box :: new ( e) ) )
83
+ }
82
84
83
85
#[ cfg( feature = "yaml" ) ]
84
86
FileFormat :: Yaml => serde_yaml:: from_slice ( & file_content)
Original file line number Diff line number Diff line change @@ -140,6 +140,16 @@ impl From<MapKey> for Expr {
140
140
}
141
141
}
142
142
143
+ impl Float {
144
+ #[ cfg( feature = "toml" ) ]
145
+ pub ( crate ) fn is_nan ( & self ) -> bool {
146
+ match self {
147
+ Float :: F32 ( f) => f. is_nan ( ) ,
148
+ Float :: F64 ( f) => f. is_nan ( ) ,
149
+ }
150
+ }
151
+ }
152
+
143
153
fn serialize_map < S > ( map : & & ' static [ MapEntry ] , serializer : S ) -> Result < S :: Ok , S :: Error >
144
154
where
145
155
S : serde:: Serializer ,
Original file line number Diff line number Diff line change @@ -189,11 +189,20 @@ impl fmt::Display for PrintExpr<'_> {
189
189
Ok ( ( ) )
190
190
} ,
191
191
192
+ // We special case floats as the TOML serializer below doesn't work
193
+ // well with floats, not rounding them appropriately. See:
194
+ // https://github.com/toml-rs/toml/issues/494
195
+ //
196
+ // For all non-NAN floats, the `Display` output is compatible with
197
+ // TOML.
198
+ Expr :: Float ( fv) if !fv. is_nan ( ) => fv. fmt ( f) ,
199
+
192
200
// All these other types can simply be serialized as is.
193
201
Expr :: Str ( _) | Expr :: Float ( _) | Expr :: Integer ( _) | Expr :: Bool ( _) | Expr :: Array ( _) => {
194
- toml:: to_string ( & self . 0 )
195
- . expect ( "string serialization to TOML failed" )
196
- . fmt ( f)
202
+ let mut s = String :: new ( ) ;
203
+ serde:: Serialize :: serialize ( & self . 0 , toml:: ser:: ValueSerializer :: new ( & mut s) )
204
+ . expect ( "string serialization to TOML failed" ) ;
205
+ s. fmt ( f)
197
206
}
198
207
}
199
208
}
You can’t perform that action at this time.
0 commit comments