Skip to main content

datex_core/traits/
convert_parts.rs

1use crate::{
2    prelude::*,
3    values::{
4        core_values::{list::List, map::Map},
5        value_container::ValueContainer,
6    },
7};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10/// Represents the kind of parts that a value can be converted into.
11pub enum PartsKind {
12    /// Represents a list value (a struct without named fields).
13    List,
14    /// Represents a map value (a struct with named fields).
15    Map,
16    /// Represents any single value
17    SingleValue,
18    /// Represents no parts.
19    None,
20}
21
22/// A trait for types that can be constructed from parts.
23pub trait FromParts: HasPartsKind {
24    /// Tries to construct the implementing type from the given map parts.
25    fn try_from_map_parts(_parts: Map) -> Result<Self, ()>
26    where
27        Self: Sized,
28    {
29        Self::try_from_map_parts_with_tag(_parts, None)
30    }
31
32    /// Tries to construct the implementing type from the given list parts.
33    fn try_from_list_parts(_parts: List) -> Result<Self, ()>
34    where
35        Self: Sized,
36    {
37        Self::try_from_list_parts_with_tag(_parts, None)
38    }
39
40    /// Tries to construct the implementing type from a single value.
41    fn try_from_single_value(_value: ValueContainer) -> Result<Self, ()>
42    where
43        Self: Sized,
44    {
45        Err(())
46    }
47
48    fn try_from_map_parts_with_tag(
49        _parts: Map,
50        _tag: Option<&str>,
51    ) -> Result<Self, ()>
52    where
53        Self: Sized,
54    {
55        Err(())
56    }
57
58    fn try_from_list_parts_with_tag(
59        _parts: List,
60        _tag: Option<&str>,
61    ) -> Result<Self, ()>
62    where
63        Self: Sized,
64    {
65        Err(())
66    }
67
68    fn try_from_single_value_with_tag(
69        _value: ValueContainer,
70        _tag: Option<&str>,
71    ) -> Result<Self, ()>
72    where
73        Self: Sized,
74    {
75        Err(())
76    }
77}
78
79/// Trait for types that have a specific kind of parts.
80pub trait HasPartsKind {
81    /// Returns the kind of parts that the implementing type can be converted into.
82    /// This can be used to check if the conversion is possible before attempting it.
83    /// If this returns `PartsKind::None`, then the conversion is not possible.
84    /// If this returns `PartsKind::List`, then the conversion is possible and `try_into_list_parts()` can be called.
85    /// If this returns `PartsKind::Map`, then the conversion is possible and `try_into_map_parts()` can be called.
86    /// If this returns `PartsKind::SingleValue`, then the conversion is possible and `try_from_single_value()` can be called.
87    fn parts_kind(&self) -> PartsKind {
88        PartsKind::None
89    }
90}
91
92/// A trait for types that can be converted into parts.
93pub trait IntoParts: HasPartsKind {
94    /// Converts the implementing type into its map parts.
95    /// Returns an error if the conversion is not possible.
96    /// You can check if the conversion is possible by calling `parts_kind()`
97    /// and checking if it returns `PartsKind::Map`.
98    fn try_into_map_parts<'a>(self: Box<Self>) -> Result<Map, ()>
99    where
100        Self: 'a,
101    {
102        Err(())
103    }
104
105    /// Converts the implementing type into its list parts.
106    /// Returns an error if the conversion is not possible.
107    /// You can check if the conversion is possible by calling `parts_kind()`
108    /// and checking if it returns `PartsKind::List`.
109    fn try_into_list_parts<'a>(self: Box<Self>) -> Result<List, ()>
110    where
111        Self: 'a,
112    {
113        Err(())
114    }
115
116    /// Converts the implementing type into a single value.
117    /// Returns an error if the conversion is not possible.
118    /// You can check if the conversion is possible by calling `parts_kind()`
119    /// and checking if it returns `PartsKind::SingleValue`.
120    fn try_into_single_value<'a>(self: Box<Self>) -> Result<ValueContainer, ()>
121    where
122        Self: 'a,
123    {
124        Err(())
125    }
126}