is_tree/traits/
has_relative.rs

1//! Traits for relative path traversal.
2
3use crate::{HasPathSegment, IsPathSegment, KnowsVisitor, PathSegment, UnsafeClone, HasParentMut, HasRootMut};
4
5use super::{HasGet, HasParent, HasRoot};
6use super::has_branches::HasBranches;
7
8/// A trait for objects that have a relative path.
9pub trait HasRelative<'a> {
10    /// Gets a relative path.
11    /// "self", "root", and "super" are reserved path segments.
12    /// "self" is the current object, "root" is the root object, and "super" is the parent object.
13    fn relative<K>(&'a self, path: impl IntoIterator<Item = K>) -> Option<Self>
14    where K: Into<String>,
15        Self: KnowsVisitor<Visitor = Self> + Clone + HasRoot + HasParent + HasPathSegment,
16        &'a Self: HasBranches<Self>
17    {
18        let mut path = path.into_iter();
19        if let Some(segment) = path.next() {
20            let segment = segment.into();
21            let visitor: Self = match segment.kind() {
22                PathSegment::Self_ => self.clone(),
23                PathSegment::Root => self.root(),
24                PathSegment::Super => self.parent()?,
25                PathSegment::Other(_) => self.get_impl::<Self>(segment)?
26            };
27            unsafe { crate::unsafe_::longer_ref(&visitor) }.relative(path)
28        } else {
29            Some(self.clone())
30        }
31    }
32}
33
34/// A trait for objects that have a relative path mutably.
35/// By design, accessing a Visitor parent is unsafe.
36pub trait HasRelativeMut<'a> {
37    /// Gets a relative path mutably.
38    /// "self", "root", and "super" are reserved path segments.
39    /// "self" is the current object, "root" is the root object, and "super" is the parent object.
40    unsafe fn relative_mut<K>(&'a mut self, path: impl IntoIterator<Item = K>) -> Option<Self>
41    where K: Into<String>,
42        Self: KnowsVisitor<VisitorMut = Self> + UnsafeClone + HasRootMut + HasParentMut + HasPathSegment + Sized,
43        &'a mut Self: HasBranches<Self>
44    {
45        let mut path = path.into_iter();
46        if let Some(segment) = path.next() {
47            let segment = segment.into();
48            let mut visitor: Self = match segment.kind() {
49                PathSegment::Self_ => self.unsafe_clone(),
50                PathSegment::Root => self.root_mut(),
51                PathSegment::Super => self.parent_mut()?,
52                PathSegment::Other(_) => self.get_impl::<Self>(segment)?
53            };
54            crate::unsafe_::longer_mut(&mut visitor).relative_mut(path)
55        } else {
56            Some(self.unsafe_clone())
57        }
58    }
59}
60
61impl<'a, T: Sized> HasRelative<'a> for T {}
62impl<'a, T: Sized> HasRelativeMut<'a> for T {}