♻️ 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,26 +3,33 @@ use std::{
iter::FromIterator,
};
#[derive (Debug, PartialEq)]
#[derive (Clone, Copy, Debug, PartialEq)]
enum Subcommand {
PtthServer,
PtthFileServer,
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")
{
Some (Subcommand::PtthServer)
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);
}
else if name.ends_with ("ptth_quic_end_server") || name.ends_with ("ptth_quic_end_server.exe")
{
Some (Subcommand::PtthQuicEndServer)
}
else {
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"])),
(