to_resolved_value.rs (1670B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 use crate::cg; 6 use crate::to_computed_value; 7 use proc_macro2::TokenStream; 8 use syn::DeriveInput; 9 use synstructure::BindStyle; 10 11 pub fn derive(input: DeriveInput) -> TokenStream { 12 let trait_impl = |from_body, to_body| { 13 quote! { 14 #[inline] 15 fn from_resolved_value(from: Self::ResolvedValue) -> Self { 16 #from_body 17 } 18 19 #[inline] 20 fn to_resolved_value( 21 self, 22 context: &crate::values::resolved::Context, 23 ) -> Self::ResolvedValue { 24 #to_body 25 } 26 } 27 }; 28 29 to_computed_value::derive_to_value( 30 input, 31 parse_quote!(crate::values::resolved::ToResolvedValue), 32 parse_quote!(ResolvedValue), 33 BindStyle::Move, 34 |binding| { 35 let attrs = cg::parse_field_attrs::<ResolvedValueAttrs>(&binding.ast()); 36 to_computed_value::ToValueAttrs { 37 field_bound: attrs.field_bound, 38 no_field_bound: attrs.no_field_bound, 39 } 40 }, 41 |binding| quote!(crate::values::resolved::ToResolvedValue::from_resolved_value(#binding)), 42 |binding| quote!(crate::values::resolved::ToResolvedValue::to_resolved_value(#binding, context)), 43 trait_impl, 44 ) 45 } 46 47 #[derive(Default, FromField)] 48 #[darling(attributes(resolve), default)] 49 struct ResolvedValueAttrs { 50 field_bound: bool, 51 no_field_bound: bool, 52 }