is_tree/traits/
has_path_segment.rs

1//! Traits for types that have a path segment.
2
3use crate::Path;
4
5/// This trait should be implemented by types that have an absolute path.
6pub trait HasPath {
7    /// Gets the path of the type.
8    fn path(&self) -> Path;
9}
10
11impl HasPath for () {
12    fn path(&self) -> Path {
13        Path::default()
14    }
15}
16
17impl<T> HasPath for Box<T>
18where
19    T: HasPath,
20{
21    fn path(&self) -> Path {
22        (**self).path()
23    }
24}
25
26/// This trait should be implemented by types that have a path segment.
27pub trait HasPathSegment {
28    /// Gets the path segment of the type.
29    fn path_segment(&self) -> String;
30
31    /// Checks if the type is identified by the given path segment.
32    fn is(&self, identifier: impl PartialEq<String>) -> bool {
33        identifier.eq(&self.path_segment())
34    }
35}
36
37impl<T: HasPathSegment> HasPathSegment for &T {
38    fn path_segment(&self) -> String {
39        (*self).path_segment()
40    }
41}
42
43impl<T: HasPathSegment> HasPathSegment for &mut T {
44    fn path_segment(&self) -> String {
45        (**self).path_segment()
46    }
47}
48
49impl HasPathSegment for String {
50    fn path_segment(&self) -> String {
51        self.clone()
52    }
53}