| Crates.io | fast_config |
| lib.rs | fast_config |
| version | 1.3.0 |
| created_at | 2023-01-19 03:13:06.919407+00 |
| updated_at | 2025-12-10 01:49:38.431657+00 |
| description | A small and simple multi-format crate to handle config files |
| homepage | |
| repository | https://github.com/FlooferLand/fast_config |
| max_upload_size | |
| id | 762230 |
| size | 34,917 |
fast_config---
A small, safe, lightweight, and easy-to-use Rust crate to read and write to config files.
Currently supports: JSON, JSON5, TOML, and YAML.
But more Serde-supported formats (such as RON) are planned to be added later.
fast_config was made to be a faster to set up, more light-weight, statically typed alternative to config.
It also manages to have its own benefits compared to some other config-reading crates as there is full support for writing/saving config files, and it also provides you with some options regarding styling your config files
key0 to key9000 in an object)2 and 3 are going to be addressed with future updates, however.
This crate is now stable, I however haven't battle-tested this in any humongous projects, so while there will NOT be any panics or crashes, some weird things might happen at scale.
Documentation might be a little weird or incomplete at the current moment, too.
Feel free to contribute any fixes by opening up an issue if you find anything that isn't working as expected!
use fast_config::FastConfig;
use fast_config::Format;
use serde::Serialize;
use serde::Deserialize;
// Create a config struct and derive FastConfig
#[derive(Serialize, Deserialize, FastConfig)]
pub struct MyData {
pub student_debt: i32,
}
// Create the data with default values
let mut data = MyData {
student_debt: 20
};
// Save to create the file
data.save("test/myconfig.json5", Format::JSON5).unwrap();
// Load from the file
data.load("test/myconfig.json5", Format::JSON5).unwrap();
// Read/write to the data
println!("I am {}$ in debt", data.student_debt);
data.student_debt = i32::MAX;
println!("Oh no, i am now {}$ in debt!!", data.student_debt);
// Save it back to disk
data.save("test/myconfig.json5", Format::JSON5).unwrap();
let data = MyData::new("example_config.json", Format::JSON).unwrap();
// Convert config to string
let json_string = data.to_string(Format::JSON).unwrap();
let pretty_json = data.to_string_pretty(Format::JSON).unwrap();
// Create config from string
let loaded = MyData::from_string(&json_string, Format::JSON).unwrap();
// Saves in a format thats indented and human-readable
data.save_pretty("config.json", Format::JSON).unwrap();
Add the crate to your project:
cargo add fast_config
serde with derive features:cargo add serde --features derive
Enable the feature(s) for the format(s) you'd like to use in your Cargo.toml:
[dependencies]
fast_config = { version = "...", features = ["json", "json5", "toml", "yaml", "derive"] }
json, json5, toml, yamlderive feature to use the #[derive(FastConfig)] macroCreate a struct to hold your data and derive the necessary traits:
use serde::Serialize;
use serde::Deserialize;
use fast_config::FastConfig;
#[derive(Serialize, Deserialize, FastConfig)]
pub struct MyConfig {
pub setting: String,
}
Use the trait methods directly on your struct:
let mut config = MyConfig { setting: "default".into() };
let config_path = "example_getting_started.json";
config.save(config_path, Format::JSON).unwrap();
config.setting = "something else";
config.load(config_path, Format::JSON).unwrap();
FastConfig TraitThe FastConfig trait provides methods for loading, saving, and serializing config data. When you derive FastConfig on your struct, these methods become available:
load(path, format) - Loads config data from a file, replacing the current struct's valuessave(path, format) - Saves config data to a file (compact format)save_pretty(path, format) - Saves config data to a file with pretty formatting (indented, readable)from_string(content, format) - Creates a new config instance from a stringto_string(format) - Converts config to a compact string representationto_string_pretty(format) - Converts config to a pretty-formatted stringnew(path, format) - Creates a new config instance by loading from a file path#[derive(FastConfig)] MacroThe derive macro automatically implements the FastConfig trait for your struct. It requires that your struct also derives Serialize and Deserialize from the serde crate.
If you're re-exporting fast_config under a different name, you can specify the crate path:
use serde::Serialize;
use serde::Deserialize;
use fast_config::FastConfig;
#[derive(Serialize, Deserialize, FastConfig)]
#[fast_config(crate = "my_crate::fast_config")]
pub struct MyConfig {
pub value: i32,
}
View the tests directory for more advanced examples.
The crate now uses a trait-based approach with #[derive(FastConfig)]. This makes the API cleaner and more ergonomic - you can now call save() and load() directly on your config struct instead of wrapping it in a Config type.
If you're migrating from an older version, see the conversion tutorial for guidance.