Quickfig
Overview
Quickfig defines a simple API for reading config files in applications.
Quickfig's goal is to replace a big chunk of boilerplate that you most likely have/will have to write many times as an application developer.
This crate is mostly a wrapper around Serde and crates that implement Serde's de/serialization model, currently including:
Modules
quickfig::core- Core exports for reading configuration filesquickfig::derive- Derive macro for config fields
Features
derive- Enables the derive macro for ConfigFields
Quickstart
$ cargo add quickfig --features derive
Imagine you want to read a user's config file at /path/to/config.json with:
In your project:
use ConfigFields;
use ;
// Define the fields you may want to read
Cookbook
A few more usage examples to show features/recommended usage:
Config::openrequires a FULL path. A crate like dirs can be helpful to create these
use *;
use PathBuf;
// Might be something like this on linux:
// "/home/username/.config/my_app/config.json"
let path_to_config: PathBuf = ;
- List of get methods available on Vec:
- NOTE: Any numbers outside of
i64range will error on TOML files as TOML spec does not support them
let config = open.unwrap;
let field = config.get.unwrap;
let f: = field.get_string;
let f: = field.get_char;
let f: = field.get_bool;
let f: = field.get_u8;
let f: = field.get_string;
let f: = field.get_char;
let f: = field.get_bool;
let f: = field.get_u8;
let f: = field.get_u16;
let f: = field.get_u32;
let f: = field.get_u64;
let f: = field.get_u128;
let f: = field.get_i8;
let f: = field.get_i16;
let f: = field.get_i32;
let f: = field.get_i64;
let f: = field.get_i128;
let f: = field.get_f32;
let f: = field.get_f64;
-
Sometimes you may not know the exact path to a user's config file, but you instead inform your user that it must in a list of possible locations.
For example, your docs may state:
MyApp will first check for your config at "~/.config/MyApp/config.json", then "~/.MyApp/config.json", then "~/.local/share/MyApp/config.json"...For that situation there is a helper method when creating a Config:
// List of paths you want to check (order does matter!)
let paths = vec!;
// Search function that determines whether a path should be used or not.
// Return Some(path) to use a path or None to continue iterating.
// Will short-circuit first Some(path) return.
let search = Boxnew;
// Will try to create a Config from the first path that your function returns
// Some(path) on. Errors if there is no match or problem creating Config.
// If no search function is provided then default is same as search above.
let config: = open_first_match;