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;
// If you need the underlying Value for custom deserialization
let f: = field.get_generic_inner;
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 a config's field isn't a basic type like String or u8.
In these cases, instead of using
field.get_u8()etc., you can usefield.get_generic_inner()to access the field value directly.If the key requested is present, Quickfig will get you a reference to its field (as
&Value) which you can then deserialize as needed.Ex: You expect a config to have "colors" & "fonts" keys, and you open a
config.jsonwith this content:
In your application:
// Fields you expect to be in the config
// Types for your expected config structure
;
// Opening the config.json file
let config = open.unwrap;
// Access "colors" key & verify only 1 match
let colors_field = config.get.unwrap;
colors_field.only_one_key.unwrap;
// Get the underlying value without trying to parse it
let colors_inner: &Value = colors_field
.get_generic_inner
.unwrap;
// Deserialize it yourself
let colors: Colors = deserialize.unwrap;
-
Sometimes you want to allow multiple possible paths for a user's config.
For example, your docs might say:
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;