From 06b27b458589bcc08a7193b0d49cf471d7822c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Mon, 30 Oct 2023 02:07:05 +0300 Subject: [PATCH] feat: added help menu refactor: debug_mode --- src/main.rs | 83 +++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7ebceea..5e60a3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ struct UserEnvironment server:bool, send:bool, location:Option, - debug:DebugMode, + debug:bool, } #[derive(Debug)] struct FileInfo @@ -339,32 +339,6 @@ impl FileInfo } } } -#[derive(Debug)] -enum DebugMode - { - On, - Off - } -impl DebugMode { - fn debug_mode(&self) -> bool - { - match self - { - DebugMode::On => - { - println!("Debug: ON"); - let debug = true; - debug - } - DebugMode::Off => - { - println!("Debug: OFF"); - let debug = false; - debug - } - } - } -} enum Connection { Server(String, String), @@ -373,10 +347,10 @@ enum Connection impl Connection { - fn server(self, file_info:&mut FileInfo, debug_mode:bool, user_environment:&UserEnvironment) + fn server(self, file_info:&mut FileInfo, user_environment:&UserEnvironment) { print!("Server -> "); - if debug_mode + if user_environment.debug { println!("{:#?}", user_environment); println!("{:#?}", file_info); @@ -403,7 +377,7 @@ impl Connection Ok(mut stream) => { println!("Connected"); - send_or_receive(file_info, &mut stream, &debug_mode, user_environment); + send_or_receive(file_info, &mut stream, &user_environment.debug, user_environment); } Err(e) => { @@ -413,10 +387,10 @@ impl Connection } } } - fn client(self, file_info:&mut FileInfo, debug_mode:bool, user_environment:&UserEnvironment) + fn client(self, file_info:&mut FileInfo, user_environment:&UserEnvironment) { print!("Client -> "); - if debug_mode + if user_environment.debug { println!("{:#?}", user_environment); println!("{:#?}", file_info); @@ -440,7 +414,7 @@ impl Connection Ok(mut stream) => { println!("Connected"); - send_or_receive(file_info, &mut stream, &debug_mode, user_environment); + send_or_receive(file_info, &mut stream, &user_environment.debug, user_environment); } Err(e) => { @@ -470,13 +444,13 @@ fn send_or_receive(file_info:&mut FileInfo, stream:&mut TcpStream, debug_mode:&b } } } -fn take_args(user_environment:&mut UserEnvironment) +fn take_args(user_environment:&mut UserEnvironment) -> bool { let env_args:Vec = env::args().collect(); - if env_args.len() > 15 + if env_args.len() > 16 { println!("Error: Too Many Arguments, You Gave {} Arguments", env_args.len()); - panic!(); + return false; } let mut i = 1; while i < env_args.len() @@ -516,15 +490,39 @@ fn take_args(user_environment:&mut UserEnvironment) } "--debug" => { - user_environment.debug = DebugMode::On; + user_environment.debug = false; } + "--help" => + { + show_help(); + return false; + + } err => { println!("Error: Invalid Argument, You Gave {}", err); + return false; } } i += 1; } + true + } +fn show_help() + { + println!("\n\n\n"); + println!(" Arguments | Details | Defaults"); + println!("--------------------------------------------------------------"); + println!(" --ip | Specify IP Address | 127.0.0.1"); + println!(" --port | Specify Port Address | 2121"); + println!(" --location | Specify Location Address | Same as Program"); + println!(" --server | Start as a Server | False"); + println!(" --client | Start as a Client | True"); + println!(" --send | Start as a Sender | False"); + println!(" --receive | Start as a Receiver | True"); + println!(" --debug | Start in Debug Mode | False"); + println!(" --help | Shows Help | False"); + println!("\n\n\n"); } fn main() { @@ -539,9 +537,12 @@ fn main() server:false, send:false, location:None, - debug:DebugMode::Off, + debug:false, }; - take_args(&mut user_environment); + if !take_args(&mut user_environment) + { + return; + } let mut file_info = FileInfo { file:None, @@ -555,13 +556,13 @@ fn main() { Connection::server (Connection::Server(user_environment.ip.to_string(), user_environment.port.to_string()), - &mut file_info, DebugMode::debug_mode(&user_environment.debug), &user_environment); + &mut file_info, &user_environment); }, false => { Connection::client (Connection::Client(user_environment.ip.to_string(), user_environment.port.to_string()), - &mut file_info, DebugMode::debug_mode(&user_environment.debug), &user_environment); + &mut file_info, &user_environment); } } }