1use crate::{
2 types::{Enum, EnumOptions, TypeIdent, TypeMap, Variant, VariantAttrs},
3 Type,
4};
5use std::{
6 collections::{BTreeMap, BTreeSet, HashMap, HashSet},
7 rc::Rc,
8};
9
10#[cfg(feature = "bytes-compat")]
11mod bytes;
12#[cfg(feature = "http-compat")]
13mod http;
14#[cfg(feature = "rmpv-compat")]
15mod rmpv;
16#[cfg(feature = "serde-bytes-compat")]
17mod serde_bytes;
18#[cfg(feature = "serde-json-compat")]
19mod serde_json;
20#[cfg(feature = "time-compat")]
21mod time;
22
23pub trait Serializable: 'static {
24 fn ident() -> TypeIdent;
26
27 fn ty() -> Type;
29
30 fn is_primitive() -> bool {
32 false
33 }
34
35 fn collect_types(types: &mut TypeMap) {
40 types.entry(Self::ident()).or_insert_with(Self::ty);
41 }
42}
43
44impl<T> Serializable for Box<T>
45where
46 T: Serializable,
47{
48 fn ident() -> TypeIdent {
49 TypeIdent {
50 name: "Box".to_owned(),
51 generic_args: vec![(TypeIdent::from("T"), vec![])],
52 ..Default::default()
53 }
54 }
55
56 fn ty() -> Type {
57 Type::Container("Box".to_owned(), TypeIdent::from("T"))
58 }
59
60 fn collect_types(types: &mut TypeMap) {
61 types.entry(Self::ident()).or_insert_with(Self::ty);
62 T::collect_types(types);
63 }
64}
65
66impl<K, V> Serializable for BTreeMap<K, V>
67where
68 K: Serializable,
69 V: Serializable,
70{
71 fn ident() -> TypeIdent {
72 TypeIdent {
73 name: "BTreeMap".to_owned(),
74 generic_args: vec![
75 (TypeIdent::from("K"), vec![]),
76 (TypeIdent::from("V"), vec![]),
77 ],
78 ..Default::default()
79 }
80 }
81
82 fn ty() -> Type {
83 Type::Map(
84 "BTreeMap".to_owned(),
85 TypeIdent::from("K"),
86 TypeIdent::from("V"),
87 )
88 }
89
90 fn collect_types(types: &mut TypeMap) {
91 types.entry(Self::ident()).or_insert_with(Self::ty);
92 K::collect_types(types);
93 V::collect_types(types);
94 }
95}
96
97impl<T> Serializable for BTreeSet<T>
98where
99 T: Serializable,
100{
101 fn ident() -> TypeIdent {
102 TypeIdent {
103 name: "BTreeSet".to_owned(),
104 generic_args: vec![(TypeIdent::from("T"), vec![])],
105 ..Default::default()
106 }
107 }
108
109 fn ty() -> Type {
110 Type::List("BTreeSet".to_owned(), TypeIdent::from("T"))
111 }
112
113 fn collect_types(types: &mut TypeMap) {
114 types.entry(Self::ident()).or_insert_with(Self::ty);
115 T::collect_types(types);
116 }
117}
118
119impl<K, V> Serializable for HashMap<K, V>
120where
121 K: Serializable,
122 V: Serializable,
123{
124 fn ident() -> TypeIdent {
125 TypeIdent {
126 name: "HashMap".to_owned(),
127 generic_args: vec![
128 (TypeIdent::from("K"), vec![]),
129 (TypeIdent::from("V"), vec![]),
130 ],
131 ..Default::default()
132 }
133 }
134
135 fn ty() -> Type {
136 Type::Map(
137 "HashMap".to_owned(),
138 TypeIdent::from("K"),
139 TypeIdent::from("V"),
140 )
141 }
142
143 fn collect_types(types: &mut TypeMap) {
144 types.entry(Self::ident()).or_insert_with(Self::ty);
145 K::collect_types(types);
146 V::collect_types(types);
147 }
148}
149
150impl<T> Serializable for HashSet<T>
151where
152 T: Serializable,
153{
154 fn ident() -> TypeIdent {
155 TypeIdent {
156 name: "HashSet".to_owned(),
157 generic_args: vec![(TypeIdent::from("T"), vec![])],
158 ..Default::default()
159 }
160 }
161
162 fn ty() -> Type {
163 Type::List("HashSet".to_owned(), TypeIdent::from("T"))
164 }
165
166 fn collect_types(types: &mut TypeMap) {
167 types.entry(Self::ident()).or_insert_with(Self::ty);
168 T::collect_types(types);
169 }
170}
171
172impl<T> Serializable for Option<T>
173where
174 T: Serializable,
175{
176 fn ident() -> TypeIdent {
177 TypeIdent {
178 name: "Option".to_owned(),
179 generic_args: vec![(TypeIdent::from("T"), vec![])],
180 ..Default::default()
181 }
182 }
183
184 fn ty() -> Type {
185 Type::Container("Option".to_owned(), TypeIdent::from("T"))
186 }
187
188 fn collect_types(types: &mut TypeMap) {
189 types.entry(Self::ident()).or_insert_with(Self::ty);
190 T::collect_types(types);
191 }
192}
193
194impl<T> Serializable for Rc<T>
195where
196 T: Serializable,
197{
198 fn ident() -> TypeIdent {
199 TypeIdent {
200 name: "Rc".to_owned(),
201 generic_args: vec![(TypeIdent::from("T"), vec![])],
202 ..Default::default()
203 }
204 }
205
206 fn ty() -> Type {
207 Type::Container("Rc".to_owned(), TypeIdent::from("T"))
208 }
209
210 fn collect_types(types: &mut TypeMap) {
211 types.entry(Self::ident()).or_insert_with(Self::ty);
212 T::collect_types(types);
213 }
214}
215
216impl<T, E> Serializable for Result<T, E>
217where
218 T: Serializable,
219 E: Serializable,
220{
221 fn ident() -> TypeIdent {
222 TypeIdent {
223 name: "Result".to_owned(),
224 generic_args: vec![
225 (TypeIdent::from("T"), vec![]),
226 (TypeIdent::from("E"), vec![]),
227 ],
228 ..Default::default()
229 }
230 }
231
232 fn ty() -> Type {
233 Type::Enum(Enum {
234 ident: Self::ident(),
235 variants: vec![
236 Variant {
237 name: "Ok".to_owned(),
238 ty: Type::Tuple(vec![TypeIdent::from("T")]),
239 doc_lines: vec![" Represents a successful result.".to_owned()],
240 attrs: VariantAttrs::default(),
241 },
242 Variant {
243 name: "Err".to_owned(),
244 ty: Type::Tuple(vec![TypeIdent::from("E")]),
245 doc_lines: vec![" Represents an error.".to_owned()],
246 attrs: VariantAttrs::default(),
247 },
248 ],
249 doc_lines: vec![
250 " A result that can be either successful (`Ok`) or represent an error (`Err`)."
251 .to_owned(),
252 ],
253 options: EnumOptions::default(),
254 })
255 }
256
257 fn collect_types(types: &mut TypeMap) {
258 types.entry(Self::ident()).or_insert_with(Self::ty);
259 T::collect_types(types);
260 E::collect_types(types);
261 }
262}
263
264impl Serializable for () {
265 fn ident() -> TypeIdent {
266 TypeIdent::from("()")
267 }
268
269 fn ty() -> Type {
270 Type::Unit
271 }
272}
273
274impl Serializable for String {
275 fn ident() -> TypeIdent {
276 TypeIdent::from("String")
277 }
278
279 fn ty() -> Type {
280 Type::String
281 }
282}
283
284impl<T> Serializable for Vec<T>
285where
286 T: Serializable,
287{
288 fn ident() -> TypeIdent {
289 TypeIdent {
290 name: "Vec".to_owned(),
291 generic_args: vec![(TypeIdent::from("T"), vec![])],
292 ..Default::default()
293 }
294 }
295
296 fn ty() -> Type {
297 Type::List("Vec".to_owned(), TypeIdent::from("T"))
298 }
299
300 fn collect_types(types: &mut TypeMap) {
301 types.entry(Self::ident()).or_insert_with(Self::ty);
302 T::collect_types(types);
303 }
304}