From e16d9873db70d29d409ae989c9b18f28f1652434 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 10 Sep 2018 00:45:53 +0200 Subject: use unwrap! instead of if let --- src/main.rs | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2e3007c..85018c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,16 @@ mod output; mod filters; +macro_rules! unwrap { + ($p:pat = $e:expr => { $r:expr} ) => { + if let $p = $e { + $r + } else { + panic!("Pattern match failed!"); + } + }; +} + /// A program that allows you to search through all your evtc logs for specific /// people. #[derive(StructOpt, Debug)] @@ -230,25 +240,20 @@ fn extract_info(entry: &DirEntry, log: &Log) -> LogResult { let mut players = log .players() .map(|p| { - if let AgentKind::Player { profession, elite } = p.kind() { - if let AgentName::Player { - account_name, - character_name, - subgroup, - } = p.name() - { - Player { - account_name: account_name.clone(), - character_name: character_name.clone(), - profession: get_profession_name(*profession, *elite).into(), - subgroup: *subgroup, - } - } else { - unreachable!() + unwrap! { AgentKind::Player { profession, elite } = p.kind() => { + unwrap! { AgentName::Player { + account_name, + character_name, + subgroup, + } = p.name() => + { + Player { + account_name: account_name.clone(), + character_name: character_name.clone(), + profession: get_profession_name(*profession, *elite).into(), + subgroup: *subgroup, } - } else { - unreachable!() - } + }}}} }).collect::>(); players.sort_by_key(|p| p.subgroup); -- cgit v1.2.3