Skip to content

Commit

Permalink
Update toml to 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasKalbertodt committed Mar 3, 2024
1 parent 15c729d commit a83bdb2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ confique-macro = { version = "=0.0.9", path = "macro" }
json5 = { version = "0.4.1", optional = true }
serde = { version = "1", features = ["derive"] }
serde_yaml = { version = "0.9", optional = true }
toml = { version = "0.5", optional = true }
toml = { version = "0.8", optional = true }

[dev-dependencies]
pretty_assertions = "1.2.1"
6 changes: 4 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ impl File {

match self.format {
#[cfg(feature = "toml")]
FileFormat::Toml => toml::from_slice(&file_content)
.map_err(|e| error(Box::new(e))),
FileFormat::Toml => {
let s = std::str::from_utf8(&file_content).map_err(|e| error(Box::new(e)))?;
toml::from_str(s).map_err(|e| error(Box::new(e)))
}

#[cfg(feature = "yaml")]
FileFormat::Yaml => serde_yaml::from_slice(&file_content)
Expand Down
10 changes: 10 additions & 0 deletions src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ impl From<MapKey> for Expr {
}
}

impl Float {
#[cfg(feature = "toml")]
pub(crate) fn is_nan(&self) -> bool {
match self {
Float::F32(f) => f.is_nan(),
Float::F64(f) => f.is_nan(),
}
}
}

fn serialize_map<S>(map: &&'static [MapEntry], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand Down
15 changes: 12 additions & 3 deletions src/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,20 @@ impl fmt::Display for PrintExpr<'_> {
Ok(())
},

// We special case floats as the TOML serializer below doesn't work
// well with floats, not rounding them appropriately. See:
// https://github.com/toml-rs/toml/issues/494
//
// For all non-NAN floats, the `Display` output is compatible with
// TOML.
Expr::Float(fv) if !fv.is_nan() => fv.fmt(f),

// All these other types can simply be serialized as is.
Expr::Str(_) | Expr::Float(_) | Expr::Integer(_) | Expr::Bool(_) | Expr::Array(_) => {
toml::to_string(&self.0)
.expect("string serialization to TOML failed")
.fmt(f)
let mut s = String::new();
serde::Serialize::serialize(&self.0, toml::ser::ValueSerializer::new(&mut s))
.expect("string serialization to TOML failed");
s.fmt(f)
}
}
}
Expand Down

0 comments on commit a83bdb2

Please sign in to comment.