apache-avro-derive 0.22.0

A library for deriving Avro schemata from Rust structs and enums
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::case::RenameRule;
use darling::{FromAttributes, FromMeta};
use proc_macro2::Span;
use serde_json::Value;
use syn::{AttrStyle, Attribute, Expr, Ident, Path, spanned::Spanned};

mod avro;
mod serde;

/// What `Schema` representation to generate for a type.
#[derive(Debug, PartialEq)]
pub enum Repr {
    /// Generate a `Schema::Enum` for a `enum`.
    ///
    /// Only works for unit variants.
    Enum,
    /// Generate a `Schema::Union` for a `enum`.
    ///
    /// There can only be one unit variant and every newtype/struct/tuple variant must be unique.
    BareUnion { untagged: bool },
    /// Generate a `Schema::Union` with a `Schema::Record`  for a `enum`.
    ///
    /// This works for every enum as the records will have unique names for every variant.
    UnionOfRecords,
    /// Generate a `Schema::Record` with a tag and content field for a `enum`.
    ///
    /// Requires `#[serde(tag = "...", content = "...")]`.
    RecordTagContent { tag: String, content: String },
    /// Generate a `Schema::Record` with a tag field and flattened variant fields for a `enum`.
    ///
    /// Requires `#[serde(tag = "...")]`
    RecordInternallyTagged { tag: String },
}

impl Repr {
    fn from_avro_and_serde(
        avro: Option<avro::Repr>,
        tag: Option<String>,
        content: Option<String>,
        untagged: bool,
        span: Span,
    ) -> Result<Option<Self>, syn::Error> {
        match avro {
            Some(avro::Repr::Enum) => {
                if tag.is_some() || content.is_some() || untagged {
                    Err(syn::Error::new(
                        span,
                        r#"AvroSchema: `#[avro(repr = "enum")]` is incompatible with `#[serde(tag = "..")]`, `#[serde(content = "..")]`, and `#[serde(untagged)]`"#,
                    ))
                } else {
                    Ok(Some(Self::Enum))
                }
            }
            Some(avro::Repr::BareUnion) => {
                if tag.is_some() || content.is_some() {
                    Err(syn::Error::new(
                        span,
                        r#"AvroSchema: `#[avro(repr = "bare_union")]` is incompatible with `#[serde(tag = "..")]` and `#[serde(content = "..")]`"#,
                    ))
                } else {
                    Ok(Some(Self::BareUnion { untagged }))
                }
            }
            Some(avro::Repr::UnionOfRecords) => {
                if tag.is_some() || content.is_some() || untagged {
                    Err(syn::Error::new(
                        span,
                        r#"AvroSchema: `#[avro(repr = "union_of_records")]` is incompatible with `#[serde(tag = "..")]`, `#[serde(content = "..")]`, and `#[serde(untagged)]`"#,
                    ))
                } else {
                    Ok(Some(Self::UnionOfRecords))
                }
            }
            Some(avro::Repr::RecordTagContent) => {
                if let Some(tag) = tag
                    && let Some(content) = content
                    && !untagged
                {
                    Ok(Some(Self::RecordTagContent { tag, content }))
                } else {
                    Err(syn::Error::new(
                        span,
                        r#"AvroSchema: `#[avro(repr = "record_tag_content")]` requires `#[serde(tag = "..", content = "..")]` and is incompatible with `#[serde(untagged)]`"#,
                    ))
                }
            }
            Some(avro::Repr::RecordInternallyTagged) => {
                if let Some(tag) = tag
                    && content.is_none()
                    && !untagged
                {
                    Ok(Some(Self::RecordInternallyTagged { tag }))
                } else {
                    Err(syn::Error::new(
                        span,
                        r#"AvroSchema: `#[avro(repr = "record_internally_tagged")]` requires `#[serde(tag = "..")]` and is incompatible with `#[serde(content = "..")]` and `#[serde(untagged)]`"#,
                    ))
                }
            }
            None => match (tag, content, untagged) {
                (Some(tag), Some(content), false) => {
                    Ok(Some(Self::RecordTagContent { tag, content }))
                }
                (Some(tag), None, false) => Ok(Some(Self::RecordInternallyTagged { tag })),
                (None, None, true) => Ok(Some(Self::BareUnion { untagged: true })),
                (None, None, false) => Ok(None),
                _ => Err(syn::Error::new(
                    span,
                    "AvroSchema: incompatible Serde tagging attributes",
                )),
            },
        }
    }
}

#[derive(Default)]
pub struct NamedTypeOptions {
    pub name: String,
    pub doc: Option<String>,
    pub aliases: Vec<String>,
    pub rename_all: RenameRule,
    pub rename_all_fields: RenameRule,
    pub transparent: bool,
    pub default: Option<Value>,
    pub repr: Option<Repr>,
}

