Skip to main content

datex_core/traits/
local_child_path_resolver.rs

1use crate::{
2    value_updates::errors::UpdateError,
3    values::{value::Value, value_container::ValueContainer},
4};
5
6/// Trait that can be implemented on core values that allows the resolution
7/// of child paths for nested local values.
8pub trait LocalChildPathResolver {
9    fn resolve_child(
10        &mut self,
11        _key: &ValueContainer,
12    ) -> Result<&mut ValueContainer, UpdateError> {
13        Err(UpdateError::InvalidUpdate)
14    }
15
16    fn resolve_value_for_path(
17        &mut self,
18        first: &ValueContainer,
19        remaining_path: &[ValueContainer],
20    ) -> Result<&mut Value, UpdateError> {
21        let child = self.resolve_child(first)?;
22
23        if let ValueContainer::Local(child) = child {
24            if let Some(([first], remaining_path)) =
25                remaining_path.split_at_checked(1)
26            {
27                child.resolve_value_for_path(first, remaining_path)
28            } else {
29                Ok(child)
30            }
31        } else {
32            Err(UpdateError::InvalidUpdate)
33        }
34    }
35}