haste_fhir_serialization_json_derive/
lib.rs

1mod deserialize;
2mod serialize;
3mod utilities;
4
5use proc_macro::TokenStream;
6use syn::{Attribute, DeriveInput, Expr, Lit, Meta, parse_macro_input};
7
8use crate::deserialize::{deserialize_complex, deserialize_typechoice};
9
10/// Determines the de/serialization type of the derive macro.
11fn get_attribute_serialization_type(attrs: &[Attribute]) -> Option<String> {
12    attrs.iter().find_map(|attr| match &attr.meta {
13        Meta::NameValue(name_value) => {
14            if name_value.path.is_ident("fhir_serialize_type") {
15                match &name_value.value {
16                    Expr::Lit(lit) => match &lit.lit {
17                        Lit::Str(lit) => Some(lit.value()),
18                        _ => panic!("Expected a string literal"),
19                    },
20                    _ => panic!("Expected a string literal"),
21                }
22            } else {
23                None
24            }
25        }
26        _ => None,
27    })
28}
29
30#[proc_macro_derive(
31    FHIRJSONSerialize,
32    attributes(
33        fhir_serialize_type,
34        rename_field,
35        // Used on the enum itself for typechoice.
36        type_choice_field_name,
37         // Used on field itself for variants.
38        type_choice_variants,
39        primitive,
40        code,
41        // For validation on vector min maxes.
42        cardinality,
43        reference
44    )
45)]
46pub fn serialize(input: TokenStream) -> TokenStream {
47    let input = parse_macro_input!(input as DeriveInput);
48
49    let serialize_type = get_attribute_serialization_type(&input.attrs);
50
51    let result = match serialize_type.unwrap().as_str() {
52        "primitive" => serialize::primitve_serialization(input),
53        "typechoice" => serialize::typechoice_serialization(input),
54        "complex" => {
55            serialize::complex_serialization(input, serialize::ComplexSerializeType::Complex)
56        }
57        "resource" => {
58            serialize::complex_serialization(input, serialize::ComplexSerializeType::Resource)
59        }
60        "valueset" => serialize::value_set_serialization(input),
61        "enum-variant" => serialize::enum_variant_serialization(input),
62        // Some("typechoice") => typechoice_serialization(input),
63        _ => panic!("Must be one of primitive, typechoice, complex or resource."),
64    };
65
66    result
67}
68
69enum DeserializeComplexType {
70    Complex,
71    Resource,
72}
73
74#[proc_macro_derive(
75    FHIRJSONDeserialize,
76    attributes(
77        fhir_serialize_type,
78        rename_field,
79
80        // Used on the enum itself for typechoice.
81        type_choice_field_name,
82
83        // Used on field itself for variants.
84        type_choice_variants,
85
86        primitive,
87
88        // Used for enum serialization.
89        determine_by,
90
91        // For validation on vector min maxes.
92        cardinality,
93        reference
94    )
95)]
96pub fn deserialize(input: TokenStream) -> TokenStream {
97    let input = parse_macro_input!(input as DeriveInput);
98
99    let serialize_type = get_attribute_serialization_type(&input.attrs);
100
101    let result = match serialize_type.unwrap().as_str() {
102        "primitive" => deserialize::primitive_deserialization(input),
103        "typechoice" => deserialize_typechoice(input),
104        "resource" => deserialize_complex(input, DeserializeComplexType::Resource),
105        "complex" => deserialize_complex(input, DeserializeComplexType::Complex),
106        "enum-variant" => deserialize::enum_variant_deserialization(input),
107        "valueset" => deserialize::deserialize_valueset(input),
108        _ => panic!("Must be one of primitive, typechoice, complex or resource."),
109    };
110
111    result.into()
112}