| 1 | use proc_macro2::TokenStream; |
| 2 | use quote::quote; |
| 3 | use syn::parse::{Parse, ParseStream, Parser}; |
| 4 | use syn::punctuated::Punctuated; |
| 5 | use syn::{parenthesized, Attribute, Error, Path, Result, Token}; |
| 6 | |
| 7 | use crate::parse::UnitStruct; |
| 8 | |
| 9 | enum Derive { |
| 10 | Copy, |
| 11 | Clone, |
| 12 | Default, |
| 13 | Hash, |
| 14 | PartialOrd, |
| 15 | Ord, |
| 16 | PartialEq, |
| 17 | Eq, |
| 18 | Debug, |
| 19 | } |
| 20 | |
| 21 | struct DeriveList { |
| 22 | derives: Vec<Derive>, |
| 23 | } |
| 24 | |
| 25 | impl Parse for DeriveList { |
| 26 | fn parse(input: ParseStream) -> Result<Self> { |
| 27 | let content; |
| 28 | parenthesized!(content in input); |
| 29 | let paths: Punctuated<Path, Token![,]> = content.parse_terminated(Path::parse_mod_style)?; |
| 30 | |
| 31 | let mut derives = Vec::new(); |
| 32 | for path in paths { |
| 33 | if path.is_ident("Copy") { |
| 34 | derives.push(Derive::Copy); |
| 35 | } else if path.is_ident("Clone") { |
| 36 | derives.push(Derive::Clone); |
| 37 | } else if path.is_ident("Default") { |
| 38 | derives.push(Derive::Default); |
| 39 | } else if path.is_ident("Hash") { |
| 40 | derives.push(Derive::Hash); |
| 41 | } else if path.is_ident("PartialOrd") { |
| 42 | derives.push(Derive::PartialOrd); |
| 43 | } else if path.is_ident("Ord") { |
| 44 | derives.push(Derive::Ord); |
| 45 | } else if path.is_ident("PartialEq") { |
| 46 | derives.push(Derive::PartialEq); |
| 47 | } else if path.is_ident("Eq") { |
| 48 | derives.push(Derive::Eq); |
| 49 | } else if path.is_ident("Debug") { |
| 50 | derives.push(Derive::Debug); |
| 51 | } else { |
| 52 | return Err(Error::new_spanned(path, "unsupported derive")); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | Ok(DeriveList { derives }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | pub fn expand<'a>( |
| 61 | attrs: &'a [Attribute], |
| 62 | input: &UnitStruct, |
| 63 | ) -> Result<(TokenStream, Vec<&'a Attribute>)> { |
| 64 | let mut expanded = TokenStream::new(); |
| 65 | let mut non_derives = Vec::new(); |
| 66 | |
| 67 | for attr in attrs { |
| 68 | if attr.path.is_ident("derive") { |
| 69 | let list = DeriveList::parse.parse2(attr.tokens.clone())?; |
| 70 | for derive in list.derives { |
| 71 | expanded.extend(apply(derive, input)); |
| 72 | } |
| 73 | } else { |
| 74 | non_derives.push(attr); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | Ok((expanded, non_derives)) |
| 79 | } |
| 80 | |
| 81 | fn apply(derive: Derive, input: &UnitStruct) -> TokenStream { |
| 82 | match derive { |
| 83 | Derive::Copy => expand_copy(input), |
| 84 | Derive::Clone => expand_clone(input), |
| 85 | Derive::Default => expand_default(input), |
| 86 | Derive::Hash => expand_hash(input), |
| 87 | Derive::PartialOrd => expand_partialord(input), |
| 88 | Derive::Ord => expand_ord(input), |
| 89 | Derive::PartialEq => expand_partialeq(input), |
| 90 | Derive::Eq => expand_eq(input), |
| 91 | Derive::Debug => expand_debug(input), |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | fn expand_copy(input: &UnitStruct) -> TokenStream { |
| 96 | let ident = &input.ident; |
| 97 | let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 98 | |
| 99 | quote! { |
| 100 | impl #impl_generics ::core::marker::Copy |
| 101 | for #ident #ty_generics #where_clause {} |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn expand_clone(input: &UnitStruct) -> TokenStream { |
| 106 | let ident = &input.ident; |
| 107 | let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 108 | |
| 109 | quote! { |
| 110 | #[allow(clippy::expl_impl_clone_on_copy)] |
| 111 | impl #impl_generics ::core::clone::Clone |
| 112 | for #ident #ty_generics #where_clause { |
| 113 | #[inline] |
| 114 | fn clone(&self) -> Self { |
| 115 | #ident |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | fn expand_default(input: &UnitStruct) -> TokenStream { |
| 122 | let ident = &input.ident; |
| 123 | let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 124 | |
| 125 | quote! { |
| 126 | impl #impl_generics ::core::default::Default |
| 127 | for #ident #ty_generics #where_clause { |
| 128 | #[inline] |
| 129 | fn default() -> Self { |
| 130 | #ident |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn expand_hash(input: &UnitStruct) -> TokenStream { |
| 137 | let ident = &input.ident; |
| 138 | let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 139 | |
| 140 | quote! { |
| 141 | impl #impl_generics ::core::hash::Hash |
| 142 | for #ident #ty_generics #where_clause { |
| 143 | #[inline] |
| 144 | fn hash<H: ::core::hash::Hasher>(&self, hasher: &mut H) { |
| 145 | let _ = hasher; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | fn expand_partialord(input: &UnitStruct) -> TokenStream { |
| 152 | let ident = &input.ident; |
| 153 | let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 154 | |
| 155 | quote! { |
| 156 | impl #impl_generics ::core::cmp::PartialOrd |
| 157 | for #ident #ty_generics #where_clause { |
| 158 | #[inline] |
| 159 | fn partial_cmp(&self, other: &Self) -> ::core::option::Option<::core::cmp::Ordering> { |
| 160 | let _ = other; |
| 161 | ::core::option::Option::Some(::core::cmp::Ordering::Equal) |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | fn expand_ord(input: &UnitStruct) -> TokenStream { |
| 168 | let ident = &input.ident; |
| 169 | let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 170 | |
| 171 | quote! { |
| 172 | impl #impl_generics ::core::cmp::Ord |
| 173 | for #ident #ty_generics #where_clause { |
| 174 | #[inline] |
| 175 | fn cmp(&self, other: &Self) -> ::core::cmp::Ordering { |
| 176 | let _ = other; |
| 177 | ::core::cmp::Ordering::Equal |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | fn expand_partialeq(input: &UnitStruct) -> TokenStream { |
| 184 | let ident = &input.ident; |
| 185 | let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 186 | |
| 187 | quote! { |
| 188 | impl #impl_generics ::core::cmp::PartialEq |
| 189 | for #ident #ty_generics #where_clause { |
| 190 | #[inline] |
| 191 | fn eq(&self, other: &Self) -> bool { |
| 192 | let _ = other; |
| 193 | true |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | fn expand_eq(input: &UnitStruct) -> TokenStream { |
| 200 | let ident = &input.ident; |
| 201 | let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 202 | |
| 203 | quote! { |
| 204 | impl #impl_generics ::core::cmp::Eq |
| 205 | for #ident #ty_generics #where_clause {} |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | fn expand_debug(input: &UnitStruct) -> TokenStream { |
| 210 | let ident = &input.ident; |
| 211 | let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 212 | let string = ident.to_string(); |
| 213 | |
| 214 | quote! { |
| 215 | impl #impl_generics ::core::fmt::Debug |
| 216 | for #ident #ty_generics #where_clause { |
| 217 | fn fmt(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { |
| 218 | ::core::fmt::Formatter::write_str(formatter, #string) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |