Module closure

Module closure 

Source
Expand description

Custom configuration loader with Fn.

§Example

use plugx_config::{
    entity::ConfigurationEntity,
    loader::{Loader, closure::Closure},
    ext::url::Url,
};

let url_scheme = "xyz";
let loader_name = "my-custom-loader";
let loader_fn = move |url: &Url, maybe_whitelist: Option<&[String]>, skip_soft_errors: bool| {
    // TODO: check `url` and get my own options
    let mut result = Vec::new();
    // TODO: check whitelist
    // load configurations
    // for example I load configuration for plugin named `foo`:
    let entity = ConfigurationEntity::new("foo_sub_item", 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.push(("foo".to_string(), entity));
    Ok(result)
};
let url = "xyz:///my/own/path?my_option=value".parse().unwrap();
let loader = Closure::new(loader_name, Box::new(loader_fn), url_scheme);
let loaded = loader.load(&url, None, false).unwrap();
assert_eq!(loaded.len(), 1);

Structs§

Closure
Builder struct.

Type Aliases§

BoxedLoaderFn
A |&Url, Option<&[String]>, bool| -> Result<Vec<String, ConfigurationEntity>, ConfigurationLoadError> Fn