is_tree/traits/
has_get.rs

1//! This module contains the `HasGet` trait and its API trait `HasGetAPI`.
2
3use crate::traits::has_branches::{HasBranches, HasBranchesAPI};
4
5use crate::HasPathSegment;
6
7/// This is the trait one should implement to provide a way to get branches by their path segments.
8pub trait HasGet {
9    /// Gets a branch by its path segment.
10    /// It's discouraged to use this method directly. Instead, use the `get` and `get_mut` method from the `HasGetAPI` trait.
11    fn get_impl<T>(self, segment: impl Into<String>) -> Option<T>
12    where Self: HasBranches<T> + Sized,
13          T: HasPathSegment
14    {
15        let segment = segment.into();
16        self.branches_impl2::<T>().find(|value| value.path_segment() == segment)
17    }
18}
19
20impl<T> HasGet for T {}
21
22/// This is an API trait for getting branches by their path segments.
23pub trait HasGetAPI<'a> {
24    /// Gets a branch by its path segment.
25    fn get<T>(&'a self, segment: impl Into<String>) -> Option<T>
26    where &'a Self: HasGet + HasBranches<T>,
27          T: HasPathSegment + 'a
28    {
29        self.get_impl::<T>(segment)
30    }
31}
32
33impl<'a, T> HasGetAPI<'a> for T {}
34
35pub trait HasGetMut<'a> {
36    /// Gets a branch by its path segment mutably.
37    fn get_mut<T>(&'a mut self, segment: impl Into<String>) -> Option<T>
38    where &'a mut Self: HasGet + HasBranches<T>,
39            T: HasPathSegment + 'a;
40}