use crate::traits::has_branches::{HasBranches, HasBranchesAPI};
use crate::{longer_mut, AddBranch, HasPathSegment};
pub trait HasGet {
fn get_impl<T>(self, segment: impl Into<String>) -> Option<T>
where Self: HasBranches<T> + Sized,
T: HasPathSegment
{
let segment = segment.into();
self.branches_impl2::<T>().find(|value| value.path_segment() == segment)
}
}
impl<T> HasGet for T {}
pub trait HasGetAPI<'a> {
fn get<T>(&'a self, segment: impl Into<String>) -> Option<T>
where &'a Self: HasGet + HasBranches<T>,
T: HasPathSegment + 'a
{
self.get_impl::<T>(segment)
}
fn get_mut<T>(&'a mut self, segment: impl Into<String>) -> Option<T>
where &'a mut Self: HasGet + HasBranches<T>,
T: HasPathSegment + 'a
{
self.get_impl::<T>(segment)
}
fn branch<T>(&'a mut self, segment: impl Into<String>) -> &'a mut T
where &'a mut Self: HasGet + HasBranches<&'a mut T>,
Self: AddBranch<T> + Sized,
T: HasPathSegment + 'a,
String: Into<T>
{
let segment = segment.into();
let self_ = unsafe { longer_mut(self) }; if let Some(value) = self.get_mut::<&mut T>(segment.clone()) {
value
} else {
self_.add_branch(segment.into())
}
}
}
impl<'a, T> HasGetAPI<'a> for T {}