1
Fork 0
mirror of https://github.com/Steffo99/micronfig.git synced 2024-10-16 14:37:29 +00:00

Add two examples

This commit is contained in:
Steffo 2023-04-28 03:10:05 +02:00
parent b31d6841ee
commit 546c1e26aa
Signed by: steffo
GPG key ID: 2A24051445686895
5 changed files with 80 additions and 0 deletions

View file

@ -3,7 +3,10 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<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" />
<excludeFolder url="file://$MODULE_DIR$/examples/e01_the_cave/target" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

View 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 = "../.." }

View 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}");
}

View 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 = "../.." }

View 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}")
}