Skip to main content

apache_avro_derive/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#![cfg_attr(nightly, feature(proc_macro_diagnostic))]
19
20//! This crate is the implementation of the `AvroSchema` derive macro.
21//! Please use it via the [`apache-avro`](https://crates.io/crates/apache-avro) crate:
22//!
23//! ```no_run
24//! use apache_avro::AvroSchema;
25//!
26//! #[derive(AvroSchema)]
27//! struct MyStruct;
28//! ```
29//! Please see the documentation of the [`AvroSchema`] trait for instructions on how to use it.
30//!
31//! [`AvroSchema`]: https://docs.rs/apache-avro/latest/apache_avro/serde/trait.AvroSchema.html
32
33mod attributes;
34mod case;
35mod enums;
36mod fields;
37mod implementation;
38mod structs;
39mod utils;
40
41use syn::{DeriveInput, parse_macro_input, spanned::Spanned};
42
43use crate::attributes::NamedTypeOptions;
44use crate::implementation::Implementation;
45use crate::utils::to_compile_errors;
46
47#[proc_macro_derive(AvroSchema, attributes(avro, serde))]
48// Templated from Serde
49pub fn proc_macro_derive_avro_schema(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
50    let input = parse_macro_input!(input as DeriveInput);
51    derive_avro_schema(input)
52        .map(Implementation::into_token_stream)
53        .unwrap_or_else(to_compile_errors)
54        .into()
55}
56
57fn derive_avro_schema(input: DeriveInput) -> Result<Implementation, Vec<syn::Error>> {
58    // It would be nice to parse the attributes before the `match`, but we first need to validate that `input` is not a union.
59    // Otherwise a user could get errors related to the attributes and after fixing those get an error because the attributes were on a union.
60    let input_span = input.span();
61    match input.data {
62        syn::Data::Struct(data_struct) => {
63            let named_type_options = NamedTypeOptions::new(&input.ident, &input.attrs, input_span)?;
64            structs::to_implementation(
65                input_span,
66                input.ident,
67                input.generics,
68                named_type_options,
69                data_struct,
70            )
71        }
72        syn::Data::Enum(data_enum) => {
73            let named_type_options = NamedTypeOptions::new(&input.ident, &input.attrs, input_span)?;
74            enums::to_implementation(
75                input_span,
76                input.ident,
77                input.generics,
78                named_type_options,
79                data_enum,
80            )
81        }
82        syn::Data::Union(_) => Err(vec![syn::Error::new(
83            input_span,
84            "AvroSchema: derive only works for structs and simple enums",
85        )]),
86    }
87}