generated from OBJNULL/Dockerized-Rust
Cleaned up Configuration Methods
This commit is contained in:
parent
ef7046b2c8
commit
3bc89c52a1
3 changed files with 111 additions and 20 deletions
|
@ -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<OperationMode> {
|
||||
// Getting command line arguments
|
||||
let args: Vec<String> = env::args().collect();
|
||||
// Structures
|
||||
pub struct Config {
|
||||
pub mode: OperationMode,
|
||||
//pub arguments: Arguments,
|
||||
}
|
||||
|
||||
// Getting operation mode
|
||||
match args[1].as_str() {
|
||||
"training" => Some(OperationMode::Training),
|
||||
"inference" => Some(OperationMode::Inference),
|
||||
_ => None,
|
||||
// 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
|
||||
}
|
||||
|
|
69
project/src/config/arguments.rs
Normal file
69
project/src/config/arguments.rs
Normal 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
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue