allow_me/
matcher.rs

1use crate::core::Request;
2
3/// Trait to extend [`Policy`](`crate::Policy`) resource matching.
4pub trait ResourceMatcher {
5    /// The type of the context associated with the request.
6    type Context;
7
8    /// This method is being called by [`Policy`](`crate::Policy`) when it tries to match a [`Request`] to
9    /// a resource in the policy rules.
10    fn do_match(&self, context: &Request<Self::Context>, input: &str, policy: &str) -> bool;
11}
12
13/// Default matcher uses equality check for resource matching.
14#[derive(Debug)]
15pub struct Default;
16
17impl ResourceMatcher for Default {
18    type Context = ();
19
20    fn do_match(&self, _context: &Request<Self::Context>, input: &str, policy: &str) -> bool {
21        input == policy
22    }
23}
24
25/// Resource matcher that uses "star-with" check for resource matching.
26/// Input matches the policy if input value starts with policy value.
27#[derive(Debug)]
28pub struct StartsWith;
29
30impl ResourceMatcher for StartsWith {
31    type Context = ();
32
33    fn do_match(&self, _context: &Request<Self::Context>, input: &str, policy: &str) -> bool {
34        input.starts_with(policy)
35    }
36}