MCPcopy Create free account
hub / github.com/BillyDM/Firewheel / from_enum

Method from_enum

crates/firewheel-macros/src/diff.rs:103–158  ·  view source on GitHub ↗

This is a fair bit more complicated because we need to account for three kinds of variants _and_ we need to be able to construct variants with all required data at once in addition to fine-grained diffing.

(
        identifier: &syn::Ident,
        data: &syn::DataEnum,
        firewheel_path: &syn::Path,
        _: &TokenStream2,
    )

Source from the content-addressed store, hash-verified

101 // three kinds of variants _and_ we need to be able to construct variants
102 // with all required data at once in addition to fine-grained diffing.
103 pub fn from_enum(
104 identifier: &syn::Ident,
105 data: &syn::DataEnum,
106 firewheel_path: &syn::Path,
107 _: &TokenStream2,
108 ) -> syn::Result<DiffOutput> {
109 // trivial unit enum
110 if data.variants.iter().all(|v| v.fields.is_empty()) {
111 let diff_arms = data.variants.iter().enumerate().map(|(i, variant)| {
112 let index = i as u32;
113 let variant_ident = &variant.ident;
114
115 quote! {
116 (#identifier::#variant_ident, #identifier::#variant_ident) => {}
117 (#identifier::#variant_ident, _) => {
118 event_queue.push_param(
119 #firewheel_path::event::ParamData::U32(#index),
120 path,
121 );
122 }
123 }
124 });
125
126 let body = quote! {
127 match (self, baseline) {
128 #(#diff_arms)*
129 }
130 };
131
132 return Ok(DiffOutput {
133 body,
134 bounds: vec![],
135 });
136 }
137
138 let body = quote! {
139 if self != baseline {
140 event_queue.push_param(
141 #firewheel_path::event::ParamData::any(<#identifier as ::core::clone::Clone>::clone(self)),
142 path,
143 );
144 }
145 };
146
147 let span = identifier.span();
148 Ok(DiffOutput {
149 body,
150 bounds: vec![quote_spanned! {span=>
151 #identifier: ::core::cmp::PartialEq
152 + ::core::clone::Clone
153 + ::core::marker::Send
154 + ::core::marker::Sync
155 + 'static
156 }],
157 })
158 }
159}

Callers

nothing calls this directly

Calls 2

allMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected