scopes_rs/
lib.rs

1#![warn(missing_docs)]
2#![doc = include_str!("../README.md")]
3//!
4//! # Crate features
5//! 
6//! - `hierarchy`: Enable hierarchical scopes support. See [`hierarchy`] for more details.
7//! 
8
9pub mod scope;
10
11pub mod error;
12
13#[cfg(feature = "hierarchy")]
14pub mod hierarchy;
15
16pub mod policy;
17
18/// Derive macro for the [`Scope`](scope::Scope) trait
19pub mod derive {
20
21    /// Derive macro to implement the [`Scope`] trait
22    /// 
23    /// Derives an implementation of [`Scope`] for an enum. The enum should also
24    /// implement [`PartialEq`].
25    /// 
26    /// It infers the scope name from the enum variant for the [`FromStr`](std::str::FromStr) implementation,
27    /// and for ordering scopes for the [`Hierarchized`] trait (when the `hierarchy` feature is enabled).
28    /// 
29    /// 
30    /// ```
31    /// use scopes_rs::derive::Scope;
32    /// 
33    /// #[derive(Clone, PartialEq, Scope)]
34    /// enum MyScope {
35    ///     Foo,
36    ///     Bar,
37    /// }
38    /// ```
39    /// 
40    /// # Inferred scope name
41    /// 
42    /// The scope name is inferred from the enum variant name by splitting it at each uppercase letter,
43    /// the converting each label to lowercase, and joining the labels with the `separator` (`.` by default).
44    /// 
45    /// A few examples :
46    /// 
47    /// | Variant name | Inferred name |
48    /// |--------------|---------------|
49    /// | `Foo` | `foo` |
50    /// | `FooBar` | `foo.bar` |
51    /// | `foo_bar` | `foo_bar` |
52    /// | `BAZ` | `b.a.z` |
53    /// 
54    /// Labels do not convoy any special meaning, but are used to infer the scopes hierarchy 
55    /// (when the `hierarchy` feature is enabled). A scope will include another one if its
56    /// labels are a prefix of the other one. For example, `foo.bar` includes `foo.bar.baz` but
57    /// not `baz`. See the [`hierarchy`](crate::hierarchy) module documentation for more details.
58    /// 
59    ///  When using the `rename` attribute, the labels will be parsed by splitting the name by `separator`.
60    /// 
61    /// # Errors
62    /// 
63    /// Compilation will fail if scopes have conflicting names :
64    /// 
65    /// ```compile_fail
66    /// #[derive(Clone, PartialEq, Scope)]
67    /// enum MyScope {
68    ///     Foo,
69    ///     #[scope(rename = "foo")]
70    ///     Bar,
71    /// }
72    /// ```
73    /// 
74    /// # Optional attributes 
75    /// 
76    /// [`Scope`]: ../scope/Scope
77    /// [`Hierarchized`]: ../hierarchy/Hierarchized
78    pub use scopes_macros::Scope;
79}
80
81// Implement the `Scope` trait for String for the tests
82#[cfg(test)]
83mod tests {
84    use crate::scope::Scope;
85
86    #[cfg(feature = "hierarchy")]
87    use crate::hierarchy::Hierarchized;
88
89    impl Scope for String {}
90
91    #[cfg(feature = "hierarchy")]
92    impl Hierarchized for String {
93        fn includes(&self, other: &Self) -> bool {
94            if self == other {
95                return true;
96            }
97            other.starts_with(self)
98        }
99    }
100
101}