Skip to content

Commit

Permalink
v0.2: Macros, better module structure
Browse files Browse the repository at this point in the history
Steffo99 committed May 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 933f28a commit 412c3e9
Showing 17 changed files with 737 additions and 528 deletions.
7 changes: 6 additions & 1 deletion .idea/micronfig.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "micronfig"
version = "0.1.2"
version = "0.2.0"
authors = ["Stefano Pigozzi <me@steffo.eu>"]
edition = "2021"
description = "Tiny crate for simple configuration management"
@@ -9,7 +9,23 @@ license = "MIT OR Apache-2.0"
keywords = ["12-factor-app", "configuration", "config", "environment", "envvar"]
categories = ["config"]

[dependencies]

[dev-dependencies]
tempfile = "3.5.0"
[package.metadata.docs.rs]
all-features = true
cargo-args = ["--bins"]
rustdoc-args = ["--document-private-items"]


[features]
default = ["single_envvars", "single_envfiles", "multi", "handle", "macros"]
single_envvars = []
single_envfiles = []
multi = ["single_envvars", "single_envfiles"]
handle = ["multi"]
macros = ["lazy_static", "handle"]
testing = ["tempfile"]


[dependencies]
lazy_static = { version = "1.4.0", optional = true }
tempfile = { version = "3.5.0", optional = true }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
Tiny crate for simple configuration management.

```rust
let ip_addr: std::net::IpAddr = micronfig::required("IP_ADDRESS");
micronfig::required!(IP_ADDRESS, std::net::IpAddr);
```

## Links
7 changes: 4 additions & 3 deletions examples/e01_the_cave/src/main.rs
Original file line number Diff line number Diff line change
@@ -2,8 +2,9 @@ use std::fmt::{Display, Formatter};
use std::str::FromStr;


fn main() {
let echo: String = micronfig::required("ECHO");
micronfig::required!(ECHO, String);


println!("ECHOing back: {echo}");
fn main() {
println!("{}", *ECHO);
}
21 changes: 11 additions & 10 deletions examples/e02_quick_math/src/main.rs
Original file line number Diff line number Diff line change
@@ -2,19 +2,20 @@ use std::fmt::{Display, Formatter};
use std::str::FromStr;


micronfig::required!(FIRST, u64);
micronfig::required!(SECOND, u64);
micronfig::required!(OPERATOR, Operator);


fn main() {
let first: u64 = micronfig::required("FIRST");
let second: u64 = micronfig::required("SECOND");
let operator: Operator = micronfig::required("OPERATOR");

let result = match operator {
Operator::Sum => first + second,
Operator::Subtraction => first - second,
Operator::Multiplication => first * second,
Operator::Division => first / second,
let result = match *OPERATOR {
Operator::Sum => (*FIRST) + (*SECOND),
Operator::Subtraction => (*FIRST) - (*SECOND),
Operator::Multiplication => (*FIRST) * (*SECOND),
Operator::Division => (*FIRST) / (*SECOND),
};

println!("{first} {operator} {second} = {result}")
println!("{} {} {} = {}", *FIRST, *OPERATOR, *SECOND, result)
}


33 changes: 19 additions & 14 deletions examples/e03_order_a_pizza/src/main.rs
Original file line number Diff line number Diff line change
@@ -2,40 +2,44 @@ use std::fmt::Formatter;
use std::net::IpAddr;
use std::str::FromStr;

fn main() {
// The name of the person who ordered the pizza.
let full_name: String = micronfig::required("FULLNAME");

// The (IP) address the pizza should be delivered to.
let destination: IpAddr = micronfig::required("DESTINATION");
// The name of the person who ordered the pizza.
micronfig::required!(FULLNAME, String);

// The (IP) address the pizza should be delivered to.
micronfig::required!(DESTINATION, IpAddr);

// The base of the pizza to add toppings on.
let pizza_base: PizzaBase = micronfig::required("PIZZABASE");
// The base of the pizza to add toppings on.
micronfig::required!(PIZZABASE, PizzaBase);

// The toppings to add to the pizza.
let pizza_toppings: PizzaToppingsList = micronfig::optional("PIZZATOPPINGS")
.unwrap_or_else(|| PizzaToppingsList{ list: vec![] });
// The toppings to add to the pizza.
micronfig::optional!(PIZZATOPPINGS, PizzaToppingsList);
// A pizza with no toppings, to use as fallback.
const PIZZATOPPINGS_NONE: PizzaToppingsList = PizzaToppingsList{ list: vec![] };


fn main() {
// Let's print the order!
println!("Pizza Order");
println!("===========");
println!();
println!("Base:");
println!("- {}", &pizza_base);
println!("- {}", *PIZZABASE);
println!();
println!("Toppings:");
for topping in pizza_toppings.list {
for topping in &(*PIZZATOPPINGS).as_ref().unwrap_or(&PIZZATOPPINGS_NONE).list {
println!("- {}", &topping);
};
println!();
println!("Deliver to:");
println!("{} @ {}", &full_name, &destination)
println!("{} @ {}", *FULLNAME, *DESTINATION)
}


/// A possible base of pizza.
#[derive(Clone, Copy, Debug)]
enum PizzaBase {
/// Just the pizza dough, with nothing else on top of it.
/// Just the pizza dough, with nothing else on top f it.
Blank,
/// Pizza dough with tomato on top.
Red,
@@ -88,6 +92,7 @@ impl std::fmt::Display for PizzaBase {
}

/// The toppings
#[derive(Clone, Debug)]
struct PizzaToppingsList {
pub list: Vec<String>
}
Loading

0 comments on commit 412c3e9

Please sign in to comment.