mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 10:45:30 +00:00
Run rustfmt, clean up aquatic_http_protocol/Cargo.toml
This commit is contained in:
parent
0cc312a78d
commit
d0e716f80b
65 changed files with 1754 additions and 2590 deletions
|
|
@ -2,10 +2,9 @@ use std::fs::File;
|
|||
use std::io::Read;
|
||||
|
||||
use anyhow::Context;
|
||||
use serde::{Serialize, Deserialize, de::DeserializeOwned};
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
use simplelog::{ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum LogLevel {
|
||||
|
|
@ -14,57 +13,50 @@ pub enum LogLevel {
|
|||
Warn,
|
||||
Info,
|
||||
Debug,
|
||||
Trace
|
||||
Trace,
|
||||
}
|
||||
|
||||
|
||||
impl Default for LogLevel {
|
||||
fn default() -> Self {
|
||||
Self::Error
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub trait Config: Default + Serialize + DeserializeOwned {
|
||||
fn get_log_level(&self) -> Option<LogLevel> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Options {
|
||||
config_file: Option<String>,
|
||||
print_config: bool,
|
||||
}
|
||||
|
||||
|
||||
impl Options {
|
||||
pub fn parse_args<I>(
|
||||
mut arg_iter: I
|
||||
) -> Result<Options, Option<String>>
|
||||
where I: Iterator<Item = String>
|
||||
pub fn parse_args<I>(mut arg_iter: I) -> Result<Options, Option<String>>
|
||||
where
|
||||
I: Iterator<Item = String>,
|
||||
{
|
||||
let mut options = Options::default();
|
||||
|
||||
loop {
|
||||
if let Some(arg) = arg_iter.next(){
|
||||
match arg.as_str(){
|
||||
if let Some(arg) = arg_iter.next() {
|
||||
match arg.as_str() {
|
||||
"-c" | "--config-file" => {
|
||||
if let Some(path) = arg_iter.next(){
|
||||
if let Some(path) = arg_iter.next() {
|
||||
options.config_file = Some(path);
|
||||
} else {
|
||||
return Err(
|
||||
Some("No config file path given".to_string())
|
||||
);
|
||||
return Err(Some("No config file path given".to_string()));
|
||||
}
|
||||
},
|
||||
}
|
||||
"-p" | "--print-config" => {
|
||||
options.print_config = true;
|
||||
},
|
||||
}
|
||||
"-h" | "--help" => {
|
||||
return Err(None);
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
return Err(Some("Unrecognized argument".to_string()));
|
||||
}
|
||||
|
|
@ -78,31 +70,34 @@ impl Options {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn run_app_with_cli_and_config<T>(
|
||||
app_title: &str,
|
||||
// Function that takes config file and runs application
|
||||
app_fn: fn(T) -> anyhow::Result<()>,
|
||||
opts: Option<Options>,
|
||||
) where T: Config {
|
||||
) where
|
||||
T: Config,
|
||||
{
|
||||
::std::process::exit(match run_inner(app_title, app_fn, opts) {
|
||||
Ok(()) => 0,
|
||||
Err(err) => {
|
||||
eprintln!("Error: {:#}", err);
|
||||
|
||||
1
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
fn run_inner<T>(
|
||||
app_title: &str,
|
||||
// Function that takes config file and runs application
|
||||
app_fn: fn(T) -> anyhow::Result<()>,
|
||||
// Possibly preparsed options
|
||||
options: Option<Options>,
|
||||
) -> anyhow::Result<()> where T: Config {
|
||||
) -> anyhow::Result<()>
|
||||
where
|
||||
T: Config,
|
||||
{
|
||||
let options = if let Some(options) = options {
|
||||
options
|
||||
} else {
|
||||
|
|
@ -110,18 +105,14 @@ fn run_inner<T>(
|
|||
|
||||
let app_path = arg_iter.next().unwrap();
|
||||
|
||||
match Options::parse_args(arg_iter){
|
||||
match Options::parse_args(arg_iter) {
|
||||
Ok(options) => options,
|
||||
Err(opt_err) => {
|
||||
let gen_info = || format!(
|
||||
"{}\n\nUsage: {} [OPTIONS]",
|
||||
app_title,
|
||||
app_path
|
||||
);
|
||||
let gen_info = || format!("{}\n\nUsage: {} [OPTIONS]", app_title, app_path);
|
||||
|
||||
print_help(gen_info, opt_err);
|
||||
|
||||
return Ok(())
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -137,7 +128,7 @@ fn run_inner<T>(
|
|||
T::default()
|
||||
};
|
||||
|
||||
if let Some(log_level) = config.get_log_level(){
|
||||
if let Some(log_level) = config.get_log_level() {
|
||||
start_logger(log_level)?;
|
||||
}
|
||||
|
||||
|
|
@ -145,11 +136,10 @@ fn run_inner<T>(
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn print_help<F>(
|
||||
info_generator: F,
|
||||
opt_error: Option<String>
|
||||
) where F: FnOnce() -> String {
|
||||
pub fn print_help<F>(info_generator: F, opt_error: Option<String>)
|
||||
where
|
||||
F: FnOnce() -> String,
|
||||
{
|
||||
println!("{}", info_generator());
|
||||
|
||||
println!("\nOptions:");
|
||||
|
|
@ -162,36 +152,30 @@ pub fn print_help<F>(
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
fn config_from_toml_file<T>(path: String) -> anyhow::Result<T>
|
||||
where T: DeserializeOwned
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let mut file = File::open(path.clone()).with_context(||
|
||||
format!("Couldn't open config file {}", path.clone())
|
||||
)?;
|
||||
let mut file = File::open(path.clone())
|
||||
.with_context(|| format!("Couldn't open config file {}", path.clone()))?;
|
||||
|
||||
let mut data = String::new();
|
||||
|
||||
file.read_to_string(&mut data).with_context(||
|
||||
format!("Couldn't read config file {}", path.clone())
|
||||
)?;
|
||||
file.read_to_string(&mut data)
|
||||
.with_context(|| format!("Couldn't read config file {}", path.clone()))?;
|
||||
|
||||
toml::from_str(&data).with_context(||
|
||||
format!("Couldn't parse config file {}", path.clone())
|
||||
)
|
||||
toml::from_str(&data).with_context(|| format!("Couldn't parse config file {}", path.clone()))
|
||||
}
|
||||
|
||||
|
||||
fn default_config_as_toml<T>() -> String
|
||||
where T: Default + Serialize
|
||||
where
|
||||
T: Default + Serialize,
|
||||
{
|
||||
toml::to_string_pretty(&T::default())
|
||||
.expect("Could not serialize default config to toml")
|
||||
toml::to_string_pretty(&T::default()).expect("Could not serialize default config to toml")
|
||||
}
|
||||
|
||||
|
||||
fn start_logger(log_level: LogLevel) -> ::anyhow::Result<()> {
|
||||
let level_filter = match log_level{
|
||||
let level_filter = match log_level {
|
||||
LogLevel::Off => LevelFilter::Off,
|
||||
LogLevel::Error => LevelFilter::Error,
|
||||
LogLevel::Warn => LevelFilter::Warn,
|
||||
|
|
@ -206,11 +190,8 @@ fn start_logger(log_level: LogLevel) -> ::anyhow::Result<()> {
|
|||
.set_location_level(LevelFilter::Off)
|
||||
.build();
|
||||
|
||||
TermLogger::init(
|
||||
level_filter,
|
||||
simplelog_config,
|
||||
TerminalMode::Stderr
|
||||
).context("Couldn't initialize logger")?;
|
||||
TermLogger::init(level_filter, simplelog_config, TerminalMode::Stderr)
|
||||
.context("Couldn't initialize logger")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue