Module plugx_config::loader::closure
source · Expand description
Custom configuration loader with Fn.
Example
use std::collections::HashMap;
use url::Url;
use plugx_config::entity::ConfigurationEntity;
use plugx_config::loader::{ConfigurationLoader, closure::ConfigurationLoaderFn};
let url_scheme = "xyz";
let loader_name = "my-custom-loader";
let loader_fn = move |url: &Url, maybe_whitelist: Option<&[String]>| {
// TODO: check `url` and get my own options
let mut result = HashMap::new();
// TODO: check whitelist
// load configurations
// for example I load configuration for plugin named `foo`:
let entity = ConfigurationEntity::new(url.clone(), "foo", loader_name)
// If you do not set format here, `Configuration` struct will try to guess it later:
.with_format("yml")
// If you do not set contents here, the default value will be `plugx_input::Input::empty_map()`
.with_contents("hello: world");
result.insert("foo".into(), entity);
Ok(result)
};
let url = "xyz:///my/own/path?my_option=value".parse().unwrap();
let loader = ConfigurationLoaderFn::new(loader_name, Box::new(loader_fn), url_scheme);
let loaded = loader.try_load(&url, None).unwrap();
assert!(loaded.contains_key("foo"));See crate::loader documentation to known how loaders work.
Structs
- Builder struct.
Type Aliases
- A
|&Url, Option<&[String]>| -> Result<HashMap<_, _>, ConfigurationLoadError>Fn