| 1118 | } |
| 1119 | |
| 1120 | fn derive_enum_body( |
| 1121 | data: &DataEnum, |
| 1122 | post_parse_steps: &TokenStream, |
| 1123 | where_collector: &mut WhereCollector, |
| 1124 | ) -> Result<TokenStream> { |
| 1125 | let plans: Vec<VariantPlan> = |
| 1126 | data.variants.iter().map(|v| VariantPlan::new(v, post_parse_steps)).collect::<Result<_>>()?; |
| 1127 | |
| 1128 | // Group by (first-field type, has-effective-atom). No-atom variants get their |
| 1129 | // own peeked-fallback group, after any atom-dispatch group for the same type. |
| 1130 | let grouped = plans |
| 1131 | .iter() |
| 1132 | .sorted_by_key(|p| { |
| 1133 | let ty = &p.first_type; |
| 1134 | (quote!(#ty).to_string(), p.effective_atom.is_none()) |
| 1135 | }) |
| 1136 | .chunk_by(|p| { |
| 1137 | let ty = &p.first_type; |
| 1138 | (quote!(#ty).to_string(), p.effective_atom.is_none()) |
| 1139 | }); |
| 1140 | |
| 1141 | let ts = grouped |
| 1142 | .into_iter() |
| 1143 | .with_position() |
| 1144 | .map(|(position, ((_type_str, no_atom), group))| { |
| 1145 | let variants: Vec<&VariantPlan> = group.collect(); |
| 1146 | Ok(EnumGroupPlan::new(no_atom, variants, &position).render(where_collector)) |
| 1147 | }) |
| 1148 | .collect::<Result<TokenStream>>()?; |
| 1149 | Ok(ts) |
| 1150 | } |
| 1151 | |
| 1152 | pub fn derive(input: DeriveInput) -> Result<TokenStream> { |
| 1153 | let mut where_collector = WhereCollector::new(); |