Skip to main content

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
69#[derive(PartialEq)]
70enum DeserializeComplexType {
71    Complex,
72    Resource,
73}
74
75#[proc_macro_derive(
76    FHIRJSONDeserialize,
77    attributes(
78        fhir_serialize_type,
79        rename_field,
80
81        // Used on the enum itself for typechoice.
82        type_choice_field_name,
83
84        // Used on field itself for variants.
85        type_choice_variants,
86
87        primitive,
88
89        // Used for enum serialization.
90        determine_by,
91
92        // For validation on vector min maxes.
93        cardinality,
94        reference
95    )
96)]
97pub fn deserialize(input: TokenStream) -> TokenStream {
98    let input = parse_macro_input!(input as DeriveInput);
99
100    let serialize_type = get_attribute_serialization_type(&input.attrs);
101
102    let result = match serialize_type.unwrap().as_str() {
103        "primitive" => deserialize::fhir_primitive_deserialization(input),
104        "typechoice" => deserialize_typechoice(input),
105        "resource" => deserialize_complex(input, DeserializeComplexType::Resource),
106        "complex" => deserialize_complex(input, DeserializeComplexType::Complex),
107        "enum-variant" => deserialize::enum_variant_deserialization(input),
108        "valueset" => deserialize::deserialize_valueset(input),
109        _ => panic!("Must be one of primitive, typechoice, complex or resource."),
110    };
111
112    result.into()
113}