rpds_pathtree/
path.rs

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
45
46
47
48
// SPDX-FileCopyrightText: The rpds-pathtree authors
// SPDX-License-Identifier: MPL-2.0

use std::{ffi::OsStr, fmt, hash::Hash};

/// Borrowed path segment.
pub trait PathSegment: Eq + Hash + fmt::Debug {
    /// Check if the segment is empty.
    #[must_use]
    fn is_empty(&self) -> bool;
}

impl PathSegment for str {
    fn is_empty(&self) -> bool {
        self.is_empty()
    }
}

impl PathSegment for OsStr {
    fn is_empty(&self) -> bool {
        self.is_empty()
    }
}

/// Decomposition of a path into segments.
pub trait SegmentedPath<S: PathSegment + ?Sized>: Clone + Eq + Hash + fmt::Debug {
    /// Iterate over all path segments.
    ///
    /// All segments are guaranteed to be non-empty.
    // TODO: How to avoid boxing the result?
    #[must_use]
    fn segments(&self) -> Box<dyn Iterator<Item = &S> + '_>;

    /// Split the path into parent segments and the last child segment.
    ///
    /// The returned iterator excludes the last segment that is
    /// included by [`Self::segments()`].
    // TODO: How to avoid boxing the result?
    #[must_use]
    fn parent_child_segments(&self) -> (Box<dyn Iterator<Item = &S> + '_>, Option<&S>);
}

/// [`SegmentedPath`] with a root element.
pub trait RootPath<S: PathSegment + ?Sized>: SegmentedPath<S> {
    /// Check if the path equals the root path.
    #[must_use]
    fn is_root(&self) -> bool;
}