diff options
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index 3246864..31e29ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,10 +42,13 @@ struct Opt { path: PathBuf, /// The fields which should be searched. - /// Possible values: all, account, character #[structopt(short = "f", long = "fields", default_value = "all")] field: SearchField, + /// Only display fights with the given outcome. + #[structopt(short = "o", long = "outcome")] + outcome: Option<FightOutcome>, + /// The regular expression to search for. #[structopt(name = "EXPR")] expression: Regex, @@ -124,6 +127,18 @@ pub enum FightOutcome { Wipe, } +impl FromStr for FightOutcome { + type Err = &'static str; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + match s { + "success" => Ok(FightOutcome::Success), + "wipe" | "fail" => Ok(FightOutcome::Wipe), + _ => Err("Must be success or wipe"), + } + } +} + fn main() { let opt = Opt::from_args(); let result = grep(&opt); @@ -183,6 +198,7 @@ fn search_log(entry: &DirEntry, opt: &Opt) -> Result<Option<LogResult>, RuntimeE return Ok(None); }; + let mut candidate_good = false; for player in log.players() { match player.name() { AgentName::Player { @@ -193,14 +209,24 @@ fn search_log(entry: &DirEntry, opt: &Opt) -> Result<Option<LogResult>, RuntimeE if (opt.field.search_account() && opt.expression.is_match(account_name)) || (opt.field.search_character() && opt.expression.is_match(character_name)) { - return Ok(Some(extract_info(entry, &log))); + candidate_good = true; } } _ => unreachable!(), } } - Ok(None) + if !candidate_good { + return Ok(None); + } + + let result = extract_info(entry, &log); + + match opt.outcome { + Some(o) if o == result.outcome => Ok(Some(result)), + None => Ok(Some(result)), + _ => Ok(None), + } } /// Extract human-readable information from the given log file. |