rs_jsonrpc_server_utils/
matcher.rs

1use globset::{GlobMatcher, GlobBuilder};
2use std::ascii::AsciiExt;
3use std::{fmt, hash};
4
5/// Pattern that can be matched to string.
6pub trait Pattern {
7	/// Returns true if given string matches the pattern.
8	fn matches<T: AsRef<str>>(&self, other: T) -> bool;
9}
10
11#[derive(Clone)]
12pub struct Matcher(Option<GlobMatcher>, String);
13impl Matcher {
14	pub fn new(string: &str) -> Matcher {
15		Matcher(
16			GlobBuilder::new(string)
17				.case_insensitive(true)
18				.build()
19				.map(|g| g.compile_matcher())
20				.map_err(|e| warn!("Invalid glob pattern for {}: {:?}", string, e))
21				.ok(),
22			string.into()
23		)
24	}
25}
26
27impl Pattern for Matcher {
28	fn matches<T: AsRef<str>>(&self, other: T) -> bool {
29		let s = other.as_ref();
30		match self.0 {
31			Some(ref matcher) => matcher.is_match(s),
32			None => self.1.eq_ignore_ascii_case(s),
33		}
34	}
35}
36
37impl fmt::Debug for Matcher {
38	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
39		write!(fmt, "{:?} ({})", self.1, self.0.is_some())
40	}
41}
42
43impl hash::Hash for Matcher {
44	fn hash<H>(&self, state: &mut H) where H: hash::Hasher {
45		self.1.hash(state)
46	}
47}
48
49impl Eq for Matcher {}
50impl PartialEq for Matcher {
51	fn eq(&self, other: &Matcher) -> bool {
52		self.1.eq(&other.1)
53	}
54}