refactor: use anyhow for error handling

This commit is contained in:
2021-02-25 20:36:14 +01:00
parent 6b35594f1a
commit 1b6835eddb
6 changed files with 30 additions and 35 deletions

View File

@ -1,12 +1,11 @@
use std::io::BufReader;
use std::fs::File;
use anyhow::{Context, Result};
use handlebars::Handlebars;
use serde_derive::Deserialize;
use serde_json::json;
use crate::exit::exit_with_error;
fn default_ldap_url() -> String {
"ldap://localhost".to_string()
}
@ -33,24 +32,17 @@ pub struct Config {
pub port: u16,
}
pub fn check_config(config: Config) -> Config {
let hb = Handlebars::new();
pub fn load_config(file_path: &str) -> Result<Config> {
let file = File::open(file_path).
with_context(|| format!("Failed to open configuration file '{}'", file_path))?;
if let Some(e) = hb.render_template(&config.dn, &json!({"username" : "foo"})).err() {
exit_with_error(&format!("Failed to parse 'dn' ({}) in configuration file: {}", config.dn, e))
};
let config: Config = serde_json::from_reader(BufReader::new(file))
.with_context(|| format!("Failed to parse configuration file '{}'", file_path))?;
config
}
pub fn load_config(file_path: &str) -> Config {
let file = match File::open(file_path) {
Ok(file) => file,
Err(_) => exit_with_error(&format!("Failed to open configuration file '{}'", file_path))
};
match serde_json::from_reader(BufReader::new(file)) {
Ok(config) => check_config(config),
Err(_) => exit_with_error(&format!("Failed to parse configuration file '{}'", file_path))
}
Handlebars::new()
.render_template(&config.dn, &json!({"username" : "foo"}))
.map(|_| ())
.with_context(|| format!("Failed to parse DN ({}) in configuration file '{}'", config.dn, file_path))?;
Ok(config)
}

View File

@ -1,4 +0,0 @@
pub fn exit_with_error(message: &str) -> ! {
eprintln!("{}", message);
std::process::exit(-1)
}

View File

@ -2,19 +2,18 @@
mod api;
mod config;
mod exit;
mod r#static;
use anyhow::{Context, Result};
use clap::{Arg, App, crate_version};
use rocket::routes;
use rocket::config::{Config, Environment};
use crate::config::load_config;
use crate::exit::exit_with_error;
const DEFAULT_CONFIG_FILE_PATH: &str = "/etc/webldappasswd/config.json";
fn main() {
fn main() -> Result<()> {
let matches = App::new("WebLDAPPasswd")
.version(crate_version!())
.arg(Arg::with_name("config")
@ -29,16 +28,13 @@ fn main() {
.value_of("config")
.unwrap_or(DEFAULT_CONFIG_FILE_PATH);
let config = load_config(config_file_path);
let config = load_config(config_file_path)?;
let rocket_config_builder = Config::build(Environment::Production)
let rocket_config = Config::build(Environment::Production)
.address(&config.host)
.port(config.port);
let rocket_config = match rocket_config_builder.finalize() {
Ok(config) => config,
Err(e) => exit_with_error(&format!("Bad host address ({}) in configuration file: {}", config.host, e)),
};
.port(config.port)
.finalize()
.with_context(|| format!("Bad host address ({}) in configuration file '{}'", config.host, config_file_path))?;
rocket::custom(rocket_config)
.mount("/", routes![r#static::index,
@ -51,4 +47,6 @@ fn main() {
api::update])
.manage(config)
.launch();
Ok(())
}