impl NamedTypeOptions {
    pub fn new(
        ident: &Ident,
        attributes: &[Attribute],
        span: Span,
    ) -> Result<Self, Vec<syn::Error>> {
        let avro =
            avro::ContainerAttributes::from_attributes(attributes).map_err(darling_to_syn)?;
        let serde =
            serde::ContainerAttributes::from_attributes(attributes).map_err(darling_to_syn)?;

        // Check for deprecated attributes
        avro.deprecated(span);

        // Collect errors so user gets all feedback at once
        let mut errors = Vec::new();

        // Check for any Serde attributes that are hard errors
        if serde.variant_identifier || serde.field_identifier {
            errors.push(syn::Error::new(
                span,
                "AvroSchema: `#[serde(variant_identifier)]` and `#[serde(field_identifier)]` are not supported",
            ));
        }
        if serde.rename_all.deserialize != serde.rename_all.serialize {
            errors.push(syn::Error::new(
                span,
                r#"AvroSchema: rename rules for serializing and deserializing must match (`rename_all(serialize = "..", deserialize = "..")`)"#
            ));
        }
        if serde.rename_all_fields.deserialize != serde.rename_all_fields.serialize {
            errors.push(syn::Error::new(
                span,
                r#"AvroSchema: rename rules for serializing and deserializing must match (`rename_all_fields(serialize = "..", deserialize = "..")`)"#
            ));
        }

        // Check for conflicts between Serde and Avro
        if avro.name.is_some() && avro.name != serde.rename {
            errors.push(syn::Error::new(
                span,
                r#"AvroSchema: #[avro(name = "..")] must match #[serde(rename = "..")] and it's deprecated. Please use only `#[serde(rename = "..")]`"#,
            ));
        }
        if avro.rename_all != RenameRule::None && serde.rename_all.serialize != avro.rename_all {
            errors.push(syn::Error::new(
                span,
                r#"AvroSchema: #[avro(rename_all = "..")] must match #[serde(rename_all = "..")] and it's deprecated. Please use only `#[serde(rename_all = "..")]`"#,
            ));
        }
        if serde.transparent
            && (serde.rename.is_some()
                || avro.name.is_some()
                || avro.namespace.is_some()
                || avro.doc.is_some()
                || avro.default.is_some()
                || !avro.alias.is_empty()
                || avro.repr.is_some()
                || avro.rename_all != RenameRule::None
                || serde.rename_all.serialize != RenameRule::None
                || serde.rename_all.deserialize != RenameRule::None
                || serde.rename_all_fields.serialize != RenameRule::None
                || serde.rename_all_fields.deserialize != RenameRule::None
                || serde.untagged
                || serde.tag.is_some()
                || serde.content.is_some())
        {
            errors.push(syn::Error::new(
                span,
                "AvroSchema: #[serde(transparent)] is incompatible with all other attributes",
            ));
        }

        let repr = match Repr::from_avro_and_serde(
            avro.repr,
            serde.tag,
            serde.content,
            serde.untagged,
            span,
        ) {
            Ok(repr) => repr,
            Err(err) => {
                errors.push(err);
                None
            }
        };

        let default = if let Some(default_value) = avro.default {
            match serde_json::from_str(default_value.as_str()) {
                Ok(value) => Some(value),
                Err(err) => {
                    errors.push(syn::Error::new(
                        ident.span(),
                        format!("Invalid Avro `default` JSON: \n{err}"),
                    ));
                    None
                }
            }
        } else {
            None
        };

        if !errors.is_empty() {
            return Err(errors);
        }

        let name = serde.rename.unwrap_or(ident.to_string());
        let full_schema_name = vec![avro.namespace, Some(name)]
            .into_iter()
            .flatten()
            .collect::<Vec<String>>()
            .join(".");

        let doc = avro.doc.or_else(|| extract_rustdoc(attributes));

        Ok(Self {
            name: full_schema_name,
            doc,
            aliases: avro.alias,
            rename_all: serde.rename_all.serialize,
            rename_all_fields: serde.rename_all_fields.serialize,
            transparent: serde.transparent,
            default,
            repr,
        })
    }
}

/// How to get the schema for this field or variant.
#[derive(Debug, PartialEq, Default, Clone)]
pub enum With {
    /// Use `<T as AvroSchemaComponent>::get_schema_in_ctxt` for fields and enum specific schemas for variants.
    #[default]
    Trait,
    /// Use `module::get_schema_in_ctxt` where the module is defined by Serde's `with` attribute.
    Serde(Path),
    /// Call the function in this expression.
    Expr(Expr),
}

