Crate nereon

source ·
Expand description

Use nereon for application configuration and option parsing.

Configuration written in NOC syntax can be parsed into a Value using the parse_noc function.

extern crate nereon;
use nereon::{parse_noc, Value};
use std::collections::HashMap;

let noc = r#"
    user admin {
        uid 1000
        name "John Doe"
    }"#;

let expected = Value::Table(HashMap::new())
    .insert(vec!["user", "admin", "uid"], Value::from("1000"))
    .insert(vec!["user", "admin", "name"], Value::from("John Doe"));

assert_eq!(parse_noc::<Value>(noc), Ok(expected));

A Nereon Value can be converted back into a NOC string:

extern crate nereon;
use nereon::{parse_noc, Value};

let noc = r#"
    user admin {
        uid 1000 + 10
        name "John Doe"
    }"#;

let expected = r#""user" {"admin" {"name" "John Doe","uid" "1010"}}"#
    .to_owned();

assert_eq!(parse_noc::<Value>(noc).map(|v| v.as_noc_string()), Ok(expected));

By using the nereon-derive crate, a Nereon Value can be converted into another type using the FromValue trait.

#[macro_use]
extern crate nereon_derive;
extern crate nereon;
use nereon::{parse_noc, NocError, ConversionError, FromValue, Value};

#[derive(FromValue, PartialEq, Debug)]
struct User {
    uid: u32,
    name: String,
}

let noc = r#"
    uid 1000 + 10
    name "John Doe"
"#;

let expected = User { uid: 1010, name: "John Doe".to_owned() };
let user = parse_noc::<User>(noc);
assert_eq!(user, Ok(expected));

Nereon can also be used to parse command lines. Command line options are described with a NOS configuration in NOC syntax. Arguments from the command line are inserted into the resulting Value. NOS accepts environment variables as option defaults and can optionally load further defaults from a configuration file.

use nereon::{configure, parse_noc, Value, FromValue};
use std::collections::HashMap;

let nos = r#"
    name "Nereon test"
    authors ["John Doe <john@doe.me>"]
    license Free
    version "0.0.1"
    option config {
        env nereon_config
        usage "Config file"
        key []
    }
    option user {
        short u
        key [user, admin, uid]
        usage "User's UID"
        hint USER
    }
    option permissions {
        env nereon_permissions
        key [user, admin, permissions]
        usage "Permissions for user"
    }"#;

// create an example NOC config file and environment variables
::std::fs::write("/tmp/nereon_test", "user admin permissions read").unwrap();
::std::env::set_var("nereon_config", "/tmp/nereon_test");
::std::env::set_var("nereon_permissions", "read,write");

let expected = parse_noc::<Value>(r#"
    user admin uid 100
    user admin permissions "read,write""#
).unwrap();

assert_eq!(configure::<Value, _, _>(&nos, &["program", "-u", "100"]), Ok(expected));

Structs

Enums

Main Value enum with variants for strings, tables, and lists.

Traits

Trait for conversion from Value into arbitrary types.

Functions

Parse command-line options into a Value.
Parse a NOC string and convert into any type that implements FromValue