By Guest

Unnamed Paste

Auto Detect 1.8 KiB 41 one month ago
/// Get generic parameter from [`syn::PathArguments`] if it exists fn get_generic(type_args: &PathArguments) -> Option<syn::Type> { if let PathArguments::AngleBracketed(bracketed) = type_args { if let GenericArgument::Type(nested_type) = &bracketed.args[0] { return Some(nested_type.clone()); } } None } fn get_generic_param(ty: &Type) -> Result<Option<syn::Type>, ()> { if let Type::Path(path) = ty { let outer_type = &path.path.segments[0].ident; // Was a vec or option if outer_type == "Vec" || outer_type == "Option" { let inner_ty = match get_generic(&path.path.segments[0].arguments) { Some(ty) => ty, None => return Err(()), }; if let Type::Path(path) = &inner_ty { // Two layers of nesting is allowed if they are not identical if &path.path.segments[0].ident == outer_type { // No nesting identical types allowed Err(()) } else if &path.path.segments[0].ident == "Vec" || &path.path.segments[0].ident == "Option" { // Final possible generic match get_generic(&path.path.segments[0].arguments) { Some(ty) => Ok(Some(ty)), None => Err(()), } } else { // Was not a nested type Ok(Some(inner_ty)) } } else { Err(()) } // panic!("{:#?}", type_param); } else { Ok(None) } // if path.path.segments[0].ident.ident == "Vec"; } else { Err(()) } }