| Crates.io | tomlreadwr |
| lib.rs | tomlreadwr |
| version | 0.1.1 |
| created_at | 2025-10-06 20:00:43.417568+00 |
| updated_at | 2025-10-07 06:15:18.514804+00 |
| description | A simple TOML configuration manager with nested key access and type-safe deserialization |
| homepage | https://github.com/nitesh545/tomlreadwr/blob/master/ |
| repository | https://github.com/nitesh545/tomlreadwr/blob/master/ |
| max_upload_size | |
| id | 1870777 |
| size | 35,956 |
A simple and intuitive TOML configuration manager for Rust with support for nested key access, modification, and type-safe deserialization.
"server.database.host")Add this to your Cargo.toml:
[dependencies]
toml_config = "0.1.0"
serde = { version = "1.0", features = ["derive"] }
Given a config.toml file:
[database]
host = "localhost"
port = 5432
username = "admin"
[server]
address = "0.0.0.0"
port = 8080
Read and modify it:
use toml_config::TomlConfig;
use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct DatabaseConfig {
host: String,
port: u16,
username: String,
}
fn main() -> anyhow::Result<()> {
// Load configuration
let mut config = TomlConfig::load("config.toml")?;
// Read values
let host = config.get_str("database.host");
println!("Host: {:?}", host);
// Deserialize into struct
let db: DatabaseConfig = config
.get_of_type("database")
.expect("Failed to deserialize database config");
println!("Database config: {:?}", db);
// Modify and save
config.set("database.port", 5432)?
.set("server.port", 9090)?
.save()?;
Ok(())
}
use toml_config::TomlConfig;
let config = TomlConfig::load("config.toml")?;
// Get raw TOML value
if let Some(value) = config.get("server.port") {
println!("Port value: {:?}", value);
}
// Get as string
if let Some(host) = config.get_str("server.address") {
println!("Server address: {}", host);
}
// Deserialize to custom type
use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct ServerConfig {
address: String,
port: u16,
}
if let Some(server) = config.get_of_type::<ServerConfig>("server") {
println!("Server: {}:{}", server.address, server.port);
}
use toml_config::TomlConfig;
let mut config = TomlConfig::load("config.toml")?;
// Set existing value (parent path must exist)
config.set("server.port", 9090)?;
// Create new nested keys (creates intermediate tables automatically)
config.create("logging.level", "debug")?
.create("logging.file", "/var/log/app.log")?
.create("logging.max_size", 1048576)?;
// Delete keys
config.delete("server.debug_mode")?;
// Save all changes back to file
config.save()?;
All modifying methods return &mut Self for easy chaining:
config
.set("server.port", 8080)?
.create("cache.enabled", true)?
.create("cache.ttl", 3600)?
.delete("old.deprecated_config")?
.save()?;
TomlConfig::load(path: impl AsRef<Path>) -> Result<Self>
Load a TOML file from the specified path.
save(&self) -> Result<()>
Save the current configuration back to the original file.
get(&self, key: &str) -> Option<&Value>
Get a value using dot notation (e.g., "server.database.host").
get_str(&self, key: &str) -> Option<&str>
Get a string value directly.
get_of_type<T>(&self, key: &str) -> Option<T>
Deserialize a value into type T (requires T: Deserialize).
set<T: Into<Value>>(&mut self, key: &str, value: T) -> Result<&mut Self>
Set a value at the specified key. Parent path must exist.
create<T: Into<Value>>(&mut self, key: &str, value: T) -> Result<&mut Self>
Create a new key-value pair, automatically creating intermediate tables.
delete(&mut self, key: &str) -> Result<&mut Self>
Delete a key from the configuration.
get_path(&self) -> &PathBuf
Get the path to the configuration file.
get_data(&self) -> &Value
Get a reference to the underlying TOML data.
All modifying operations return Result<&mut Self> for proper error handling:
use toml_config::TomlConfig;
let mut config = TomlConfig::load("config.toml")?;
match config.set("invalid.path.that.does.not.exist", 123) {
Ok(_) => println!("Successfully set value"),
Err(e) => eprintln!("Error: {}", e),
}
// Or use ? operator for early return
config.set("server.port", 8080)?
.save()?;
Common errors:
create instead)set vs createset(key, value) - Requires all parent paths to exist. Fails if path is missing.create(key, value) - Automatically creates missing parent tables.// If "logging" table doesn't exist:
config.set("logging.level", "debug")?; // ❌ Error: Path 'logging' does not exist
config.create("logging.level", "debug")?; // ✅ Creates "logging" table automatically
serde - Serialization frameworktoml - TOML parseranyhow - Error handlingCheck the examples directory for more usage examples:
cargo run --example basic
cargo run --example modify
cargo run --example custom_types
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Licensed under either of:
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.