Git Product home page Git Product logo

hocon.rs's Introduction

HOCON.rs License: MIT Realease Doc Crate

Warning

This repository is archived as I don't use this crate anymore and don't have the time to update its dependencies. It's stuck on an old version of nom and too painful for me to update for now.

You can contact me if you want to help with maintenance or take over.

The API docs for the master branch are published here.

Parse HOCON configuration files in Rust following the HOCON Specifications.

This implementation goal is to be as permissive as possible, returning a valid document with all errors wrapped in Hocon::BadValue. strict mode can be enabled to return the first Error encountered instead.

Examples

Parsing a string to a struct using serde

use serde::Deserialize;

#[derive(Deserialize)]
struct Configuration {
    host: String,
    port: u8,
    auto_connect: bool,
}

fn main() -> Result<(), Error> {
    let s = r#"{
        host: 127.0.0.1
        port: 80
        auto_connect: false
    }"#;

    let conf: Configuration = hocon::de::from_str(s)?;

    Ok(())
}

Reading from a string and getting value directly

use hocon::HoconLoader;

fn main() -> Result<(), Error> {
    let s = r#"{ a: 7 }"#;

    let doc = HoconLoader::new()
        .load_str(s)?
        .hocon()?;

    let a = doc["a"].as_i64();
    assert_eq!(a, Some(7));

    Ok(())
}

Deserializing to a struct using serde

use serde::Deserialize;

use hocon::HoconLoader;

#[derive(Deserialize)]
struct Configuration {
    host: String,
    port: u8,
    auto_connect: bool,
}

fn main() -> Result<(), Error> {
    let s = r#"{
        host: 127.0.0.1
        port: 80
        auto_connect: false
    }"#;

    let conf: Configuration = HoconLoader::new()
        .load_str(s)?
        .resolve()?;

    Ok(())
}

Reading from a file

use hocon::HoconLoader;

fn main() -> Result<(), Error> {
    let doc = HoconLoader::new()
        .load_file("tests/data/basic.conf")?
        .hocon()?;

    let a = doc["a"].as_i64();
    assert_eq!(a, Some(5));

    Ok(())
}

Reading from several documents

use hocon::HoconLoader;

fn main() -> Result<(), Error> {
    let s = r#"{
        a: will be changed
        unchanged: original value
    }"#;

    let doc = HoconLoader::new()
        .load_str(s)?
        .load_file("tests/data/basic.conf")?
        .hocon()?;

    let a = doc["a"].as_i64();
    assert_eq!(a, Some(5));
    let unchanged = doc["unchanged"].as_string();
    assert_eq!(unchanged, Some(String::from("original value")));

    Ok(())
}

Features

All features are enabled by default. They can be disabled to reduce dependencies.

url-support

This feature enable fetching URLs in includes with include url("http://mydomain.com/myfile.conf") (see spec). If disabled, includes will only load local files specified with include "path/to/file.conf" or include file("path/to/file.conf").

serde-support

This feature enable deserializing to a struct implementing Deserialize using serde

use serde::Deserialize;

use hocon::HoconLoader;

#[derive(Deserialize)]
struct Configuration {
    host: String,
    port: u8,
    auto_connect: bool,
}

# fn main() -> Result<(), Error> {
let s = r#"{host: 127.0.0.1, port: 80, auto_connect: false}"#;

# #[cfg(feature = "serde-support")]
let conf: Configuration = HoconLoader::new().load_str(s)?.resolve()?;
# Ok(())
# }

Spec Coverage

https://github.com/lightbend/config/blob/master/HOCON.md

  • parsing JSON
  • comments
  • omit root braces
  • key-value separator
  • commas are optional if newline is present
  • whitespace
  • duplicate keys and object merging
  • unquoted strings
  • multi-line strings
  • value concatenation
  • object concatenation
  • array concatenation
  • path expressions
  • path as keys
  • substitutions
  • includes
  • conversion of numerically-indexed objects to arrays
  • allow URL for included files
  • duration unit format
  • period unit format
  • size unit format

hocon.rs's People

Contributors

mockersf avatar dependabot-preview[bot] avatar mergify[bot] avatar lostiniceland avatar mredaelli avatar wez avatar bit-ranger avatar dependabot[bot] avatar dependabot-support avatar null-dev avatar zolkko avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.