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.
Useful teleports:
- Migrating to a newer version of the crate
- Code examples
- Getting Started
- Things that need work (for contributors!)
What is this crate?
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
Why this crate?
- It's small and fast (uses compile-time features to remove/add code)
- It's safe and robust (uses Rust's structs to store data, instead of HashMaps)
- Ridiculously simple to use (only takes 3 lines of short code to make a config file, write/read something, and save it)
Why not this crate?
- It doesn't work if you don't know the way your data will be formatted
(for example if you want your users to be able to have any keys ranging from
key0tokey9000in an object) - It cannot currently understand the RON file format
- It cannot currently save comments in config files.
2 and 3 are going to be addressed with future updates, however.
⚠ Documentation and tests are still being made! ⚠
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!
Examples:
Basic Usage
use FastConfig;
use Format;
use Serialize;
use Deserialize;
// Create a config struct and derive FastConfig
// Create the data with default values
let mut data = MyData ;
// Save to create the file
data.save.unwrap;
// Load from the file
data.load.unwrap;
// Read/write to the data
println!;
data.student_debt = i32MAX;
println!;
// Save it back to disk
data.save.unwrap;
Creating Config from File
let data = new.unwrap;
String Serialization
// Convert config to string
let json_string = data.to_string.unwrap;
let pretty_json = data.to_string_pretty.unwrap;
// Create config from string
let loaded = from_string.unwrap;
Pretty Formatting
// Saves in a format thats indented and human-readable
data.save_pretty.unwrap;
Getting started
-
Add the crate to your project:
- Also add
serdewith derive features:
- Also add
-
Enable the feature(s) for the format(s) you'd like to use in your
Cargo.toml:[] = { = "...", = ["json", "json5", "toml", "yaml", "derive"] }- Available formats:
json,json5,toml,yaml - Enable the
derivefeature to use the#[derive(FastConfig)]macro
- Available formats:
-
Create a struct to hold your data and derive the necessary traits:
use Serialize; use Deserialize; use FastConfig; -
Use the trait methods directly on your struct:
let mut config = MyConfig ; let config_path = "example_getting_started.json"; config.save.unwrap; config.setting = "something else"; config.load.unwrap;
API Reference
The FastConfig Trait
The FastConfig trait provides methods for loading, saving, and serializing config data. When you derive FastConfig on your struct, these methods become available:
File Operations
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)
String Operations
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 string
Constructor
new(path, format)- Creates a new config instance by loading from a file path
The #[derive(FastConfig)] Macro
The derive macro automatically implements the FastConfig trait for your struct. It requires that your struct also derives Serialize and Deserialize from the serde crate.
Custom Crate Path
If you're re-exporting fast_config under a different name, you can specify the crate path:
use Serialize;
use Deserialize;
use FastConfig;
View the tests directory for more advanced examples.
Migration Note
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.