cotis_modules_debug/serialize_targets/
leaf_serialization.rs1use std::marker::PhantomData;
35
36use cotis::element_configuring::{
37 ConfigureElements, ConfigureLeafElements, OpenElement, OpenLeafElement,
38};
39use serde::de::DeserializeOwned;
40use serde::{Deserialize, Serialize};
41
42#[derive(Clone, Copy, Debug)]
44pub struct LeafConfigListNil;
45
46#[derive(Debug)]
52pub struct LeafConfigList<Head, Tail>(PhantomData<(Head, Tail)>);
53
54impl<Head, Tail> Clone for LeafConfigList<Head, Tail> {
55 fn clone(&self) -> Self {
56 *self
57 }
58}
59
60impl<Head, Tail> Copy for LeafConfigList<Head, Tail> {}
61
62impl<Head, Tail> LeafConfigList<Head, Tail> {
63 pub fn new() -> LeafConfigList<Head, LeafConfigListNil> {
65 LeafConfigList(PhantomData)
66 }
67
68 pub fn extend<Head2, Tail2>() -> LeafConfigList<Head, LeafConfigList<Head2, Tail2>> {
70 LeafConfigList(PhantomData)
71 }
72
73 pub fn new_long() -> LeafConfigList<Head, Tail> {
75 LeafConfigList(PhantomData)
76 }
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct LeafConfigData {
84 internal: Vec<u8>,
85}
86
87impl LeafConfigData {
88 pub(crate) fn new<Head: Serialize>(config: &Head) -> LeafConfigData {
89 Self {
90 internal: serde_json::to_vec(&config).unwrap(),
91 }
92 }
93
94 fn try_reconstruct<Head: DeserializeOwned>(&self) -> Option<Head> {
95 serde_json::from_slice(&self.internal).ok()
96 }
97}
98
99pub trait LeafConfigListCompatible<Configurer, ConfigType>
104where
105 Configurer: ConfigureElements<ConfigType>,
106{
107 fn configure(
109 data: &LeafConfigData,
110 configurer: &mut OpenLeafElement<'_, Configurer, ConfigType>,
111 );
112
113 fn configure_open(data: &LeafConfigData, configurer: OpenElement<'_, Configurer, ConfigType>);
115}
116
117impl<Head, Tail, Configurer, ConfigType> LeafConfigListCompatible<Configurer, ConfigType>
118 for LeafConfigList<Head, Tail>
119where
120 Configurer: ConfigureLeafElements<Head> + ConfigureElements<ConfigType>,
121 Tail: LeafConfigListCompatible<Configurer, ConfigType>,
122 Head: DeserializeOwned,
123{
124 fn configure(
125 data: &LeafConfigData,
126 configurer: &mut OpenLeafElement<'_, Configurer, ConfigType>,
127 ) {
128 let reconstruction: Option<Head> = data.try_reconstruct();
129 match reconstruction {
130 Some(reconstruction) => {
131 configurer.set_leaf_config(reconstruction);
132 }
133 None => {
134 Tail::configure(data, configurer);
135 }
136 }
137 }
138
139 fn configure_open(data: &LeafConfigData, configurer: OpenElement<'_, Configurer, ConfigType>) {
140 let reconstruction: Option<Head> = data.try_reconstruct();
141 match reconstruction {
142 Some(reconstruction) => {
143 configurer.set_leaf_config(reconstruction);
144 }
145 None => {
146 Tail::configure_open(data, configurer);
147 }
148 }
149 }
150}
151
152impl<Configurer, ConfigType> LeafConfigListCompatible<Configurer, ConfigType> for LeafConfigListNil
153where
154 Configurer: ConfigureElements<ConfigType>,
155{
156 fn configure(
157 _data: &LeafConfigData,
158 _configurer: &mut OpenLeafElement<'_, Configurer, ConfigType>,
159 ) {
160 }
161
162 fn configure_open(_data: &LeafConfigData, configurer: OpenElement<'_, Configurer, ConfigType>) {
163 drop(configurer);
164 }
165}