#config-file #json5 #configuration #yaml-config #settings #file-format

fast_config

A small and simple multi-format crate to handle config files

4 stable releases

1.1.3 Jan 28, 2023
1.0.0 Jan 27, 2023
0.1.1 Jan 19, 2023
0.1.0 Jan 19, 2023

#206 in Configuration

Download history 6/week @ 2024-02-18 8/week @ 2024-02-25 1/week @ 2024-03-03 4/week @ 2024-03-10 121/week @ 2024-03-31

125 downloads per month

MIT license

36KB
383 lines

fast_config


github crates.io docs.rs
license code size issues GitHub Workflow Status

A small, safe, lightweight, and easy-to-use Rust crate to read and write to config files.

Currently only supports: JSON5, TOML, and YAML.

But more Serde-supported formats (such as RON) are planned to be added later.

Useful teleports:

NOTE: I'm taking a 1-3 month break from this project

I've been working on it the WHOLE week, around 6 hours every day/night. And I'm feeling very burnt out at the current moment.

I plan on implementing the next big release once I'm back, though! (I keep TODOs for the entire project at the beginning of lib.rs if anyone wants to check that out)

I'll also be working on a few side-projects using this crate until the next major release to properly battle test it.

The next major release might be a bit far away (weeks or months) unless there's something huge I have to fix.

Pull requests and issues will still be reviewed, fixed, and accepted!
Feel free to contribute anything, I'll still be active. I just don't plan on implementing any BIG new features myself at the current moment.

- Floof


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?

  1. 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 key0 to key9000 in an object)
  2. It cannot currently understand the RON file format
  3. 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:

use fast_config::Config;
use serde::{Serialize, Deserialize};

// Creating a config struct to store our data
#[derive(Serialize, Deserialize)]
pub struct MyData {
    pub student_debt: i32
}

fn main() {
    // Initializing a logging system (needed to show some warnings/errors)
    env_logger::init();

    // Creating our data (default values)
    let data = MyData {
        student_debt: 20
    };

    // Creating a new config struct with our data struct
    let mut config = Config::new("./config/myconfig.json5", data).unwrap();

    // Read/writing to the data
    println!("I am ${} in debt", config.data.student_debt);
    config.data.student_debt = i32::MAX;
    println!("Oh no, i am now ${} in debt!!", config.data.student_debt);

    // Saving it back to the disk
    config.save().unwrap();
}

Getting started

  1. Add the crate to your project via
    cargo add fast_config
    • Additionally, also add serde as it is required!

  1. Enable the feature(s) for the format(s) you'd like to use
    • Currently only json5, toml, and yaml are supported

  1. Create a struct to hold your data that derives Serialize and Deserialize

  1. Create an instance of your data struct
  • Optionally use the crate's Config type for convenience
    use fast_config::Config;

  1. Use
    let my_config = Config::new("./path/to/my_config_file", your_data).unwrap();
    
    to create and store your config file(s)! Alternatively you could also use Config::from_settings to style some things and manually set the format!

View the examples directory for more advanced examples.

Dependencies

~0.5–1.9MB
~41K SLoC