Expand description
Jsony Config is an opinionated application/service configuration framework based on jsony.
Features:
- Fast compile times, a thin layer over jsony.
- Lenient Json Configs Supporting (jsony::JsonParserConfig)
- Trailing Commas
- Comments
- Unquoted static keys
- Loading from multiple config files with priority.
- Resolving paths relative to the location of the config file (relative_path)
- Warnings for duplicate and unused fields, localized to the specific file and line (Diagnostic)
- Various search strategies for finding configs (Search)
- Lazy initialized global config, useable from tests (GlobalConfig)
§Configuration Formats
Two configuration file formats are supported: .json and .js.
§JSON (.json)
The .json format supports lenient features like trailing commas and comments, which can be useful for configuration files. However, editor support for these extensions to the JSON standard can be inconsistent and may require special configuration.
// application.config.json
{
"number": 32,
// A useful comment
"test_output": "./output"
}§JavaScript (.js)
To provide a better out-of-the-box editor experience with syntax highlighting and validation for lenient JSON features, a .js format is also supported.
This format is a workaround that uses a subset of JavaScript syntax.
The configuration must be assigned to a const CONFIG = declaration. jsony_config will locate this line and parse the object that follows.
// application.config.js
const CONFIG = {
number: 32,
// A useful comment
test_output: "./output",
};Note: While this looks like JavaScript, it is not executed as such. JavaScript features like variables, functions, or arithmetic are not supported. This approach is simply a “hack” to leverage editor support for JavaScript object literals, which closely resembles the lenient JSON syntax.
§Example
use jsony_config::{Search, GlobalConfig, relative_path};
#[derive(jsony::Jsony, Debug)]
#[jsony(Flattenable)]
pub struct Config {
#[jsony(default = 42)]
number: u32,
#[jsony(with = relative_path)]
test_output: Option<std::path::PathBuf>,
}
static CONFIG: GlobalConfig<Config> = GlobalConfig::new(&[
Search::Flag("--config"),
Search::Upwards{
file_stem: "application.config",
override_file_stem: Some("application.local.config"),
},
]);
fn main() {
CONFIG.initialize(&mut jsony_config::print_diagnostics).unwrap();
println!("{:#?}", CONFIG);
assert_eq!(CONFIG.number, 42)
}Modules§
- relative_
path - Helper for parsing paths relative to the config file’s location.
Structs§
- Diagnostic
- Diagnostic message (e.g., a warning or error) from the config loading process.
- Global
Config - A thread-safe, lazily initialized global configuration container.
Enums§
- Diagnostic
Level - The severity level of a diagnostic message.
- Error
- This type represents all possible errors thatcan occur when loading configurations.
- Search
- Defines the strategy for locating configuration files.
Traits§
- Jsony
Config - A trait that must be implemented by the root configuration struct.
Functions§
- kvlog_
diagnostics - A
DiagnosticHandlerthat emits structured logs using thekvlogcrate. - load
- Loads a configuration of type
Tfrom the specified search locations. - print_
diagnostics - A
DiagnosticHandlerthat prints formatted messages to standard output.
Type Aliases§
- Diagnostic
Handler - A function type that handles diagnostics emitted during config parsing.