This is an automated email from the ASF dual-hosted git repository.
kriskras99 pushed a commit to branch feat/enums
in repository https://gitbox.apache.org/repos/asf/avro-rs.git
The following commit(s) were added to refs/heads/feat/enums by this push:
new 6a5b20d docs
6a5b20d is described below
commit 6a5b20de3cdd09faf8d3e906fafd66fa693bf7c5
Author: Kriskras99 <[email protected]>
AuthorDate: Fri Mar 6 08:57:26 2026 +0100
docs
---
avro_derive/src/attributes/mod.rs | 14 +++++---
avro_derive/src/enums/bare_union.rs | 7 +++-
avro_derive/src/enums/record_internally_tagged.rs | 7 +++-
avro_derive/src/enums/record_tag_content.rs | 7 +++-
avro_derive/src/enums/union_of_records.rs | 15 ++++++--
avro_derive/src/lib.rs | 42 +++++++++++++++++------
avro_derive/src/tuple.rs | 33 ++++++++++++------
7 files changed, 93 insertions(+), 32 deletions(-)
diff --git a/avro_derive/src/attributes/mod.rs
b/avro_derive/src/attributes/mod.rs
index ce609cd..13fadfe 100644
--- a/avro_derive/src/attributes/mod.rs
+++ b/avro_derive/src/attributes/mod.rs
@@ -390,20 +390,24 @@ pub enum FieldDefault {
}
impl FieldDefault {
- pub fn into_tokenstream(self, span: Span, field_type: &Type) ->
Result<TokenStream, Vec<syn::Error>> {
+ pub fn into_tokenstream(
+ self,
+ span: Span,
+ field_type: &Type,
+ ) -> Result<TokenStream, Vec<syn::Error>> {
match self {
- FieldDefault::Disabled => Ok(quote! { None }),
+ FieldDefault::Disabled => Ok(quote! { ::std::option::Option::None
}),
FieldDefault::Trait => type_to_field_default_expr(field_type),
FieldDefault::Value(default_value) => {
- let _: serde_json::Value =
serde_json::from_str(&default_value[..])
- .map_err(|e| {
+ let _: serde_json::Value =
+ serde_json::from_str(&default_value[..]).map_err(|e| {
vec![syn::Error::new(
span,
format!("Invalid avro default json: \n{e}"),
)]
})?;
Ok(quote! {
-
Some(::serde_json::from_str(#default_value).expect(format!("Invalid JSON:
{:?}", #default_value).as_str()))
+
::std::option::Option::Some(::serde_json::from_str(#default_value).expect(format!("Invalid
JSON: {:?}", #default_value).as_str()))
})
}
}
diff --git a/avro_derive/src/enums/bare_union.rs
b/avro_derive/src/enums/bare_union.rs
index 3385343..4c30231 100644
--- a/avro_derive/src/enums/bare_union.rs
+++ b/avro_derive/src/enums/bare_union.rs
@@ -22,7 +22,12 @@ pub fn get_data_enum_schema_def(
});
match variant.fields {
Fields::Named(named) => {
- let fields = named_to_record_fields(named,
variant_attrs.rename_all.or(container_attrs.rename_all_fields))?;
+ let fields = named_to_record_fields(
+ named,
+ variant_attrs
+ .rename_all
+ .or(container_attrs.rename_all_fields),
+ )?;
let schema_expr = quote! {
::apache_avro::schema::Schema::Record(
diff --git a/avro_derive/src/enums/record_internally_tagged.rs
b/avro_derive/src/enums/record_internally_tagged.rs
index 8c3c76e..1baf095 100644
--- a/avro_derive/src/enums/record_internally_tagged.rs
+++ b/avro_derive/src/enums/record_internally_tagged.rs
@@ -23,7 +23,12 @@ pub fn get_data_enum_schema_def(
});
match variant.fields {
Fields::Named(named) => {
- field_additions.extend(named_to_record_fields(named,
variant_attrs.rename_all.or(container_attrs.rename_all_fields))?);
+ field_additions.extend(named_to_record_fields(
+ named,
+ variant_attrs
+ .rename_all
+ .or(container_attrs.rename_all_fields),
+ )?);
}
Fields::Unnamed(unnamed) => {
if unnamed.unnamed.len() == 1 {
diff --git a/avro_derive/src/enums/record_tag_content.rs
b/avro_derive/src/enums/record_tag_content.rs
index c02eca9..6108f03 100644
--- a/avro_derive/src/enums/record_tag_content.rs
+++ b/avro_derive/src/enums/record_tag_content.rs
@@ -25,7 +25,12 @@ pub fn get_data_enum_schema_def(
});
match variant.fields {
Fields::Named(named) => {
- let fields = named_to_record_fields(named,
variant_attrs.rename_all.or(container_attrs.rename_all_fields))?;
+ let fields = named_to_record_fields(
+ named,
+ variant_attrs
+ .rename_all
+ .or(container_attrs.rename_all_fields),
+ )?;
let schema_expr = quote! {
::apache_avro::schema::Schema::Record(
diff --git a/avro_derive/src/enums/union_of_records.rs
b/avro_derive/src/enums/union_of_records.rs
index 44a853c..e556708 100644
--- a/avro_derive/src/enums/union_of_records.rs
+++ b/avro_derive/src/enums/union_of_records.rs
@@ -1,6 +1,6 @@
use crate::attributes::{NamedTypeOptions, VariantOptions};
+use crate::named_to_record_fields;
use crate::tuple::tuple_to_record_schema;
-use crate::{named_to_record_fields};
use proc_macro2::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
@@ -20,7 +20,12 @@ pub fn get_data_enum_schema_def(
});
match variant.fields {
Fields::Named(named) => {
- let fields = named_to_record_fields(named,
variant_attrs.rename_all.or(container_attrs.rename_all_fields))?;
+ let fields = named_to_record_fields(
+ named,
+ variant_attrs
+ .rename_all
+ .or(container_attrs.rename_all_fields),
+ )?;
let schema_expr = quote! {
::apache_avro::schema::Schema::Record(
@@ -36,7 +41,11 @@ pub fn get_data_enum_schema_def(
}
Fields::Unnamed(unnamed) => {
let schema_expr = if unnamed.unnamed.len() == 1 {
- tuple_to_record_schema(unnamed, &name,
&["org.apache.avro.rust.union_of_records"])?
+ tuple_to_record_schema(
+ unnamed,
+ &name,
+ &["org.apache.avro.rust.union_of_records"],
+ )?
} else {
tuple_to_record_schema(unnamed, &name, &[])?
};
diff --git a/avro_derive/src/lib.rs b/avro_derive/src/lib.rs
index 96583db..ce92b9c 100644
--- a/avro_derive/src/lib.rs
+++ b/avro_derive/src/lib.rs
@@ -37,12 +37,14 @@ mod tuple;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
- DataStruct, DeriveInput, Expr, Field, Fields, FieldsNamed, Generics,
Ident, Type, parse_macro_input, spanned::Spanned
+ DataStruct, DeriveInput, Expr, Field, Fields, FieldsNamed, Generics,
Ident, Type,
+ parse_macro_input, spanned::Spanned,
};
use crate::{
attributes::{FieldOptions, NamedTypeOptions, With},
- case::RenameRule, tuple::unnamed_to_record_fields,
+ case::RenameRule,
+ tuple::unnamed_to_record_fields,
};
#[proc_macro_derive(AvroSchema, attributes(avro, serde))]
@@ -81,10 +83,8 @@ fn derive_avro_schema(input: DeriveInput) ->
Result<TokenStream, Vec<syn::Error>
let (get_schema_impl, get_record_fields_impl) = if
named_type_options.transparent {
get_transparent_struct_schema_def(data_struct.fields,
input_span)?
} else {
- let (schema_def, record_fields) = get_struct_schema_def(
- &named_type_options,
- data_struct,
- )?;
+ let (schema_def, record_fields) =
+ get_struct_schema_def(&named_type_options,
data_struct)?;
(
handle_named_schemas(named_type_options.name,
schema_def),
record_fields,
@@ -282,7 +282,7 @@ fn get_transparent_struct_schema_def(
"AvroSchema: #[serde(transparent)] is only allowed on
structs with one unskipped field",
)])
}
- },
+ }
Fields::Unit => Err(vec![syn::Error::new(
input_span,
"AvroSchema: `#[serde(transparent)` does not work for unit
structs",
@@ -314,6 +314,15 @@ fn get_field_schema_expr(field: &Field, with: With) ->
Result<TokenStream, Vec<s
}
}
+/// Call `get_record_fields_in_ctxt` for this field.
+///
+/// # `TokenStream`
+/// ## Context
+/// The token stream expects the following variables to be defined:
+/// - `named_schemas`: `&mut HashSet<Name>`
+/// - `enclosing_namespace`: `Option<&str>`
+/// ## Returns
+/// A call to a `get_record_fields_in_ctxt(named_schemas, enclosing_namespace)
-> Option<Vec<RecordField>>`
fn get_field_get_record_fields_expr(
field: &Field,
with: With,
@@ -350,6 +359,14 @@ fn get_field_get_record_fields_expr(
}
/// Takes in the Tokens of a type and returns the tokens of an expression with
return type `Schema`
+///
+/// # `TokenStream`
+/// ## Context
+/// The token stream expects the following variables to be defined:
+/// - `named_schemas`: `&mut HashSet<Name>`
+/// - `enclosing_namespace`: `Option<&str>`
+/// ## Returns
+/// A call to a `get_schema_in_ctxt(named_schemas, enclosing_namespace) ->
Schema`
fn type_to_schema_expr(ty: &Type) -> Result<TokenStream, Vec<syn::Error>> {
match ty {
Type::Array(_) | Type::Slice(_) | Type::Path(_) | Type::Reference(_)
=> Ok(
@@ -417,7 +434,10 @@ fn type_to_field_default_expr(ty: &Type) ->
Result<TokenStream, Vec<syn::Error>>
}
/// Create a vector of `RecordField`s.
-fn named_to_record_fields(named: FieldsNamed, rename_all: RenameRule) ->
Result<Vec<TokenStream>, Vec<syn::Error>> {
+fn named_to_record_fields(
+ named: FieldsNamed,
+ rename_all: RenameRule,
+) -> Result<Vec<TokenStream>, Vec<syn::Error>> {
let mut fields = Vec::with_capacity(named.named.len());
for field in named.named {
let field_attrs = FieldOptions::new(&field.attrs, field.span())?;
@@ -455,7 +475,9 @@ fn named_to_record_fields(named: FieldsNamed, rename_all:
RenameRule) -> Result<
}
_ => {}
}
- let default_value =
field_attrs.default.into_tokenstream(field.ident.span(), &field.ty)?;
+ let default_value = field_attrs
+ .default
+ .into_tokenstream(field.ident.span(), &field.ty)?;
let aliases = field_aliases(&field_attrs.alias);
let doc = doc_into_tokenstream(field_attrs.doc);
let field_schema_expr = type_to_schema_expr(&field.ty)?;
@@ -473,7 +495,7 @@ fn named_to_record_fields(named: FieldsNamed, rename_all:
RenameRule) -> Result<
}
/// Stolen from serde
-fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
+fn to_compile_errors(errors: Vec<syn::Error>) -> TokenStream {
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
quote!(#(#compile_errors)*)
}
diff --git a/avro_derive/src/tuple.rs b/avro_derive/src/tuple.rs
index 8d98995..1c100e4 100644
--- a/avro_derive/src/tuple.rs
+++ b/avro_derive/src/tuple.rs
@@ -1,19 +1,23 @@
use proc_macro2::TokenStream;
-use syn::{FieldsUnnamed, spanned::Spanned};
use quote::quote;
+use syn::{FieldsUnnamed, spanned::Spanned};
use crate::{FieldOptions, doc_into_tokenstream, field_aliases,
type_to_schema_expr};
/// Create a `Schema::Record` from this tuple definition.
-///
+///
/// Fields are named `field_{field_index}` and the struct will have the
provided name.
-///
+///
/// The schema will have the attribute `org.apache.avro.rust.tuple` any any
other specified in `extra_attributes`.
/// All attributes will have a value of `true`.
-pub fn tuple_to_record_schema(unnamed: FieldsUnnamed, name: &str,
extra_attributes: &[&str]) -> Result<TokenStream, Vec<syn::Error>> {
+pub fn tuple_to_record_schema(
+ unnamed: FieldsUnnamed,
+ name: &str,
+ extra_attributes: &[&str],
+) -> Result<TokenStream, Vec<syn::Error>> {
let fields = unnamed_to_record_fields(unnamed)?;
- Ok(quote! {
+ Ok(quote! {
::apache_avro::schema::Schema::Record(::apache_avro::schema::RecordSchema::builder()
.name(::apache_avro::schema::Name::new_with_enclosing_namespace(#name,
enclosing_namespace).expect(&format!("Unable to parse variant record name for
schema {}", #name)[..]))
.fields(vec![#(#fields,)*])
@@ -29,21 +33,28 @@ pub fn tuple_to_record_schema(unnamed: FieldsUnnamed, name:
&str, extra_attribut
}
/// Create a vector of `RecordField`s named `field_{field_index}`.
-pub fn unnamed_to_record_fields(unnamed: FieldsUnnamed) ->
Result<Vec<TokenStream>, Vec<syn::Error>> {
+pub fn unnamed_to_record_fields(
+ unnamed: FieldsUnnamed,
+) -> Result<Vec<TokenStream>, Vec<syn::Error>> {
let mut fields = Vec::with_capacity(unnamed.unnamed.len());
for (index, field) in unnamed.unnamed.into_iter().enumerate() {
let field_attrs = FieldOptions::new(&field.attrs, field.span())?;
if field_attrs.skip {
continue;
} else if field_attrs.flatten {
- return Err(vec![
- syn::Error::new(field.span(), "AvroSchema: `#[serde(flatten)]`
is not supported on tuple fields")
- ]);
+ return Err(vec![syn::Error::new(
+ field.span(),
+ "AvroSchema: `#[serde(flatten)]` is not supported on tuple
fields",
+ )]);
}
- let default_value =
field_attrs.default.into_tokenstream(field.ident.span(), &field.ty)?;
+ let default_value = field_attrs
+ .default
+ .into_tokenstream(field.ident.span(), &field.ty)?;
let aliases = field_aliases(&field_attrs.alias);
let doc = doc_into_tokenstream(field_attrs.doc);
- let name = field_attrs.rename.unwrap_or_else(||
format!("field_{index}"));
+ let name = field_attrs
+ .rename
+ .unwrap_or_else(|| format!("field_{index}"));
let field_schema_expr = type_to_schema_expr(&field.ty)?;
fields.push(quote! {
::apache_avro::schema::RecordField::builder()