From 8d0ae1058ed2ae8627d8b0b109ca4b2fb1a62ed9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Nov 2018 13:51:42 +0100 Subject: [PATCH] Make code more functional by more function chaining Signed-off-by: Matthias Beyer --- bin/domain/imag-log/src/main.rs | 47 ++++++++++++++++----------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/bin/domain/imag-log/src/main.rs b/bin/domain/imag-log/src/main.rs index 5e09efce..0be4003e 100644 --- a/bin/domain/imag-log/src/main.rs +++ b/bin/domain/imag-log/src/main.rs @@ -185,28 +185,6 @@ fn get_diary_name(rt: &Runtime) -> String { .ok_or_else(|| Error::from(err_msg("Configuration not present, cannot continue"))) .map_err_trace_exit_unwrap(1); - let logs = cfg - .read("log.logs") - .map_err(Error::from) - .map_err_trace_exit_unwrap(1) - .ok_or_else(|| Error::from(err_msg("Configuration missing: 'log.logs'"))) - .map_err_trace_exit_unwrap(1) - .as_array() - .ok_or_else(|| Error::from(err_msg("Configuration 'log.logs' is not an Array"))) - .map_err_trace_exit_unwrap(1); - - if !logs.iter().all(|e| is_match!(e, &Value::String(_))) { - error!("Configuration 'log.logs' is not an Array!"); - ::std::process::exit(1); - } - - let logs = logs - .into_iter() - .map(Value::as_str) - .map(Option::unwrap) - .map(String::from) - .collect::>(); - let current_log = cfg .read_string("log.default") .map_err(Error::from) @@ -214,13 +192,34 @@ fn get_diary_name(rt: &Runtime) -> String { .ok_or_else(|| Error::from(err_msg("Configuration missing: 'log.default'"))) .map_err_trace_exit_unwrap(1); - if !logs.contains(¤t_log) { + if cfg + .read("log.logs") + .map_err(Error::from) + .map_err_trace_exit_unwrap(1) + .ok_or_else(|| Error::from(err_msg("Configuration missing: 'log.logs'"))) + .map_err_trace_exit_unwrap(1) + .as_array() + .ok_or_else(|| Error::from(err_msg("Configuration 'log.logs' is not an Array"))) + .map_err_trace_exit_unwrap(1) + .iter() + .map(|e| if is_match!(e, &Value::String(_)) { + error!("Configuration 'log.logs' is not an Array!"); + ::std::process::exit(1) + } else { + e + }) + .map(Value::as_str) + .map(Option::unwrap) // safe by map from above + .map(String::from) + .filter(|log| log == ¤t_log) + .next() + .is_none() + { error!("'log.logs' does not contain 'log.default'"); ::std::process::exit(1) } else { current_log.into() } - } fn get_log_text(rt: &Runtime) -> String {