impl With {
    fn from_avro_and_serde(
        avro: &avro::With,
        serde: Option<&String>,
        span: Span,
    ) -> Result<Self, syn::Error> {
        match &avro {
            avro::With::Trait => Ok(Self::Trait),
            avro::With::Serde => {
                if let Some(serde) = serde {
                    let path = Path::from_string(serde).map_err(|err| {
                        syn::Error::new(
                            span,
                            format!(
                                r#"AvroSchema: Expected a path for `#[serde(with = "..")]`: {err:?}"#
                            ),
                        )
                    })?;
                    Ok(Self::Serde(path))
                } else {
                    Err(syn::Error::new(
                        span,
                        r#"`#[avro(with)]` requires `#[serde(with = "some_module")]` or provide a function to call `#[avro(with = some_fn)]`"#,
                    ))
                }
            }
            avro::With::Expr(expr) => Ok(Self::Expr(expr.clone())),
        }
    }
}

pub struct VariantOptions {
    pub aliases: Vec<String>,
    pub doc: Option<String>,
    pub rename: Option<String>,
    pub rename_all: RenameRule,
    pub skip: bool,
    pub with: With,
}

impl VariantOptions {
    pub fn new(attributes: &[Attribute], span: Span) -> Result<Self, Vec<syn::Error>> {
        let avro = avro::VariantAttributes::from_attributes(attributes).map_err(darling_to_syn)?;
        let serde =
            serde::VariantAttributes::from_attributes(attributes).map_err(darling_to_syn)?;

        // Check for deprecated attributes
        avro.deprecated(span);

        // Collect errors so user gets all feedback at once
        let mut errors = Vec::new();

        // Check for any Serde attributes that are hard errors
        if serde.other || serde.untagged {
            errors.push(syn::Error::new(
                span,
                "AvroSchema: `#[serde(other)]` and `#[serde(untagged)]` are not supported on variants",
            ));
        }
        if serde.rename_all.deserialize != serde.rename_all.serialize {
            errors.push(syn::Error::new(
                span,
                r#"AvroSchema: rename rules for serializing and deserializing must match (`rename_all(serialize = "..", deserialize = "..")`)"#
            ));
        }

        // Check for conflicts between Serde and Avro
        if avro.rename.is_some() && serde.rename != avro.rename {
            errors.push(syn::Error::new(
                span,
                r#"`#[avro(rename = "..")]` must match `#[serde(rename = "..")]`, it's also deprecated. Please use only `#[serde(rename = "..")]`"#
            ));
        }

        let with = match With::from_avro_and_serde(&avro.with, serde.with.as_ref(), span) {
            Ok(with) => with,
            Err(error) => {
                errors.push(error);
                // This won't actually be used, but it does simplify the code
                With::Trait
            }
        };

        if !errors.is_empty() {
            return Err(errors);
        }

        let doc = avro.doc.or_else(|| extract_rustdoc(attributes));

        Ok(Self {
            aliases: serde.alias,
            doc,
            rename: serde.rename,
            rename_all: serde.rename_all.serialize,
            // For variants we don't care about defaults for skipping, as Serde will error if a skipped
            // variant is serialized or deserialized.
            skip: serde.skip || (serde.skip_serializing && serde.skip_deserializing),
            with,
        })
    }

    /// Check that only the `skip`, `rename` and `alias` attributes are set.
    ///
    /// This is used for variants where the other attributes are not allowed.
    pub fn only_skip_rename_and_alias_can_be_set(&self) -> bool {
        self.doc.is_none() && self.rename_all == RenameRule::None && self.with == With::Trait
    }
}

/// How to get the default value for a value.
#[derive(Debug, PartialEq, Default)]
pub enum FieldDefault {
    /// Use `<T as AvroSchemaComponent>::field_default`.
    #[default]
    Trait,
    /// Don't set a default.
    Disabled,
    /// Use this JSON value.
    Value(Value),
}

impl FromMeta for FieldDefault {
    fn from_string(value: &str) -> darling::Result<Self> {
        Ok(Self::Value(serde_json::from_str(value).map_err(|e| {
            darling::Error::custom(format!("Failed to parse field default: {e:?}"))
        })?))
    }

    fn from_bool(value: bool) -> darling::Result<Self> {
        if value {
            Err(darling::Error::custom(
                "Expected `false` or a JSON string, got `true`",
            ))
        } else {
            Ok(Self::Disabled)
        }
    }
}

#[derive(Default)]
pub struct FieldOptions {
    pub doc: Option<String>,
    pub default: FieldDefault,
    pub alias: Vec<String>,
    pub rename: Option<String>,
    pub skip: bool,
    pub flatten: bool,
    pub with: With,
}

