From 74dc6574650a157ab57779dc633e140d020b792a Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 May 2020 15:25:33 +0200 Subject: add -class player filter --- src/filters/player.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/filters/player.rs') diff --git a/src/filters/player.rs b/src/filters/player.rs index 3af2be2..2b14eb0 100644 --- a/src/filters/player.rs +++ b/src/filters/player.rs @@ -3,12 +3,12 @@ //! Additionally, it provides methods to lift a player filter to a log filter with [`any`][any] and //! [`all`][all]. use super::{ - super::{guilds, EarlyLogResult, LogResult, Player, SearchField}, + super::{guilds, playerclass::PlayerClass, EarlyLogResult, LogResult, Player, SearchField}, log::LogFilter, Filter, Inclusion, }; -use std::convert::TryFrom; +use std::{collections::HashSet, convert::TryFrom}; use evtclib::{Agent, AgentKind}; @@ -113,3 +113,25 @@ pub fn character(regex: Regex) -> Box { pub fn account(regex: Regex) -> Box { name(SearchField::Account, regex) } + +#[derive(Clone, Debug)] +struct ClassFilter(HashSet); + +impl Filter for ClassFilter { + fn filter_early(&self, agent: &Agent) -> Inclusion { + if let AgentKind::Player(ref player) = agent.kind() { + self.0.contains(&player.into()).into() + } else { + Inclusion::Unknown + } + } + + fn filter(&self, player: &Player) -> bool { + self.0.contains(&player.profession) + } +} + +/// Construct a `PlayerFilter` that matches only the given classes. +pub fn class(classes: HashSet) -> Box { + Box::new(ClassFilter(classes)) +} -- cgit v1.2.3