From 4c79af3f4b75e6146e7be84e6fe389a582a0a0dc Mon Sep 17 00:00:00 2001 From: _ <> Date: Sat, 2 Oct 2021 18:58:27 +0000 Subject: [PATCH] :recycle: refactor: clean up `.exe` handling and add a place for `ptth_file_server` --- crates/ptth_multi_call_server/src/main.rs | 35 +++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/crates/ptth_multi_call_server/src/main.rs b/crates/ptth_multi_call_server/src/main.rs index 479453c..7349f62 100644 --- a/crates/ptth_multi_call_server/src/main.rs +++ b/crates/ptth_multi_call_server/src/main.rs @@ -3,25 +3,32 @@ use std::{ iter::FromIterator, }; -#[derive (Debug, PartialEq)] +#[derive (Clone, Copy, Debug, PartialEq)] enum Subcommand { PtthServer, + PtthFileServer, PtthQuicEndServer, } -fn parse_subcommand (name: &str) -> Option +fn parse_subcommand (arg: &str) -> Option { - if name.ends_with ("ptth_server") || name.ends_with ("ptth_server.exe") - { - Some (Subcommand::PtthServer) - } - else if name.ends_with ("ptth_quic_end_server") || name.ends_with ("ptth_quic_end_server.exe") - { - Some (Subcommand::PtthQuicEndServer) - } - else { - None + use Subcommand::*; + + let map = vec! [ + ("ptth_server", PtthServer), + ("ptth_file_server", PtthFileServer), + ("ptth_quic_end_server", PtthQuicEndServer), + ]; + + let arg = arg.strip_suffix (".exe").unwrap_or (arg); + + for (suffix, subcommand) in &map { + if arg.ends_with (suffix) { + return Some (*subcommand); + } } + + None } fn parse_args (args: &[OsString]) -> anyhow::Result <(Subcommand, &[OsString])> @@ -60,6 +67,7 @@ async fn main () -> anyhow::Result <()> { let (subcommand, args) = parse_args (&args)?; match subcommand { Subcommand::PtthServer => ptth_server::executable::main (&args).await, + Subcommand::PtthFileServer => unimplemented! (), Subcommand::PtthQuicEndServer => quic_demo::executable_end_server::main (&args).await, } } @@ -74,6 +82,7 @@ mod tests { vec! [], vec! ["invalid_exe_name"], vec! ["ptth_multi_call_server"], + vec! ["ptth_server.ex"], vec! ["ptth_multi_call_server", "invalid_subcommand"], ]; @@ -85,8 +94,10 @@ mod tests { } let positive_cases = vec! [ + (vec! ["ptth_server.exe"], (Subcommand::PtthServer, vec! ["ptth_server.exe"])), (vec! ["ptth_server"], (Subcommand::PtthServer, vec! ["ptth_server"])), (vec! ["ptth_server", "--help"], (Subcommand::PtthServer, vec! ["ptth_server", "--help"])), + (vec! ["ptth_file_server"], (Subcommand::PtthFileServer, vec! ["ptth_file_server"])), (vec! ["ptth_quic_end_server", "--help"], (Subcommand::PtthQuicEndServer, vec! ["ptth_quic_end_server", "--help"])), (vec! ["ptth_multi_call_server", "ptth_server"], (Subcommand::PtthServer, vec! ["ptth_server"])), (