Crate nereon[][src]

nereon is an option parser which creates a JSON representation of data parsed from a combination of command line options and environment variables.

Cammand line options are described using struct Opt

nereon_init is used to parse the command line options and returns a serde_json::value::Value tree.

nereon_json does the same as nereon_init except it returns a JSON string.

Variable interpolation is performed so an option passed on the command line can, for example, be expanded to the contents of a file, which itself will be parsed and inserted into the JSON structure. Files included in this way can be JSON or UCL.

Options are processed in order and variable interpolation is performed as options are processed. Later options can therefore use values created by earlier option processing (as can be seen in the following example).

For more details on variable interpolation see expand_vars.

Note: nereon depends on libucl. Libucl is a C that must be installed and accessible to the dynamic linker (via LD_LIBRARY_PATH, DYLD_LIBRARY_PATH or similar).

Examples

extern crate nereon;
use nereon::{Opt, nereon_init, ucl_to_value};

// create an example config file with UCL syntax
std::fs::write("/tmp/nereon_test", r#"
    user "admin" {
        permissions = "${env:nereon_permissions}"
    }
"#);

// UCL can be loaded and converted into JSON using ucl_to_value
assert_eq!(
    ucl_to_value(&mut std::fs::File::open("/tmp/nereon_test").unwrap()),
    Ok( json!(
        {
            "user" : {
                "admin" : {
                    // note: ${env:nereon_permissions} expands to ""
                    // as the environment variable isn't set
                    "permissions" : ""
                }
            }
        }
    ))
);

// .. or can be expanded during option processing with nereon_init
let options = vec![
    Opt::new(
        "",
        None,
        None,
        Some("nereon_config"),
        0,
        None,
        Some("${file:{}}"),
        Some("Config file"),
    ),
    Opt::new(
        "user.admin.uid",
        Some("u"),
        None,
        None,
        0,
        None,
        None,
        Some("UID of admin user"),
    ),
];

let args = "-u 100".split(" ").map(|a| a.to_owned()).collect::<Vec<_>>();

std::env::set_var("nereon_config", "/tmp/nereon_test");
std::env::set_var("nereon_permissions", "read,write");

assert_eq!(nereon_init(options, args), Ok(json!(
    {
        "user" : {
            "admin" : {
                "uid" : "100",
                "permissions" : "read,write"
            }
        }
    }))
);
std::fs::remove_file("/tmp/nereon_test");

Modules

libucl

Structs

Opt

Enums

OptFlag

Functions

expand_vars

Perform variable interpolation within serde_json Value.

nereon_init

Parse command-line options into a serde_json Value.

nereon_json

Parse command-line options into JSON formatted configuration.

ucl_to_value

Converts UCL formatted data into serde_json Value.