diff --git a/project/src/config.rs b/project/src/config.rs index 27ff69d..c5db45f 100644 --- a/project/src/config.rs +++ b/project/src/config.rs @@ -1,5 +1,6 @@ // Libraries -use std::env; +mod arguments; +use arguments::Arguments; // Enums pub enum OperationMode { @@ -7,15 +8,40 @@ pub enum OperationMode { Inference, } -// Functions -pub fn get_operation_mode() -> Option { - // Getting command line arguments - let args: Vec = env::args().collect(); - - // Getting operation mode - match args[1].as_str() { - "training" => Some(OperationMode::Training), - "inference" => Some(OperationMode::Inference), - _ => None, - } +// Structures +pub struct Config { + pub mode: OperationMode, + //pub arguments: Arguments, +} + +// Implementations +impl Config { + // Constructors + pub fn new() -> Self { + // 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 } diff --git a/project/src/config/arguments.rs b/project/src/config/arguments.rs new file mode 100644 index 0000000..3176dcd --- /dev/null +++ b/project/src/config/arguments.rs @@ -0,0 +1,69 @@ +// Libraries +use std::env; + +// Constants + +// Structures +pub struct Argument { + pub key: String, + pub val: String, +} +pub struct Arguments { + arguments: Vec, +} + +// Implementations +impl Arguments { + // Constructor + pub fn new() -> Self { + // Result + let mut result: Vec = Vec::new(); + + // Getting Arguments + let args: Vec = 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 + } +} diff --git a/project/src/main.rs b/project/src/main.rs index 1d190de..2199cf5 100644 --- a/project/src/main.rs +++ b/project/src/main.rs @@ -1,23 +1,19 @@ // Libraries mod config; +use config::Config; mod neural; use neural::NeuralNetwork; // Entry-Point fn main() { - // Getting Running Mode First - let operation_mode = config::get_operation_mode(); + // Reading configuration + let config: Config = Config::new(); // Creating a Neural Network let neural: NeuralNetwork; // Creating a Neural Network with the Operation Mode - match operation_mode { - Some(mode) => { - neural = NeuralNetwork::new(mode); - }, - _ => panic!("Main: `OperationMode` not defined!"), - } + neural = NeuralNetwork::new(config.mode); // Starting the network neural.start();