/// Generate code for the juniper::graphql_scalar proc macro.
Generate code for the juniper::graphql_scalar proc macro.
Generate code for the juniper :: graphql_scalar proc macro .
pub fn build_scalar(attributes: TokenStream, body: TokenStream, is_internal: bool) -> TokenStream {
    let attrs = match syn::parse::<util::FieldAttributes>(attributes) {
        Ok(attrs) => attrs,
        Err(e) => {
            panic!("Invalid attributes:\n{}", e);
        }
    };
    let input = syn::parse_macro_input!(body as ScalarCodegenInput);
    let impl_for_type = input
        .impl_for_type
        .expect("Unable to find target for implementation target for `GraphQLScalar`");
    let custom_data_type = input
        .custom_data_type
        .expect("Unable to find custom scalar data type");
    let resolve_body = input
        .resolve_body
        .expect("Unable to find body of `resolve` method");
    let from_input_value_arg = input
        .from_input_value_arg
        .expect("Unable to find argument for `from_input_value` method");
    let from_input_value_body = input
        .from_input_value_body
        .expect("Unable to find body of `from_input_value` method");
    let from_input_value_result = input
        .from_input_value_result
        .expect("Unable to find return type of `from_input_value` method");
    let from_str_arg = input
        .from_str_arg
        .expect("Unable to find argument for `from_str` method");
    let from_str_body = input
        .from_str_body
        .expect("Unable to find body of `from_str` method");
    let from_str_result = input
        .from_str_result
        .expect("Unable to find return type of `from_str` method");
    let name = attrs
        .name
        .unwrap_or_else(|| impl_for_type.ident.to_string());
    let crate_name = match is_internal {
        true => quote!(crate),
        _ => quote!(juniper),
    };
    let description = match attrs.description {
        Some(val) => quote!(.description(#val)),
        None => quote!(),
    };
    let async_generic_type = match input.custom_data_type_is_struct {
        true => quote!(__S),
        _ => quote!(#custom_data_type),
    };
    let async_generic_type_decl = match input.custom_data_type_is_struct {
        true => quote!(<#async_generic_type>),
        _ => quote!(),
    };
    let generic_type = match input.custom_data_type_is_struct {
        true => quote!(S),
        _ => quote!(#custom_data_type),
    };
    let generic_type_decl = match input.custom_data_type_is_struct {
        true => quote!(<#generic_type>),
        _ => quote!(),
    };
    let generic_type_bound = match input.custom_data_type_is_struct {
        true => quote!(where #generic_type: #crate_name::ScalarValue,),
        _ => quote!(),
    };
    let _async = quote!(
        impl#async_generic_type_decl #crate_name::GraphQLTypeAsync<#async_generic_type> for #impl_for_type
        where
            #async_generic_type: #crate_name::ScalarValue + Send + Sync,
            Self: #crate_name::GraphQLType<#async_generic_type> + Send + Sync,
            Self::Context: Send + Sync,
            Self::TypeInfo: Send + Sync,
        {
            fn resolve_async<'a>(
                &'a self,
                info: &'a Self::TypeInfo,
                selection_set: Option<&'a [#crate_name::Selection<#async_generic_type>]>,
                executor: &'a #crate_name::Executor<Self::Context, #async_generic_type>,
            ) -> #crate_name::BoxFuture<'a, #crate_name::ExecutionResult<#async_generic_type>> {
                use #crate_name::GraphQLType;
                use futures::future;
                let v = self.resolve(info, selection_set, executor);
                Box::pin(future::ready(v))
            }
        }
    );
    quote!(
        #_async
        impl#generic_type_decl #crate_name::GraphQLType<#generic_type> for #impl_for_type
        #generic_type_bound
        {
            type Context = ();
            type TypeInfo = ();
            fn name(_: &Self::TypeInfo) -> Option<&str> {
                Some(#name)
            }
            fn meta<'r>(
                info: &Self::TypeInfo,
                registry: &mut #crate_name::Registry<'r, #generic_type>,
            ) -> #crate_name::meta::MetaType<'r, #generic_type>
            where
                #generic_type: 'r,
            {
                registry.build_scalar_type::<Self>(info)
                    #description
                    .into_meta()
            }
            fn resolve(
                &self,
                info: &(),
                selection: Option<&[#crate_name::Selection<#generic_type>]>,
                executor: &#crate_name::Executor<Self::Context, #generic_type>,
            ) -> #crate_name::ExecutionResult<#generic_type> {
                Ok(#resolve_body)
            }
        }
        impl#generic_type_decl #crate_name::ToInputValue<#generic_type> for #impl_for_type
        #generic_type_bound
        {
            fn to_input_value(&self) -> #crate_name::InputValue<#generic_type> {
                let v = #resolve_body;
                #crate_name::ToInputValue::to_input_value(&v)
            }
        }
        impl#generic_type_decl #crate_name::FromInputValue<#generic_type> for #impl_for_type
        #generic_type_bound
        {
            fn from_input_value(#from_input_value_arg: &#crate_name::InputValue<#generic_type>) -> #from_input_value_result {
                #from_input_value_body
            }
        }
        impl#generic_type_decl #crate_name::ParseScalarValue<#generic_type> for #impl_for_type
        #generic_type_bound
            {
                fn from_str<'a>(
                    #from_str_arg: #crate_name::parser::ScalarToken<'a>,
                ) -> #from_str_result {
                #from_str_body
            }
        }
    ).into()
}
pub fn build_scalar ( attributes : TokenStream , body : TokenStream , is_internal : bool ) -> TokenStream { let attrs = match syn :: parse :: < util :: FieldAttributes > ( attributes ) { Ok ( attrs ) => attrs , Err ( e ) => { panic ! ( " \n " e ) ; } } ; let input = syn :: parse_macro_input ! ( body as ScalarCodegenInput ) ; let impl_for_type = input . impl_for_type . expect ( " " ) ; let custom_data_type = input . custom_data_type . expect ( " " ) ; let resolve_body = input . resolve_body . expect ( " " ) ; let from_input_value_arg = input . from_input_value_arg . expect ( " " ) ; let from_input_value_body = input . from_input_value_body . expect ( " " ) ; let from_input_value_result = input . from_input_value_result . expect ( " " ) ; let from_str_arg = input . from_str_arg . expect ( " " ) ; let from_str_body = input . from_str_body . expect ( " " ) ; let from_str_result = input . from_str_result . expect ( " " ) ; let name = attrs . name . unwrap_or_else ( | | impl_for_type . ident . to_string ( ) ) ; let crate_name = match is_internal { true => quote ! ( crate ) , _ => quote ! ( juniper ) , } ; let description = match attrs . description { Some ( val ) => quote ! ( description ( val ) ) , None => quote ! ( ) , } ; let async_generic_type = match input . custom_data_type_is_struct { true => quote ! ( __S ) , _ => quote ! ( custom_data_type ) , } ; let async_generic_type_decl = match input . custom_data_type_is_struct { true => quote ! ( async_generic_type ) , _ => quote ! ( ) , } ; let generic_type = match input . custom_data_type_is_struct { true => quote ! ( S ) , _ => quote ! ( custom_data_type ) , } ; let generic_type_decl = match input . custom_data_type_is_struct { true => quote ! ( generic_type ) , _ => quote ! ( ) , } ; let generic_type_bound = match input . custom_data_type_is_struct { true => quote ! ( where generic_type crate_name ScalarValue ) , _ => quote ! ( ) , } ; let _async = quote ! ( impl async_generic_type_decl crate_name GraphQLTypeAsync async_generic_type for impl_for_type where async_generic_type crate_name ScalarValue Send Sync Self crate_name GraphQLType async_generic_type Send Sync Self Context Send Sync Self TypeInfo Send Sync { fn resolve_async ' a ( ' a self info ' a Self TypeInfo selection_set Option ' a [ crate_name Selection async_generic_type ] executor ' a crate_name Executor Self Context async_generic_type ) crate_name BoxFuture ' a crate_name ExecutionResult async_generic_type { use crate_name GraphQLType use futures future let v self resolve ( info selection_set executor ) Box pin ( future ready ( v ) ) } } ) ; quote ! ( async impl generic_type_decl crate_name GraphQLType generic_type for impl_for_type generic_type_bound { type Context ( ) type TypeInfo ( ) fn name ( Self TypeInfo ) Option str { Some ( name ) } fn meta ' r ( info Self TypeInfo registry mut crate_name Registry ' r generic_type ) crate_name meta MetaType ' r generic_type where generic_type ' r { registry build_scalar_type Self ( info ) description into_meta ( ) } fn resolve ( self info ( ) selection Option [ crate_name Selection generic_type ] executor crate_name Executor Self Context generic_type ) crate_name ExecutionResult generic_type { Ok ( resolve_body ) } } impl generic_type_decl crate_name ToInputValue generic_type for impl_for_type generic_type_bound { fn to_input_value ( self ) crate_name InputValue generic_type { let v resolve_body crate_name ToInputValue to_input_value ( v ) } } impl generic_type_decl crate_name FromInputValue generic_type for impl_for_type generic_type_bound { fn from_input_value ( from_input_value_arg crate_name InputValue generic_type ) from_input_value_result { from_input_value_body } } impl generic_type_decl crate_name ParseScalarValue generic_type for impl_for_type generic_type_bound { fn from_str ' a ( from_str_arg crate_name parser ScalarToken ' a ) from_str_result { from_str_body } } ) . into ( ) }
Generate code for the juniper::graphql_scalar proc macro.
Generate code for the juniper :: graphql_scalar proc macro .
attributes
TokenStream
body
TokenStream
is_internal
bool