scopes_rs/
scope.rs

1//! Contains the trait that types representing a scope should implement
2
3use std::str::FromStr;
4
5#[cfg(feature = "hierarchy")]
6use crate::hierarchy::Hierarchized;
7use crate::policy::{Policy, PolicyBuilder};
8
9#[cfg(not(feature = "hierarchy"))]
10/// A trait implemented by types representing a scope.
11/// 
12/// The [`FromStr`] implementation is used to parse scopes
13/// from strings.
14pub trait Scope: FromStr + PartialEq {}
15
16#[cfg(feature = "hierarchy")]
17/// A trait implemented by types representing a scope.
18/// 
19/// The [`FromStr`] implementation is used to parse scopes
20/// from strings.
21/// 
22/// The [`Hierarchized`] trait is only required with the `hierarchy`
23/// feature.
24pub trait Scope: FromStr + PartialEq + Hierarchized {}
25
26
27/// Used to do a cheap reference-to-reference conversion
28pub trait AsScopeRef<S: Scope> {
29    /// Converts this type to a reference 
30    fn as_scope_ref(&self) -> &S;
31}
32
33impl<S: Scope> Policy<S> {
34    /// Create a new [`PolicyBuilder<S>`]
35    /// 
36    /// Equivalent to calling [`PolicyBuilder<S>::new`]
37    pub fn builder() -> PolicyBuilder<S> {
38        PolicyBuilder::new()
39    }
40}
41
42impl<S: Scope> AsScopeRef<S> for S {
43    fn as_scope_ref(&self) -> &S {
44        self
45    }
46}
47
48impl<'a, S: Scope> AsScopeRef<S> for &'a S {
49    fn as_scope_ref(&self) -> &S {
50        *self
51    }
52}
53
54impl<'a, 'b, S: Scope> AsScopeRef<S> for &'a &'b S {
55    fn as_scope_ref(&self) -> &S {
56        **self
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use crate::scope::AsScopeRef;
63
64    #[test]
65    fn test_as_scope_ref() {
66
67        let scope = "foo".to_string();
68
69        assert_eq!(&scope, scope.as_scope_ref());
70        assert_eq!(&scope, (&scope).as_scope_ref());
71        assert_eq!(&scope, (&&scope).as_scope_ref());
72        assert_eq!(&scope, (&&&scope).as_scope_ref());
73
74    }
75}