Shorten main function

This commit is contained in:
Takashi Kokubun 2021-12-17 23:50:57 -08:00
parent ae7d2deff4
commit d76ab68d7c
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD
2 changed files with 15 additions and 17 deletions

View File

@ -62,7 +62,10 @@ impl<'de> Deserialize<'de> for Action {
None => return serde_error::<Self::Value, M>("missing action"),
};
if let Some(key) = map.next_key::<String>()? {
return serde_error::<Self::Value, M>(&format!("only one action key is expected but also got: {}", key));
return serde_error::<Self::Value, M>(&format!(
"only one action key is expected but also got: {}",
key
));
}
Ok(action)
}

View File

@ -2,13 +2,14 @@ use evdev::EventType;
use std::env;
use std::error::Error;
use std::process::exit;
use crate::config::Config;
mod config;
mod input;
mod output;
mod transform;
fn event_loop() -> Result<(), Box<dyn Error>> {
fn event_loop(_config: &Config) -> Result<(), Box<dyn Error>> {
let mut input_device =
input::select_device().map_err(|e| format!("Failed to open an input device: {}", e))?;
let mut output_device = output::build_device(&input_device)
@ -32,28 +33,22 @@ fn event_loop() -> Result<(), Box<dyn Error>> {
}
}
fn abort(message: &str) -> ! {
println!("{}", message);
exit(1);
}
fn main() {
let filename = match env::args().nth(1) {
Some(filename) => filename,
None => {
println!("Usage: xremap <file>");
exit(1);
}
None => abort("Usage: xremap <file>"),
};
let config = match config::load_config(&filename) {
Ok(config) => config,
Err(e) => {
println!("Failed to load config '{}': {}", filename, e);
exit(1);
}
Err(e) => abort(&format!("Failed to load config '{}': {}", filename, e)),
};
println!("{:#?}", config);
match event_loop() {
Ok(()) => {}
Err(e) => {
println!("Error: {}", e);
exit(1);
}
if let Err(e) = event_loop(&config) {
abort(&format!("Error: {}", e));
}
}