schemars_derive/ast/
from_serde.rs1use super::*;
2use serde_derive_internals::ast as serde_ast;
3use serde_derive_internals::Ctxt;
4
5pub trait FromSerde: Sized {
6 type SerdeType;
7
8 fn from_serde(errors: &Ctxt, serde: Self::SerdeType) -> Self;
9
10 fn vec_from_serde(errors: &Ctxt, serdes: Vec<Self::SerdeType>) -> Vec<Self> {
11 serdes
12 .into_iter()
13 .map(|s| Self::from_serde(errors, s))
14 .collect()
15 }
16}
17
18impl<'a> FromSerde for Container<'a> {
19 type SerdeType = serde_ast::Container<'a>;
20
21 fn from_serde(errors: &Ctxt, serde: Self::SerdeType) -> Self {
22 let data = Data::from_serde(errors, serde.data);
23 let attrs = ContainerAttrs::new(&serde.original.attrs, &data, errors);
24 let rename_type_params = match &attrs.rename_format_string {
25 Some(s) => crate::name::get_rename_format_type_params(errors, s, serde.generics),
26 None => BTreeSet::new(),
27 };
28
29 let mut cont = Self {
30 ident: serde.ident,
31 serde_attrs: serde.attrs,
32 data,
33 attrs,
34 rename_type_params,
35 relevant_type_params: BTreeSet::new(),
37 generics: serde.generics.clone(),
39 };
40
41 crate::bound::find_trait_bounds(serde.generics, &mut cont);
42
43 cont
44 }
45}
46
47impl<'a> FromSerde for Data<'a> {
48 type SerdeType = serde_ast::Data<'a>;
49
50 fn from_serde(errors: &Ctxt, serde: Self::SerdeType) -> Self {
51 match serde {
52 serde_ast::Data::Enum(variants) => {
53 Data::Enum(Variant::vec_from_serde(errors, variants))
54 }
55 serde_ast::Data::Struct(style, fields) => {
56 Data::Struct(style, Field::vec_from_serde(errors, fields))
57 }
58 }
59 }
60}
61
62impl<'a> FromSerde for Variant<'a> {
63 type SerdeType = serde_ast::Variant<'a>;
64
65 fn from_serde(errors: &Ctxt, serde: Self::SerdeType) -> Self {
66 Self {
67 ident: serde.ident,
68 serde_attrs: serde.attrs,
69 style: serde.style,
70 fields: Field::vec_from_serde(errors, serde.fields),
71 original: serde.original,
72 attrs: VariantAttrs::new(&serde.original.attrs, errors),
73 }
74 }
75}
76
77impl<'a> FromSerde for Field<'a> {
78 type SerdeType = serde_ast::Field<'a>;
79
80 fn from_serde(errors: &Ctxt, serde: Self::SerdeType) -> Self {
81 Self {
82 member: serde.member,
83 serde_attrs: serde.attrs,
84 ty: serde.ty,
85 original: serde.original,
86 attrs: FieldAttrs::new(&serde.original.attrs, errors),
87 }
88 }
89}