add aquatic crate with master executable, refactor cli_helpers

This commit is contained in:
Joakim Frostegård 2020-08-13 00:13:01 +02:00
parent c69b09ab9c
commit 9efc1fc66a
17 changed files with 243 additions and 62 deletions

16
aquatic/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "aquatic"
version = "0.1.0"
authors = ["Joakim Frostegård <joakim.frostegard@gmail.com>"]
edition = "2018"
license = "Apache-2.0"
[[bin]]
name = "aquatic"
[dependencies]
aquatic_cli_helpers = { path = "../aquatic_cli_helpers" }
aquatic_http = { path = "../aquatic_http" }
aquatic_udp = { path = "../aquatic_udp" }
aquatic_ws = { path = "../aquatic_ws" }
mimalloc = { version = "0.1", default-features = false }

99
aquatic/src/main.rs Normal file
View file

@ -0,0 +1,99 @@
use aquatic_cli_helpers::{Options, run_app_with_cli_and_config, print_help};
use aquatic_http::config::Config as HttpConfig;
use aquatic_udp::config::Config as UdpConfig;
use aquatic_ws::config::Config as WsConfig;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
const APP_NAME: &str = "aquatic: BitTorrent tracker";
fn main(){
::std::process::exit(match run(){
Ok(()) => 0,
Err(None) => {
print_help(|| gen_info(), None);
0
},
Err(opt_err@Some(_)) => {
print_help(|| gen_info(), opt_err);
1
},
})
}
fn run() -> Result<(), Option<String>>{
let mut arg_iter = ::std::env::args().skip(1);
let protocol = if let Some(protocol) = arg_iter.next(){
protocol
} else {
return Err(None);
};
let options = match Options::parse_args(arg_iter){
Ok(options) => options,
Err(opt_err) => {
return Err(opt_err);
}
};
match protocol.as_str() {
"udp" => {
run_app_with_cli_and_config::<UdpConfig>(
aquatic_udp::APP_NAME,
aquatic_udp::run,
Some(options),
)
},
"http" => {
run_app_with_cli_and_config::<HttpConfig>(
aquatic_http::APP_NAME,
aquatic_http::run,
Some(options),
)
},
"ws" => {
run_app_with_cli_and_config::<WsConfig>(
aquatic_ws::APP_NAME,
aquatic_ws::run,
Some(options),
)
},
arg => {
let opt_err = if arg == "-h" || arg == "--help" {
None
} else if arg.chars().next() == Some('-'){
Some("First argument must be protocol".to_string())
} else {
Some("Invalid protocol".to_string())
};
return Err(opt_err)
},
}
Ok(())
}
fn gen_info() -> String {
let mut info = String::new();
info.push_str(APP_NAME);
let app_path = ::std::env::args().next().unwrap();
info.push_str(&format!("\n\nUsage: {} PROTOCOL [OPTIONS]", app_path));
info.push_str("\n\nAvailable protocols:");
info.push_str("\n udp");
info.push_str("\n http");
info.push_str("\n ws");
info
}