Is there a crate to read *.toml files?

Is there a crate to read and write to *.toml file(s) (which is useful for configurations for the Rust programs)?

There's toml, at least.

5 Likes
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Config { ... }

fn read_config() -> std::io::Result<Config> {
    let content = std::fs::read_to_string("somewhere/file.toml")?;
    Ok(toml::from_str(&content)?)
}

Currently the toml crate lacks a from_reader function which would allow to do this more cleanly.

3 Likes

Is there another crate that you might know which does a good job by any chance?

What's wrong with toml? It's fit my needs in the past just fine.

5 Likes

toml is owned by @alexcrichton, who is part of the Rust team, a moderator on this forum and it is a dependency of Cargo (master depends on v0.5.7; v0.5.8 is the most recent version), so IMHO, just use it. It's a must-trust crate and anyone who deals with dependency reviews will appreciate having to review one less crate or at least being able to lower the priority.

5 Likes

Yeah, using serde with the toml crate is really going to be the easiest way to directly use TOML for config files.