Plugx Input (work-in-progress)
A simple and flexible data-structure for configuration and state manipulation of plugins.
Package | Documentation | Repository
Demo
use Input;
let mut map = new_map;
map.map_mut.unwrap.insert;
let inner_list = map
.map_mut // Option<&mut Hashmap<String, Input>>
.unwrap // &mut Hashmap<String, Input>
.get_mut // Option<&mut Input>
.unwrap // &mut Input (which is a list)
.list_mut // Option<&mut Vec<Input>>
.unwrap; // &mut Vec<Input>
*inner_list.get_mut.unwrap = 3.14.into;
*inner_list.get_mut.unwrap = true.into;
*inner_list.get_mut.unwrap = "hello world".into;
assert_eq!;
Features
Diff
use Input;
use ;
let mut map = new_map; // {}
map.map_mut.unwrap.insert; // {"foo": {}}
map // {"foo": {"bar": [50, 60, 70]}}
.map_mut.unwrap
.get_mut.unwrap
.map_mut.unwrap
.insert;
let mut map2 = map.clone; // {"foo": {"bar": [50, 60, 70]}}
*map2 // {"foo": {"bar": [100, 60, 70]}}
.map_mut.unwrap
.get_mut.unwrap
.map_mut.unwrap
.get_mut.unwrap
.list_mut.unwrap
.get_mut.unwrap
.int_mut.unwrap = 100;
diff;
// prints:
// value [foo][bar][0] increased by 50
Merge
use Input;
use merge;
let mut map = new_map; // {}
map.map_mut.unwrap.insert; // {"foo": {}}
map // {"foo": {"bar": false}}
.map_mut.unwrap
.get_mut.unwrap
.map_mut.unwrap
.insert;
let mut map2 = new_map; // {}
map2.map_mut.unwrap.insert; // {"foo": {}}
map2 // {"foo": {"baz": true}}
.map_mut.unwrap
.get_mut.unwrap
.map_mut.unwrap
.insert;
merge;
println!;
// prints:
// {"foo": {"bar": false, "baz": true}}
Validation with human-readable errors
use Input;
use validate;
use ;
let rules_json = r#"
{
"type": "static_map",
"definitions": {
"foo": {"definition": {"type": "boolean"}},
"bar": {"definition": {"type": "string"}, "default": "hello world"},
"baz": {"definition": {"type": "enum", "items": ["x", "y", "z"]}, "default": "y"},
"qux": {
"definition": {
"type": "either",
"definitions": [
{"type": "enum", "items": ["yes", "y", "no", "n"]},
{"type": "boolean"}
]
}
}
}
}
"#;
// Also we could programmatically build `rules`:
let rules: InputDefinitionType = from_str.unwrap;
let mut map = new_map;
let error = validate.err.unwrap;
println!;
// prints:
// qux is not set (expected a value that must be enum with possible values ["yes", "y", "no", "n"] or boolean)
map.map_mut.unwrap.insert;
let error = validate.err.unwrap;
println!;
// prints:
// foo is not set (expected boolean)
map.map_mut.unwrap.insert;
assert!;
// Default values:
assert_eq!;
assert_eq!;
Cargo features
- default: Nothing!
- validation: Enables validation of
Input. - logging: Enables logging via log crate.
- tracing: Enables logging via tracing crate.
To contributors
I ❤️ PR from everyone, and I appreciate your help but before opening a PR, file an issue and describe your feature, fix, etc.