Skip to main content

datex_core/values/value_container/
convert_parts.rs

1use crate::{
2    prelude::*,
3    traits::convert_parts::{FromParts, HasPartsKind, IntoParts, PartsKind},
4    values::{
5        core_values::{list::List, map::Map},
6        value::Value,
7        value_container::ValueContainer,
8    },
9};
10
11impl HasPartsKind for ValueContainer {
12    fn parts_kind(&self) -> PartsKind {
13        match self {
14            ValueContainer::Local(value) => value.parts_kind(),
15            ValueContainer::Shared(_shared) => PartsKind::None,
16        }
17    }
18}
19
20impl IntoParts for ValueContainer {
21    fn try_into_map_parts<'a>(self: Box<Self>) -> Result<Map, ()>
22    where
23        Self: 'a,
24    {
25        match self {
26            ValueContainer::Local(value) => {
27                Box::new(value).try_into_map_parts()
28            }
29            ValueContainer::Shared(_shared) => Err(()),
30        }
31    }
32
33    fn try_into_list_parts<'a>(self: Box<Self>) -> Result<List, ()>
34    where
35        Self: 'a,
36    {
37        match self {
38            ValueContainer::Local(value) => {
39                Box::new(value).try_into_list_parts()
40            }
41            ValueContainer::Shared(_shared) => Err(()),
42        }
43    }
44
45    fn try_into_single_value<'a>(self: Box<Self>) -> Result<ValueContainer, ()>
46    where
47        Self: 'a,
48    {
49        Ok(*self)
50    }
51}
52
53impl FromParts for ValueContainer {
54    fn try_from_map_parts_with_tag(
55        parts: Map,
56        tag: Option<&str>,
57    ) -> Result<Self, ()>
58    where
59        Self: Sized,
60    {
61        Ok(ValueContainer::Local(Value::try_from_map_parts_with_tag(
62            parts, tag,
63        )?))
64    }
65    fn try_from_list_parts_with_tag(
66        parts: List,
67        tag: Option<&str>,
68    ) -> Result<Self, ()>
69    where
70        Self: Sized,
71    {
72        Ok(ValueContainer::Local(Value::try_from_list_parts_with_tag(
73            parts, tag,
74        )?))
75    }
76
77    fn try_from_single_value_with_tag(
78        value: ValueContainer,
79        _tag: Option<&str>,
80    ) -> Result<Self, ()>
81    where
82        Self: Sized,
83    {
84        Ok(value)
85    }
86}