Skip to content

Commit a83bdb2

Browse files
Update toml to 0.8
1 parent 15c729d commit a83bdb2

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ confique-macro = { version = "=0.0.9", path = "macro" }
3030
json5 = { version = "0.4.1", optional = true }
3131
serde = { version = "1", features = ["derive"] }
3232
serde_yaml = { version = "0.9", optional = true }
33-
toml = { version = "0.5", optional = true }
33+
toml = { version = "0.8", optional = true }
3434

3535
[dev-dependencies]
3636
pretty_assertions = "1.2.1"

src/file.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ impl File {
7777

7878
match self.format {
7979
#[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+
}
8284

8385
#[cfg(feature = "yaml")]
8486
FileFormat::Yaml => serde_yaml::from_slice(&file_content)

src/meta.rs

+10
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ impl From<MapKey> for Expr {
140140
}
141141
}
142142

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+
143153
fn serialize_map<S>(map: &&'static [MapEntry], serializer: S) -> Result<S::Ok, S::Error>
144154
where
145155
S: serde::Serializer,

src/toml.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,20 @@ impl fmt::Display for PrintExpr<'_> {
189189
Ok(())
190190
},
191191

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+
192200
// All these other types can simply be serialized as is.
193201
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)
197206
}
198207
}
199208
}

0 commit comments

Comments
 (0)