1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//! Traits for types that have a path segment.

use crate::Path;

/// This trait should be implemented by types that have an absolute path.
pub trait HasPath {
    /// Gets the path of the type.
    fn path(&self) -> Path;
}

impl HasPath for () {
    fn path(&self) -> Path {
        Path::default()
    }
}

/// This trait should be implemented by types that have a path segment.
pub trait HasPathSegment {
    /// Gets the path segment of the type.
    fn path_segment(&self) -> String;

    /// Checks if the type is identified by the given path segment.
    fn is(&self, identifier: impl PartialEq<String>) -> bool {
        identifier.eq(&self.path_segment())
    }
}

impl<T: HasPathSegment> HasPathSegment for &T {
    fn path_segment(&self) -> String {
        (*self).path_segment()
    }
}

impl<T: HasPathSegment> HasPathSegment for &mut T {
    fn path_segment(&self) -> String {
        (**self).path_segment()
    }
}

impl HasPathSegment for String {
    fn path_segment(&self) -> String {
        self.clone()
    }
}