toml_config
A simple and intuitive TOML configuration manager for Rust with support for nested key access, modification, and type-safe deserialization.
Features
- 🔍 Nested key access using dot notation (
"server.database.host") - 🔄 Type-safe deserialization with Serde support
- ✏️ Modify configurations and save back to file
- 🛠️ Create nested structures automatically
- 🚀 Simple API with method chaining
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["derive"] }
Quick Start
Given a config.toml file:
[]
= "localhost"
= 5432
= "admin"
[]
= "0.0.0.0"
= 8080
Read and modify it:
use TomlConfig;
use Deserialize;
Usage Examples
Reading Values
use TomlConfig;
let config = load?;
// Get raw TOML value
if let Some = config.get
// Get as string
if let Some = config.get_str
// Deserialize to custom type
use Deserialize;
if let Some = config.
Modifying Values
use TomlConfig;
let mut config = load?;
// Set existing value (parent path must exist)
config.set?;
// Create new nested keys (creates intermediate tables automatically)
config.create?
.create?
.create?;
// Delete keys
config.delete?;
// Save all changes back to file
config.save?;
Method Chaining
All modifying methods return &mut Self for easy chaining:
config
.set?
.create?
.create?
.delete?
.save?;
API Documentation
Core Methods
Loading and Saving
-
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.
Reading Values
-
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 typeT(requiresT: Deserialize).
Modifying Values
-
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.
Utility Methods
-
get_path(&self) -> &PathBufGet the path to the configuration file. -
get_data(&self) -> &ValueGet a reference to the underlying TOML data.
Error Handling
All modifying operations return Result<&mut Self> for proper error handling:
use TomlConfig;
let mut config = load?;
match config.set
// Or use ? operator for early return
config.set?
.save?;
Common errors:
- "Key cannot be empty" - Empty key string provided
- "Path 'x' does not exist" - Parent path doesn't exist (use
createinstead) - "'x' is not a table" - Trying to access nested keys on a non-table value
- File I/O errors when loading or saving
Differences: set vs create
set(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?; // ❌ Error: Path 'logging' does not exist
config.create?; // ✅ Creates "logging" table automatically
Requirements
- Rust 1.70 or later
- Dependencies:
serde- Serialization frameworktoml- TOML parseranyhow- Error handling
Examples
Check the examples directory for more usage examples:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
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.
Changelog
0.1.0 (2025-10-07)
- Initial release
- Basic TOML configuration management
- Nested key access with dot notation
- Type-safe deserialization
- Modify and save functionality