2017-05-21 16:48:10 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2018-12-02 21:46:06 +03:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
2017-05-21 16:48:10 +03:00
|
|
|
|
2019-03-30 03:16:01 +03:00
|
|
|
use derive_common::cg;
|
2018-11-30 14:28:20 +03:00
|
|
|
use proc_macro2::{Span, TokenStream};
|
|
|
|
use syn::{DeriveInput, Ident};
|
2017-08-23 18:01:15 +03:00
|
|
|
use synstructure::BindStyle;
|
2017-05-21 16:48:10 +03:00
|
|
|
|
2018-11-30 14:28:20 +03:00
|
|
|
pub fn derive(mut input: DeriveInput) -> TokenStream {
|
2018-03-10 01:29:05 +03:00
|
|
|
let mut where_clause = input.generics.where_clause.take();
|
2019-03-03 14:31:21 +03:00
|
|
|
cg::propagate_clauses_to_output_type(
|
|
|
|
&mut where_clause,
|
|
|
|
&input.generics,
|
|
|
|
parse_quote!(crate::values::computed::ToComputedValue),
|
|
|
|
parse_quote!(ComputedValue),
|
|
|
|
);
|
2018-03-10 01:29:05 +03:00
|
|
|
let (to_body, from_body) = {
|
|
|
|
let params = input.generics.type_params().collect::<Vec<_>>();
|
|
|
|
for param in ¶ms {
|
|
|
|
cg::add_predicate(
|
|
|
|
&mut where_clause,
|
2018-11-01 15:41:10 +03:00
|
|
|
parse_quote!(#param: crate::values::computed::ToComputedValue),
|
2018-03-10 01:29:05 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let to_body = cg::fmap_match(&input, BindStyle::Ref, |binding| {
|
|
|
|
let attrs = cg::parse_field_attrs::<ComputedValueAttrs>(&binding.ast());
|
|
|
|
if attrs.field_bound {
|
|
|
|
let ty = &binding.ast().ty;
|
|
|
|
|
2018-09-07 16:18:51 +03:00
|
|
|
let output_type = cg::map_type_params(
|
|
|
|
ty,
|
|
|
|
¶ms,
|
2018-11-01 15:41:10 +03:00
|
|
|
&mut |ident| parse_quote!(<#ident as crate::values::computed::ToComputedValue>::ComputedValue),
|
2018-09-07 16:18:51 +03:00
|
|
|
);
|
2018-03-10 01:29:05 +03:00
|
|
|
|
|
|
|
cg::add_predicate(
|
|
|
|
&mut where_clause,
|
|
|
|
parse_quote!(
|
2018-11-01 15:41:10 +03:00
|
|
|
#ty: crate::values::computed::ToComputedValue<ComputedValue = #output_type>
|
2018-03-10 01:29:05 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
quote! {
|
2018-11-01 15:41:10 +03:00
|
|
|
crate::values::computed::ToComputedValue::to_computed_value(#binding, context)
|
2018-03-10 01:29:05 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
let from_body = cg::fmap_match(&input, BindStyle::Ref, |binding| {
|
|
|
|
quote! {
|
2018-11-01 15:41:10 +03:00
|
|
|
crate::values::computed::ToComputedValue::from_computed_value(#binding)
|
2018-03-10 01:29:05 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
(to_body, from_body)
|
|
|
|
};
|
|
|
|
|
|
|
|
input.generics.where_clause = where_clause;
|
2017-05-21 16:48:10 +03:00
|
|
|
let name = &input.ident;
|
2018-03-10 01:29:05 +03:00
|
|
|
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
2017-08-23 18:01:15 +03:00
|
|
|
|
2018-03-10 01:29:05 +03:00
|
|
|
if input.generics.type_params().next().is_none() {
|
2018-03-03 22:15:17 +03:00
|
|
|
return quote! {
|
2018-11-01 15:41:10 +03:00
|
|
|
impl #impl_generics crate::values::computed::ToComputedValue for #name #ty_generics
|
2018-03-03 22:15:17 +03:00
|
|
|
#where_clause
|
|
|
|
{
|
2018-03-10 01:29:05 +03:00
|
|
|
type ComputedValue = Self;
|
2018-03-03 22:15:17 +03:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_computed_value(
|
|
|
|
&self,
|
2018-11-01 15:41:10 +03:00
|
|
|
_context: &crate::values::computed::Context,
|
2018-03-03 22:15:17 +03:00
|
|
|
) -> Self::ComputedValue {
|
2018-11-01 15:41:10 +03:00
|
|
|
std::clone::Clone::clone(self)
|
2018-03-03 22:15:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
2018-11-01 15:41:10 +03:00
|
|
|
std::clone::Clone::clone(computed)
|
2018-03-03 22:15:17 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-07 16:18:51 +03:00
|
|
|
};
|
2018-03-03 22:15:17 +03:00
|
|
|
}
|
|
|
|
|
2018-03-10 01:29:05 +03:00
|
|
|
let computed_value_type = cg::fmap_trait_output(
|
|
|
|
&input,
|
2018-11-01 15:41:10 +03:00
|
|
|
&parse_quote!(crate::values::computed::ToComputedValue),
|
2018-11-30 14:28:20 +03:00
|
|
|
Ident::new("ComputedValue", Span::call_site()),
|
2018-03-10 01:29:05 +03:00
|
|
|
);
|
2017-05-21 16:48:10 +03:00
|
|
|
|
|
|
|
quote! {
|
2018-11-01 15:41:10 +03:00
|
|
|
impl #impl_generics crate::values::computed::ToComputedValue for #name #ty_generics #where_clause {
|
2017-05-21 16:48:10 +03:00
|
|
|
type ComputedValue = #computed_value_type;
|
|
|
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[inline]
|
2018-11-01 15:41:10 +03:00
|
|
|
fn to_computed_value(&self, context: &crate::values::computed::Context) -> Self::ComputedValue {
|
2017-05-21 16:48:10 +03:00
|
|
|
match *self {
|
|
|
|
#to_body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
|
|
|
match *computed {
|
|
|
|
#from_body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-28 18:13:13 +03:00
|
|
|
|
|
|
|
#[darling(attributes(compute), default)]
|
|
|
|
#[derive(Default, FromField)]
|
|
|
|
struct ComputedValueAttrs {
|
2018-03-10 01:29:05 +03:00
|
|
|
field_bound: bool,
|
2017-08-28 18:13:13 +03:00
|
|
|
}
|