diff --git a/.idea/micronfig.iml b/.idea/micronfig.iml index 9b4cf84..88f3b82 100644 --- a/.idea/micronfig.iml +++ b/.idea/micronfig.iml @@ -3,7 +3,10 @@ + + + diff --git a/examples/e01_the_cave/Cargo.toml b/examples/e01_the_cave/Cargo.toml new file mode 100644 index 0000000..c0517e0 --- /dev/null +++ b/examples/e01_the_cave/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "e01_the_cave" +description = "Echoes back the value of ECHO." +version = "0.0.0" +edition = "2021" + +[dependencies] +micronfig = { path = "../.." } diff --git a/examples/e01_the_cave/src/main.rs b/examples/e01_the_cave/src/main.rs new file mode 100644 index 0000000..06b673a --- /dev/null +++ b/examples/e01_the_cave/src/main.rs @@ -0,0 +1,9 @@ +use std::fmt::{Display, Formatter}; +use std::str::FromStr; + +fn main() { + let echo: String = micronfig::get("ECHO") + .expect("ECHO configuration value to be defined"); + + println!("ECHOing back: {echo}"); +} diff --git a/examples/e02_quick_math/Cargo.toml b/examples/e02_quick_math/Cargo.toml new file mode 100644 index 0000000..aa06a52 --- /dev/null +++ b/examples/e02_quick_math/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "e02_quick_math" +description = "Performs OPERATOR between FIRST and SECOND." +version = "0.0.0" +edition = "2021" + +[dependencies] +micronfig = { path = "../.." } diff --git a/examples/e02_quick_math/src/main.rs b/examples/e02_quick_math/src/main.rs new file mode 100644 index 0000000..66b04de --- /dev/null +++ b/examples/e02_quick_math/src/main.rs @@ -0,0 +1,52 @@ +use std::fmt::{Display, Formatter}; +use std::str::FromStr; + +pub enum Operator { + Sum, + Subtraction, + Multiplication, + Division, +} + +impl FromStr for Operator { + type Err = (); + + fn from_str(s: &str) -> Result { + match s { + "+" => Ok(Self::Sum), + "-" => Ok(Self::Subtraction), + "*" => Ok(Self::Multiplication), + "/" => Ok(Self::Division), + _ => Err(()) + } + } +} + +impl Display for Operator { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Sum => "+", + Self::Subtraction => "-", + Self::Multiplication => "*", + Self::Division => "/", + }) + } +} + +fn main() { + let first: u64 = micronfig::get("FIRST") + .expect("FIRST operand to be properly defined"); + let second: u64 = micronfig::get("SECOND") + .expect("SECOND operand to be properly defined"); + let operator: Operator = micronfig::get("OPERATOR") + .expect("OPERATOR to be properly defined"); + + 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}") +}