Kriskras99 commented on code in PR #561:
URL: https://github.com/apache/avro-rs/pull/561#discussion_r3480068575


##########
avro_derive/src/enums/plain.rs:
##########
@@ -17,49 +17,128 @@
 
 use crate::attributes::{NamedTypeOptions, VariantOptions};
 use crate::case::RenameRule;
-use crate::enums::default_enum_variant;
+use crate::implementation::Implementation;
+use crate::utils::json_value_expr;
 use crate::{aliases, preserve_optional};
-use proc_macro2::{Span, TokenStream};
+use proc_macro2::{Ident, Span};
 use quote::quote;
+use serde_json::Value;
 use syn::spanned::Spanned;
-use syn::{DataEnum, Fields};
+use syn::{Attribute, DataEnum, Fields, Generics, Meta};
 
-pub fn schema_def(
-    container_attrs: &NamedTypeOptions,
-    data_enum: &DataEnum,
-    ident_span: Span,
-) -> Result<TokenStream, Vec<syn::Error>> {
+pub fn to_implementation(
+    input_span: Span,
+    ident: Ident,
+    generics: Generics,
+    container_attrs: NamedTypeOptions,
+    data: DataEnum,
+) -> Result<Implementation, Vec<syn::Error>> {
     let doc = preserve_optional(container_attrs.doc.as_ref());
     let enum_aliases = aliases(&container_attrs.aliases);
-    if data_enum.variants.iter().all(|v| Fields::Unit == v.fields) {
-        let default_value = default_enum_variant(data_enum, ident_span)?;
-        let default = preserve_optional(default_value);
-        let mut symbols = Vec::new();
-        for variant in &data_enum.variants {
-            let field_attrs = VariantOptions::new(&variant.attrs, 
variant.span())?;
-            let name = match (field_attrs.rename, container_attrs.rename_all) {
-                (Some(rename), _) => rename,
+
+    let mut errors = Vec::new();
+    let mut symbols = Vec::new();
+    let mut default = match &container_attrs.default {
+        None => None,
+        Some(Value::String(s)) => Some(s.clone()),
+        Some(_) => {
+            errors.push(syn::Error::new(
+                input_span,
+                "Expected a JSON string for an enum default",
+            ));
+            None
+        }
+    };
+
+    for variant in data.variants {
+        let variant_attrs = match VariantOptions::new(&variant.attrs, 
variant.span()) {
+            Ok(attrs) => attrs,
+            Err(errs) => {
+                errors.extend(errs);
+                continue;
+            }
+        };
+
+        // Check the skip attribute before the check if it is a unit variant, 
because a skipped (un)named
+        // variant is not a problem
+        if variant_attrs.skip {
+            continue;
+        }
+
+        if !matches!(variant.fields, Fields::Unit) {
+            errors.push(syn::Error::new(
+                variant.span(),
+                "AvroSchema: derive does not work for enums with non unit 
structs",
+            ));
+            continue;
+        }
+
+        // When a variant has the `#[default]` attribute and we don't have a 
default yet, we assign
+        // it as the default. If an enum has multiple variants with the 
default attribute, the compiler
+        // throws an error anyway so we can ignore that situation. If the 
default attribute is on a
+        // skipped variant, we ignore it as Serde will throw an error if we 
try to deserialize that
+        // variant.
+        if default.is_none() && has_default_attr(&variant.attrs) {

Review Comment:
   The `#[default]` attribute is from `#[derive(Default)]` so I think the user 
should be able to overwrite that with the `#[avro(default = "...")]` attribute.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to