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