Cleaned up Configuration Methods

This commit is contained in:
Maddox Werts 2025-03-21 09:37:03 -04:00
parent ef7046b2c8
commit 3bc89c52a1
3 changed files with 111 additions and 20 deletions

View file

@ -1,5 +1,6 @@
// Libraries // Libraries
use std::env; mod arguments;
use arguments::Arguments;
// Enums // Enums
pub enum OperationMode { pub enum OperationMode {
@ -7,15 +8,40 @@ pub enum OperationMode {
Inference, Inference,
} }
// Functions // Structures
pub fn get_operation_mode() -> Option<OperationMode> { pub struct Config {
// Getting command line arguments pub mode: OperationMode,
let args: Vec<String> = env::args().collect(); //pub arguments: Arguments,
}
// Getting operation mode // Implementations
match args[1].as_str() { impl Config {
"training" => Some(OperationMode::Training), // Constructors
"inference" => Some(OperationMode::Inference), pub fn new() -> Self {
_ => None, // Grabbing all arguments
let arguments: Arguments = Arguments::new();
// What mode do we want?
let mode: OperationMode;
match arguments.find("--mode") {
None => {
eprintln!("Config> Mode missing... Assuming Inference");
mode = OperationMode::Inference;
} }
Some(arg) => match arg.val.as_str() {
"inference" => mode = OperationMode::Inference,
"training" => mode = OperationMode::Training,
_ => {
eprintln!("Config> Invalid Mode... Assuming Inference");
eprintln!("Config> Valid Modes are: inference, training");
mode = OperationMode::Inference;
}
},
}
// Return Result
Self { mode }
}
// Functions
} }

View file

@ -0,0 +1,69 @@
// Libraries
use std::env;
// Constants
// Structures
pub struct Argument {
pub key: String,
pub val: String,
}
pub struct Arguments {
arguments: Vec<Argument>,
}
// Implementations
impl Arguments {
// Constructor
pub fn new() -> Self {
// Result
let mut result: Vec<Argument> = Vec::new();
// Getting Arguments
let args: Vec<String> = env::args().collect();
// Going through all arguments
for i in 0..args.len() {
// Checking if it's an argument
if args[i].starts_with("--") {
continue;
}
// Figuring out what kind of variable it is
if args[i + 1].starts_with("--") {
result.push(Argument {
key: args[i].clone(),
val: String::new(),
});
} else {
result.push(Argument {
key: args[i].clone(),
val: args[i + 1].clone(),
});
}
}
// Return Result
Self { arguments: result }
}
// Functions
pub fn find(&self, key: &str) -> Option<&Argument> {
// Storing Result
let mut result: Option<&Argument> = None;
// Looping through arguments to find the one we need
for arg in &self.arguments {
// Comparing the keys
if key != arg.key {
continue;
}
// This is it!
result = Some(arg);
}
// Could not find it...
result
}
}

View file

@ -1,23 +1,19 @@
// Libraries // Libraries
mod config; mod config;
use config::Config;
mod neural; mod neural;
use neural::NeuralNetwork; use neural::NeuralNetwork;
// Entry-Point // Entry-Point
fn main() { fn main() {
// Getting Running Mode First // Reading configuration
let operation_mode = config::get_operation_mode(); let config: Config = Config::new();
// Creating a Neural Network // Creating a Neural Network
let neural: NeuralNetwork; let neural: NeuralNetwork;
// Creating a Neural Network with the Operation Mode // Creating a Neural Network with the Operation Mode
match operation_mode { neural = NeuralNetwork::new(config.mode);
Some(mode) => {
neural = NeuralNetwork::new(mode);
},
_ => panic!("Main: `OperationMode` not defined!"),
}
// Starting the network // Starting the network
neural.start(); neural.start();