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