martin-g commented on code in PR #401:
URL: https://github.com/apache/avro-rs/pull/401#discussion_r2691985932


##########
avro_derive/tests/ui/avro_rs_401_unknown_type_variant.stderr:
##########
@@ -0,0 +1,5 @@
+error: AvroSchema: Unexpected type encountered, please open an issue if this 
kind of type should be supported: Type::BareFn { lifetimes: None, unsafety: 
None, abi: None, fn_token: Fn, paren_token: Paren, inputs: [], variadic: None, 
output: ReturnType::Type(RArrow, Type::Path { qself: None, path: Path { 
leading_colon: None, segments: [PathSegment { ident: Ident { ident: "String", 
span: #0 bytes(882..888) }, arguments: PathArguments::None }] } }) }

Review Comment:
   ```suggestion
   error: AvroSchema: Unexpected type encountered! Please open an issue if this 
kind of type should be supported: Type::BareFn { lifetimes: None, unsafety: 
None, abi: None, fn_token: Fn, paren_token: Paren, inputs: [], variadic: None, 
output: ReturnType::Type(RArrow, Type::Path { qself: None, path: Path { 
leading_colon: None, segments: [PathSegment { ident: Ident { ident: "String", 
span: #0 bytes(882..888) }, arguments: PathArguments::None }] } }) }
   ```



##########
avro_derive/src/lib.rs:
##########
@@ -266,45 +265,24 @@ fn get_data_enum_schema_def(
 
 /// Takes in the Tokens of a type and returns the tokens of an expression with 
return type `Schema`
 fn type_to_schema_expr(ty: &Type) -> Result<TokenStream, Vec<syn::Error>> {
-    if let Type::Path(p) = ty {
-        let type_string = p.path.segments.last().unwrap().ident.to_string();
-
-        let schema = match &type_string[..] {
-            "bool" => quote! {apache_avro::schema::Schema::Boolean},
-            "i8" | "i16" | "i32" | "u8" | "u16" => quote! 
{apache_avro::schema::Schema::Int},
-            "u32" | "i64" => quote! {apache_avro::schema::Schema::Long},
-            "f32" => quote! {apache_avro::schema::Schema::Float},
-            "f64" => quote! {apache_avro::schema::Schema::Double},
-            "String" | "str" => quote! {apache_avro::schema::Schema::String},
-            "char" => {
-                return Err(vec![syn::Error::new_spanned(
-                    ty,
-                    "AvroSchema: Cannot guarantee successful deserialization 
of this type",
-                )]);
-            }
-            "u64" => {
-                return Err(vec![syn::Error::new_spanned(
-                    ty,
-                    "Cannot guarantee successful serialization of this type 
due to overflow concerns",
-                )]);
-            } // Can't guarantee serialization type
-            _ => {
-                // Fails when the type does not implement AvroSchemaComponent 
directly
-                // TODO check and error report with something like 
https://docs.rs/quote/1.0.15/quote/macro.quote_spanned.html#example
-                type_path_schema_expr(p)
-            }
-        };
-        Ok(schema)
-    } else if let Type::Array(ta) = ty {
-        let inner_schema_expr = type_to_schema_expr(&ta.elem)?;
-        Ok(quote! {apache_avro::schema::Schema::array(#inner_schema_expr)})
-    } else if let Type::Reference(tr) = ty {
-        type_to_schema_expr(&tr.elem)
-    } else {
-        Err(vec![syn::Error::new_spanned(
+    match ty {
+        Type::Array(_) | Type::Slice(_) | Type::Path(_) | Type::Reference(_) 
=> Ok(
+            quote! {<#ty as 
apache_avro::AvroSchemaComponent>::get_schema_in_ctxt(named_schemas, 
enclosing_namespace)},
+        ),
+        Type::Ptr(_) => Err(vec![syn::Error::new_spanned(
             ty,
-            format!("Unable to generate schema for type: {ty:?}"),
-        )])
+            "AvroSchema: derive does not support raw pointers",
+        )]),
+        Type::Tuple(_) => Err(vec![syn::Error::new_spanned(
+            ty,
+            "AvroSchema: derive does not support tuples",
+        )]),
+        _ => Err(vec![syn::Error::new_spanned(
+            ty,
+            format!(
+                "AvroSchema: Unexpected type encountered, please open an issue 
if this kind of type should be supported: {ty:?}"

Review Comment:
   ```suggestion
                   "AvroSchema: Unexpected type encountered! Please open an 
issue if this kind of type should be supported: {ty:?}"
   ```



-- 
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