♻️ refactor: clean up `.exe` handling and add a place for `ptth_file_server`

main
_ 2021-10-02 18:58:27 +00:00
parent 4329562aa3
commit 4c79af3f4b
1 changed files with 23 additions and 12 deletions

View File

@ -3,25 +3,32 @@ use std::{
iter::FromIterator, iter::FromIterator,
}; };
#[derive (Debug, PartialEq)] #[derive (Clone, Copy, Debug, PartialEq)]
enum Subcommand { enum Subcommand {
PtthServer, PtthServer,
PtthFileServer,
PtthQuicEndServer, PtthQuicEndServer,
} }
fn parse_subcommand (name: &str) -> Option <Subcommand> fn parse_subcommand (arg: &str) -> Option <Subcommand>
{ {
if name.ends_with ("ptth_server") || name.ends_with ("ptth_server.exe") use Subcommand::*;
{
Some (Subcommand::PtthServer) let map = vec! [
} ("ptth_server", PtthServer),
else if name.ends_with ("ptth_quic_end_server") || name.ends_with ("ptth_quic_end_server.exe") ("ptth_file_server", PtthFileServer),
{ ("ptth_quic_end_server", PtthQuicEndServer),
Some (Subcommand::PtthQuicEndServer) ];
}
else { let arg = arg.strip_suffix (".exe").unwrap_or (arg);
None
for (suffix, subcommand) in &map {
if arg.ends_with (suffix) {
return Some (*subcommand);
}
} }
None
} }
fn parse_args (args: &[OsString]) -> anyhow::Result <(Subcommand, &[OsString])> fn parse_args (args: &[OsString]) -> anyhow::Result <(Subcommand, &[OsString])>
@ -60,6 +67,7 @@ async fn main () -> anyhow::Result <()> {
let (subcommand, args) = parse_args (&args)?; let (subcommand, args) = parse_args (&args)?;
match subcommand { match subcommand {
Subcommand::PtthServer => ptth_server::executable::main (&args).await, Subcommand::PtthServer => ptth_server::executable::main (&args).await,
Subcommand::PtthFileServer => unimplemented! (),
Subcommand::PtthQuicEndServer => quic_demo::executable_end_server::main (&args).await, Subcommand::PtthQuicEndServer => quic_demo::executable_end_server::main (&args).await,
} }
} }
@ -74,6 +82,7 @@ mod tests {
vec! [], vec! [],
vec! ["invalid_exe_name"], vec! ["invalid_exe_name"],
vec! ["ptth_multi_call_server"], vec! ["ptth_multi_call_server"],
vec! ["ptth_server.ex"],
vec! ["ptth_multi_call_server", "invalid_subcommand"], vec! ["ptth_multi_call_server", "invalid_subcommand"],
]; ];
@ -85,8 +94,10 @@ mod tests {
} }
let positive_cases = vec! [ 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"], (Subcommand::PtthServer, vec! ["ptth_server"])),
(vec! ["ptth_server", "--help"], (Subcommand::PtthServer, vec! ["ptth_server", "--help"])), (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_quic_end_server", "--help"], (Subcommand::PtthQuicEndServer, vec! ["ptth_quic_end_server", "--help"])),
(vec! ["ptth_multi_call_server", "ptth_server"], (Subcommand::PtthServer, vec! ["ptth_server"])), (vec! ["ptth_multi_call_server", "ptth_server"], (Subcommand::PtthServer, vec! ["ptth_server"])),
( (