This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch feat/derive_with
in repository https://gitbox.apache.org/repos/asf/avro-rs.git


The following commit(s) were added to refs/heads/feat/derive_with by this push:
     new 8a69205  Add support for using a closure/lambda for #[avro(with = || 
Schema::Xyz )]
8a69205 is described below

commit 8a69205f35261aff66df5c0a46c839ced502708f
Author: Martin Tzvetanov Grigorov <[email protected]>
AuthorDate: Fri Jan 16 10:57:39 2026 +0200

    Add support for using a closure/lambda for #[avro(with = || Schema::Xyz )]
---
 avro_derive/src/lib.rs                             | 11 ++++++--
 avro_derive/tests/derive.rs                        | 32 ++++++++++++++++++++++
 .../tests/ui/avro_rs_397_with_expr_lambda.rs       | 27 ------------------
 .../tests/ui/avro_rs_397_with_expr_lambda.stderr   |  8 ------
 4 files changed, 41 insertions(+), 37 deletions(-)

diff --git a/avro_derive/src/lib.rs b/avro_derive/src/lib.rs
index 9b6a05a..603e5fa 100644
--- a/avro_derive/src/lib.rs
+++ b/avro_derive/src/lib.rs
@@ -23,7 +23,7 @@ mod case;
 use proc_macro2::{Span, TokenStream};
 use quote::quote;
 use syn::{
-    AttrStyle, Attribute, DeriveInput, Ident, Meta, Type, parse_macro_input, 
spanned::Spanned,
+    AttrStyle, Attribute, DeriveInput, Expr, Ident, Meta, Type, 
parse_macro_input, spanned::Spanned,
 };
 
 use crate::{
@@ -175,7 +175,14 @@ fn get_data_struct_schema_def(
                     With::Serde(path) => {
                         quote! {#path::get_schema_in_ctxt(named_schemas, 
enclosing_namespace)}
                     }
-                    With::Expr(expr) => quote! {#expr(named_schemas, 
enclosing_namespace)},
+                    With::Expr(expr) => match expr {
+                        Expr::Closure(closure) => {
+                            let _inputs = &closure.inputs;
+                            let body = &closure.body;
+                            quote! {#body}
+                        }
+                        _ => quote! {#expr(named_schemas, 
enclosing_namespace)},
+                    },
                 };
                 record_field_exprs.push(quote! {
                     schema_fields.push(::apache_avro::schema::RecordField {
diff --git a/avro_derive/tests/derive.rs b/avro_derive/tests/derive.rs
index bdb2b51..8528bdd 100644
--- a/avro_derive/tests/derive.rs
+++ b/avro_derive/tests/derive.rs
@@ -2042,3 +2042,35 @@ fn avro_rs_401_supported_type_variants() {
     let schema = Schema::parse_str(schema).unwrap();
     assert_eq!(schema, Foo::get_schema());
 }
+
+#[test]
+fn avro_rs_397_derive_with_expr_lambda() {
+    let schema = r#"
+  {
+    "type":"record",
+    "name":"Foo",
+    "fields": [
+      {
+        "name": "_a",
+        "type": "bytes"
+      },
+      {
+        "name": "_b",
+        "type": "int"
+      }
+    ]
+  }"#;
+
+    let expected_schema = Schema::parse_str(schema).unwrap();
+
+    #[derive(AvroSchema)]
+    struct Foo {
+        #[avro(with = || Schema::Bytes)]
+        _a: String,
+        _b: i32,
+    }
+
+    let derived_schema = Foo::get_schema();
+
+    assert_eq!(expected_schema, derived_schema);
+}
diff --git a/avro_derive/tests/ui/avro_rs_397_with_expr_lambda.rs 
b/avro_derive/tests/ui/avro_rs_397_with_expr_lambda.rs
deleted file mode 100644
index 06c6454..0000000
--- a/avro_derive/tests/ui/avro_rs_397_with_expr_lambda.rs
+++ /dev/null
@@ -1,27 +0,0 @@
-// 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 apache_avro::{AvroSchema, Schema};
-
-#[derive(AvroSchema)]
-struct Foo {
-    #[avro(with = || Schema::Bytes)]
-    a: String,
-    b: i32,
-}
-
-pub fn main() {}
diff --git a/avro_derive/tests/ui/avro_rs_397_with_expr_lambda.stderr 
b/avro_derive/tests/ui/avro_rs_397_with_expr_lambda.stderr
deleted file mode 100644
index 982748b..0000000
--- a/avro_derive/tests/ui/avro_rs_397_with_expr_lambda.stderr
+++ /dev/null
@@ -1,8 +0,0 @@
-error[E0618]: expected function, found `Schema`
-  --> tests/ui/avro_rs_397_with_expr_lambda.rs:22:22
-   |
-20 | #[derive(AvroSchema)]
-   |          ---------- call expression requires function
-21 | struct Foo {
-22 |     #[avro(with = || Schema::Bytes)]
-   |                      ^^^^^^^^^^^^^

Reply via email to