mirror of
https://github.com/Steffo99/micronfig.git
synced 2024-11-21 15:44:20 +00:00
Add two examples
This commit is contained in:
parent
b31d6841ee
commit
546c1e26aa
5 changed files with 80 additions and 0 deletions
|
@ -3,7 +3,10 @@
|
||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/examples/e01_the_cave/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/examples/e01_the_cave/target" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
|
|
8
examples/e01_the_cave/Cargo.toml
Normal file
8
examples/e01_the_cave/Cargo.toml
Normal file
|
@ -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 = "../.." }
|
9
examples/e01_the_cave/src/main.rs
Normal file
9
examples/e01_the_cave/src/main.rs
Normal file
|
@ -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}");
|
||||||
|
}
|
8
examples/e02_quick_math/Cargo.toml
Normal file
8
examples/e02_quick_math/Cargo.toml
Normal file
|
@ -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 = "../.." }
|
52
examples/e02_quick_math/src/main.rs
Normal file
52
examples/e02_quick_math/src/main.rs
Normal file
|
@ -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<Self, Self::Err> {
|
||||||
|
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}")
|
||||||
|
}
|
Loading…
Reference in a new issue