20 releases (5 breaking)

Uses old Rust 2015

0.6.0 Oct 23, 2018
0.4.4 Oct 20, 2018
0.1.8 Jul 11, 2018

#1392 in Data structures


Used in 3 crates

BSD-2-Clause

71KB
1.5K SLoC

Multi-configuration parser library

Nereon configuration tools in native Rust. See nereon-models, nereon-syntax and libnereon for background information.

There is a NOC playground application bundled in noc. It generates a simple 2-pane web page with an editable NOC on the left and parsed results on the right. To get this running use something along the lines of:

git clone git@github.com:riboseinc/rust-nereon.git
cd rust-nereon
cargo build --manifest-path=noc/Cargo.toml
./target/debug/noc -p 8042

and point your browser at http://localhost:8042

Documentation

Website


lib.rs:

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));

Dependencies

~5MB
~93K SLoC