Rust

parser

Module

Parsers: from raw contents to a key/value map.

Contents

use utopia_config::Dotenv;
Rust
pub struct Dotenv

KEY=value lines (.env files).

Rust
use utopia_config::{Dotenv, Parser, Value};

let config = Dotenv.parse(&Value::from("HOST=127.0.0.1\nPASSWORD=\"abc#123\" # quoted\nEMPTY=null"), None)?;
assert_eq!(config.get_str("HOST"), Some(&Value::from("127.0.0.1")));
assert_eq!(config.get_str("PASSWORD"), Some(&Value::from("abc#123")));
assert_eq!(config.get_str("EMPTY"), Some(&Value::Null));
use utopia_config::Json;
Rust
pub struct Json

A JSON object.

Rust
use utopia_config::{Json, Parser, Value};

let config = Json.parse(&Value::from(r#"{"port": 8080, "tls": true}"#), None)?;
assert_eq!(config.get_str("port"), Some(&Value::Int(8080)));
use utopia_config::NoParser;
Rust
pub struct NoParser

Contents that are already an array, used as they are.

Rust
use utopia_config::{Array, NoParser, Parser, Value};

assert!(NoParser.parse(&Value::Array(Array::default()), None)?.is_empty());
assert_eq!(NoParser.parse(&Value::from("x"), None).unwrap_err().to_string(), "Contents must be an array");
use utopia_config::Php;
Rust
pub struct Php

Undocumented.

Rust
use utopia_config::{Parser, Php, Value};

let config = Php.parse(&Value::from("<?php return ['workers' => 2 * 4, 'name' => 'api'];"), None)?;
assert_eq!(config.get_str("workers"), Some(&Value::Int(8)));
use utopia_config::Yaml;
Rust
pub struct Yaml

Undocumented.

Rust
use utopia_config::{Parser, Value, Yaml};

let config = Yaml.parse(&Value::from("debug: yes\nworkers: 4\n"), None)?;
assert_eq!(config.get_str("debug"), Some(&Value::Bool(true)));
assert_eq!(config.get_str("workers"), Some(&Value::Int(4)));
use utopia_config::Parser;
Rust
pub trait Parser

Turns a source's contents into a map of keys to values.

The schema lets loosely typed formats convert values to the type of the property that reads them (Dotenv reads booleans this way).