#env-var #config-parser #config-format #json-configuration #source #environment #yaml


A library to manage config from multiple source and formats

4 releases

0.1.3 Mar 9, 2024
0.1.2 Mar 9, 2024
0.1.1 Mar 9, 2024
0.1.0 Mar 9, 2024

#3 in #config-format


411 lines


A rstdev_config is a library used to fetch a configuration data from multiple source and multiple format types

The main purpose of this library to help engineers to maintain their configuration data. To give flexibility to engineers, it will provide a base abstractions so they should be able to create their own source adapter and parsers based on their needs

This library also provide an implementation of Source that will be able to parse given configuration. Current supported format types:

  • YAML
  • TOML
  • JSON
  • Environment variables data types


rstdev-config = {version = "0.1.3"}


use serde::Deserialize;

use rstdev_config::format::{use_toml, use_env};
use rstdev_config::{Builder, ConfigError};
use rstdev_config::parser::{from_file, from_env};

struct Config {


fn main() -> Result<(), ConfigError> {
    let cfg_file_path = "./test.toml";
    let cfg_file: Config = Builder::new(from_file(cfg_file_path)).fetch()?.parse(use_toml)?;
    let cfg_env: Config = Builder::new(from_env("PREFIX_")).fetch()?.parse(use_env)?;

Base Abstractions

pub trait SourceFormatter<'a, TValue>: Clone {
    fn get_source_value(&'a self) -> TValue;

pub trait SourceParser<TFormatter, TValue>
    TValue: Clone,
    TFormatter: for<'a> SourceFormatter<'a, TValue>,
    fn fetch(&self) -> Result<Source<TFormatter, TValue>, ConfigError>;

Example implementation of types::SourceFormatter:

pub struct Value {
    input: String,

impl Value {
    pub fn new(input: String) -> Self {
        Self { input }

impl<'a> SourceFormatter<'a, String> for Value {
    fn get_source_value(&'a self) -> String {

Example implementation of types::SourceParser:

pub struct File {
    filepath: String,

impl File {
    pub fn new(filepath: String) -> Self {
        Self { filepath }

impl SourceParser<StringValue, String> for File {
    fn fetch(&self) -> Result<Source<StringValue, String>, ConfigError> {
        let content = fs::read_to_string(self.filepath.clone())
            .map_err(|err| ConfigError::ParseError(err.to_string()))?;


Example of Builder using parser::from_file :

let cfg: MessageGroup = Builder::new(from_file(toml_file))

The parse method is a part of Source public method:

pub fn parse<F, Out>(&self, cb: F) -> Result<Out, ConfigError>
    F: FnOnce(St) -> Result<Out, ConfigError>,
    Out: DeserializeOwned,

the use_toml, actually is a callback function that implement FnOnce(St) -> Result<Out, ConfigError>:

pub fn use_toml<In, Out>(input: In) -> Result<Out, ConfigError>
    In: ToString,
    Out: DeserializeOwned,
    let from_toml = toml::from_str(&input.to_string())
        .map_err(|err| ConfigError::FormatError(err.to_string()))?;



~118K SLoC