martin-g commented on code in PR #398:
URL: https://github.com/apache/avro-rs/pull/398#discussion_r2681739064
##########
avro_derive/src/lib.rs:
##########
@@ -264,6 +283,69 @@ fn get_data_enum_schema_def(
}
}
+/// Generate a schema definition for a type marked transparent.
+fn get_transparent_data_schema_def(
+ data: &syn::Data,
+ input_span: Span,
+) -> Result<TokenStream, Vec<syn::Error>> {
+ match data {
+ syn::Data::Struct(data_struct) => match &data_struct.fields {
+ syn::Fields::Named(fields_named) => {
+ if fields_named.named.len() != 1 {
+ return Err(vec![syn::Error::new(
+ input_span,
+ "#[serde(transparent)] is only allowed on structs with
one field",
+ )]);
+ }
+ let field = fields_named
+ .named
+ .first()
+ .expect("There is exactly one field");
+ let field_attrs = FieldOptions::new(&field.attrs,
field.span())?;
+ if field_attrs != FieldOptions::default() {
+ return Err(vec![syn::Error::new(
+ input_span,
+ "#[serde(transparent)] is incompatible with all other
attributes",
+ )]);
+ }
+ let ty = &field.ty;
+ Ok(quote! {
+ #ty::get_schema_in_ctxt(named_schemas, enclosing_namespace)
+ })
+ }
+ syn::Fields::Unnamed(_) => Err(vec![syn::Error::new(
+ input_span,
+ "AvroSchema derive does not work for tuple structs",
+ )]),
+ syn::Fields::Unit => Err(vec![syn::Error::new(
+ input_span,
+ "AvroSchema derive does not work for unit structs",
+ )]),
+ },
+ syn::Data::Enum(data_enum) => {
+ if data_enum
+ .variants
+ .iter()
+ .all(|v| syn::Fields::Unit == v.fields)
+ {
+ Err(vec![syn::Error::new(
+ input_span,
+ "AvroSchema derive does not support #[serde(transparent)]
on simple enums",
+ )])
+ } else {
+ Err(vec![syn::Error::new(
+ input_span,
+ "AvroSchema derive does not work for enums with non unit
structs",
+ )])
+ }
+ }
+ syn::Data::Union(_) => Err(vec![syn::Error::new(
+ input_span,
+ "AvroSchema derive only works for structs and simple enums",
Review Comment:
This error is returned by get_transparent_data_schema_def(), so it is
special for the `transparent` attribute.
The generic error is at
https://github.com/apache/avro-rs/blob/6b9a15f7db96b165cb6839dc9a655fdf1b5b7ddb/avro_derive/src/lib.rs#L91
--
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]