Skip to content

Typesafe, ergonomic config for Rust

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

magnet/config-jam-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

config-jam

WORK-IN-PROGRESS, RELEASE SOON :)

Typesafe, ergonomic config for Rust

use config_jam::{config, OverrideWith};

config! {
#[derive(Debug, Default, Clone, OverrideWith, serde::Serialize, serde::Deserialize)]
#[field_derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub config Foo {
    pub barval: String = "Foo".into(),
    pub test: u32 = 42,
    pub required: u64
}

// We can add several configs
#[derive(Debug, Default)]
#[field_derive(Debug)]
pub config Bar {
    pub single: &'static str = "Bar",
    // And we can have complex initializers
    pub multiple: Vec<u32> = vec![42, 38]
}
}

fn main() {
    let mut foo_base = Foo::default();

    println!("Foo Base:: {:#?}", foo_base.get());

    let foo_toml = Foo {
        barval: Some("Bar".to_string().into()), // TODO improve this
        test: None,
        required: 9000,
    };

    println!("Foo TOML:: {:#?}", foo_toml.get());

    foo_base.override_with(foo_toml);

    println!("Foo Base overriden by Foo TOML:: {:#?}", foo_base.get());

    let foo_env = Foo {
        barval: Some("Bazinga!".to_string().into()), // TODO improve this
        test: Some(98.into()),
        required: 9999,
    };

    println!("Foo ENV {:#?}", foo_env.get());

    foo_base.override_with(foo_env);

    println!(
        "Foo Base overriden by Foo TOML then Foo ENV:: {:#?}",
        foo_base.get()
    );

    // You can now use any field you like
    let some_val = dbg!(foo_base.get().barval);
    // do_something with some_val!

    let bar = Bar::default();
    // Now read-only access with the same structure!
    let bar = bar.get();

    let s = dbg!(bar.single);
    let m = dbg!(bar.multiple);
}

This program prints the following output:

Foo Base:: FooView {
    barval: "Foo",
    test: 42,
    required: 0
}
Foo TOML:: FooView {
    barval: "Bar",
    test: 42,
    required: 9000
}
Foo Base overriden by Foo TOML:: FooView {
    barval: "Bar",
    test: 42,
    required: 9000
}
Foo ENV FooView {
    barval: "Bazinga!",
    test: 98,
    required: 9999
}
Foo Base overriden by Foo TOML then Foo ENV:: FooView {
    barval: "Bazinga!",
    test: 98,
    required: 9999
}
[src/main.rs:57] foo_base.get().barval = "Bazinga!"
[src/main.rs:65] bar.single = "Bar"
[src/main.rs:66] bar.multiple = [
    42,
    38
]

Required Rust version

Config Jam works on Rust stable.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Typesafe, ergonomic config for Rust

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages