Skip to main content

datex_core/values/value/
convert_parts.rs

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