impl FieldOptions {
    pub fn new(attributes: &[Attribute], span: Span) -> Result<Self, Vec<syn::Error>> {
        let mut avro =
            avro::FieldAttributes::from_attributes(attributes).map_err(darling_to_syn)?;
        let mut serde =
            serde::FieldAttributes::from_attributes(attributes).map_err(darling_to_syn)?;
        // Sort the aliases, so our check for equality does not fail if they are provided in a different order
        avro.alias.sort();
        serde.alias.sort();

        // Check for deprecated attributes
        avro.deprecated(span);

        // Collect errors so user gets all feedback at once
        let mut errors = Vec::new();

        // Check for conflicts between Serde and Avro
        if avro.skip && !(serde.skip || (serde.skip_serializing && serde.skip_deserializing)) {
            errors.push(syn::Error::new(
                span,
                "`#[avro(skip)]` requires `#[serde(skip)]`, it's also deprecated. Please use only `#[serde(skip)]`"
            ));
        }
        if avro.flatten && !serde.flatten {
            errors.push(syn::Error::new(
                span,
                "`#[avro(flatten)]` requires `#[serde(flatten)]`, it's also deprecated. Please use only `#[serde(flatten)]`"
            ));
        }
        // TODO: rename and alias checking can be relaxed with a more complex check, would require the field name
        if avro.rename.is_some() && serde.rename != avro.rename {
            errors.push(syn::Error::new(
                span,
                r#"`#[avro(rename = "..")]` must match `#[serde(rename = "..")]`, it's also deprecated. Please use only `#[serde(rename = "..")]`"#
            ));
        }
        if !avro.alias.is_empty() && serde.alias != avro.alias {
            errors.push(syn::Error::new(
                span,
                r#"`#[avro(alias = "..")]` must match `#[serde(alias = "..")]`, it's also deprecated. Please use only `#[serde(alias = "..")]`"#
            ));
        }

        let with = match With::from_avro_and_serde(&avro.with, serde.with.as_ref(), span) {
            Ok(with) => with,
            Err(error) => {
                errors.push(error);
                // This won't actually be used, but it does simplify the code
                With::Trait
            }
        };
        // TODO: Implement a better way to do this (maybe if user specifies `#[avro(with)]` also use that for the default)
        // Disable getting the field default, if the schema is not retrieved from the field type
        if with != With::Trait && avro.default == FieldDefault::Trait {
            avro.default = FieldDefault::Disabled;
        }

        if ((serde.skip_serializing && !serde.skip_deserializing)
            || serde.skip_serializing_if.is_some())
            && avro.default == FieldDefault::Disabled
        {
            errors.push(syn::Error::new(
                span,
                "`#[serde(skip_serializing)]` and `#[serde(skip_serializing_if)]` are incompatible with `#[avro(default = false)]`"
            ));
        }

        if !errors.is_empty() {
            return Err(errors);
        }

        let doc = avro.doc.or_else(|| extract_rustdoc(attributes));

        Ok(Self {
            doc,
            default: avro.default,
            alias: serde.alias,
            rename: serde.rename,
            skip: serde.skip || (serde.skip_serializing && serde.skip_deserializing),
            flatten: serde.flatten,
            with,
        })
    }
}

fn extract_rustdoc(attributes: &[Attribute]) -> Option<String> {
    let doc = attributes
        .iter()
        .filter(|attr| attr.style == AttrStyle::Outer && attr.path().is_ident("doc"))
        .filter_map(|attr| {
            let name_value = attr.meta.require_name_value();
            match name_value {
                Ok(name_value) => match &name_value.value {
                    syn::Expr::Lit(expr_lit) => match expr_lit.lit {
                        syn::Lit::Str(ref lit_str) => Some(lit_str.value().trim().to_string()),
                        _ => None,
                    },
                    _ => None,
                },
                Err(_) => None,
            }
        })
        .collect::<Vec<String>>()
        .join("\n");
    if doc.is_empty() { None } else { Some(doc) }
}

fn darling_to_syn(e: darling::Error) -> Vec<syn::Error> {
    let msg = format!("{e}");
    let token_errors = e.write_errors();
    vec![syn::Error::new(token_errors.span(), msg)]
}

#[cfg(nightly)]
/// Emit a compiler warning.
///
/// This is a no-op when the `nightly` feature is not enabled.
fn warn(span: Span, message: &str, help: &str) {
    proc_macro::Diagnostic::spanned(span.unwrap(), proc_macro::Level::Warning, message)
        .help(help)
        .emit();
}

#[cfg(not(nightly))]
/// Emit a compiler warning.
///
/// This is a no-op when the `nightly` feature is not enabled.
fn warn(_span: Span, _message: &str, _help: &str) {}