Skip to main content

datex_core/traits/
value_access.rs

1use crate::{
2    prelude::*,
3    shared_values::errors::AccessError,
4    values::value_container::{
5        ValueContainer,
6        borrowed_value_container::{
7            BorrowedValueContainer, BorrowedValueContainerMut,
8        },
9    },
10};
11
12/// Trait for accessing properties of a value as [ValueContainers] or [BorrowedValueContainer]. This is used for accessing properties of values in a generic way, such as for maps and structs.
13pub trait ValueAccess {
14    /// Gets a reference to a property on the value if applicable (e.g. for map and structs).
15    /// This method returns a [BorrowedValueContainer] which can be either a [ValueContainer] or a [Callable] value.
16    fn try_get_property(
17        &self,
18        _key: &ValueContainer,
19    ) -> Result<BorrowedValueContainer<'_>, AccessError> {
20        Err(AccessError::InvalidOperation(
21            "Cannot get property".to_string(),
22        ))
23    }
24
25    /// Gets a mutable reference to a property on the value if applicable (e.g. for map and structs)
26    fn try_get_property_mut(
27        &mut self,
28        _key: &ValueContainer,
29    ) -> Result<BorrowedValueContainerMut<'_>, AccessError> {
30        Err(AccessError::InvalidOperation(
31            "Cannot get property".to_string(),
32        ))
33    }
34}