| Crates.io | jsony_config |
| lib.rs | jsony_config |
| version | 0.1.1 |
| created_at | 2025-06-08 20:42:45.749006+00 |
| updated_at | 2025-08-22 18:13:14.958038+00 |
| description | Lenient JSON configuration framework for Rust applications based on jsony. |
| homepage | |
| repository | https://github.com/exrok/jsony |
| max_upload_size | |
| id | 1705253 |
| size | 35,770 |
Jsony Config is an opinionated application/service configuration framework based on jsony
Two configuration file formats are supported: .json and .js.
.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"
}
.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.
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)
}