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//! ```
28//! Please see the documentation of the [`AvroSchema`] trait for instructions on how to use it.
29//!
30//! [`AvroSchema`]: https://docs.rs/apache-avro/latest/apache_avro/serde/trait.AvroSchema.html
31
32mod attributes;
33mod case;
34mod enums;
35mod fields;
36mod implementation;
37mod structs;
38mod utils;
39
40use syn::{DeriveInput, parse_macro_input, spanned::Spanned};
41
42use crate::attributes::NamedTypeOptions;
43use crate::implementation::Implementation;
44use crate::utils::to_compile_errors;
45
46#[proc_macro_derive(AvroSchema, attributes(avro, serde))]
47// Templated from Serde
48pub fn proc_macro_derive_avro_schema(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
49 let input = parse_macro_input!(input as DeriveInput);
50 derive_avro_schema(input)
51 .map(Implementation::into_token_stream)
52 .unwrap_or_else(to_compile_errors)
53 .into()
54}
55
56fn derive_avro_schema(input: DeriveInput) -> Result<Implementation, Vec<syn::Error>> {
57 // It would be nice to parse the attributes before the `match`, but we first need to validate that `input` is not a union.
58 // Otherwise a user could get errors related to the attributes and after fixing those get an error because the attributes were on a union.
59 let input_span = input.span();
60 match input.data {
61 syn::Data::Struct(data_struct) => {
62 let named_type_options = NamedTypeOptions::new(&input.ident, &input.attrs, input_span)?;
63 structs::to_implementation(
64 input_span,
65 input.ident,
66 input.generics,
67 named_type_options,
68 data_struct,
69 )
70 }
71 syn::Data::Enum(data_enum) => {
72 let named_type_options = NamedTypeOptions::new(&input.ident, &input.attrs, input_span)?;
73 enums::to_implementation(
74 input_span,
75 input.ident,
76 input.generics,
77 named_type_options,
78 data_enum,
79 )
80 }
81 syn::Data::Union(_) => Err(vec![syn::Error::new(
82 input_span,
83 "AvroSchema: derive only works for structs and simple enums",
84 )]),
85 }
86}