Skip to main content

datex_core/values/value/
convert_value.rs

1use crate::{
2    traits::convert_value_container::ConvertValueContainer,
3    values::{
4        value::{
5            Value,
6            borrowed_value::{BorrowedValue, BorrowedValueMut},
7        },
8        value_container::{
9            ValueContainer,
10            borrowed_value_container::{
11                BorrowedValueContainer, BorrowedValueContainerMut,
12            },
13        },
14    },
15};
16
17impl ConvertValueContainer for Value {
18    fn to_value_container(self) -> ValueContainer {
19        ValueContainer::Local(self)
20    }
21
22    fn as_borrowed_value_container(&self) -> BorrowedValueContainer<'_> {
23        BorrowedValueContainer::Local(BorrowedValue::from(self))
24    }
25    fn as_borrowed_value_container_mut(
26        &mut self,
27    ) -> BorrowedValueContainerMut<'_> {
28        BorrowedValueContainerMut::Local(
29            // FIXME, shall we bubble up the error of static string slices really to that level?
30            BorrowedValueMut::try_from(self)
31                .expect("Failed to convert to BorrowedValueMut"),
32        )
33    }
34
35    fn try_from_value_container(
36        value_container: ValueContainer,
37    ) -> Result<Self, ValueContainer>
38    where
39        Self: Sized,
40    {
41        match value_container {
42            ValueContainer::Local(value) => Ok(value),
43            _ => Err(value_container),
44        }
45    }
46
47    fn try_borrow_from_value_container(
48        value_container: &ValueContainer,
49    ) -> Result<&Self, ()>
50    where
51        Self: Sized,
52    {
53        match value_container {
54            ValueContainer::Local(value) => Ok(value),
55            _ => Err(()),
56        }
57    }
58
59    fn try_borrow_mut_from_value_container(
60        value_container: &mut ValueContainer,
61    ) -> Result<&mut Self, ()>
62    where
63        Self: Sized,
64    {
65        match value_container {
66            ValueContainer::Local(value) => Ok(value),
67            _ => Err(()),
68        }
69    }
70}