2020-05-19 05:27:01 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2020-11-22 01:53:18 +00:00
|
|
|
use syn::ItemImpl;
|
2020-05-19 05:27:01 +00:00
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
use crate::{
|
|
|
|
args::{self, RenameTarget},
|
|
|
|
utils::{get_crate_name, get_rustdoc, get_type_path_and_name, visible_fn, GeneratorResult},
|
2020-12-11 08:03:28 +00:00
|
|
|
};
|
2020-10-16 02:45:27 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
pub fn generate(
|
|
|
|
scalar_args: &args::Scalar,
|
|
|
|
item_impl: &mut ItemImpl,
|
|
|
|
) -> GeneratorResult<TokenStream> {
|
2020-10-26 13:34:05 +00:00
|
|
|
let crate_name = get_crate_name(scalar_args.internal);
|
2020-11-22 01:53:18 +00:00
|
|
|
let self_name = get_type_path_and_name(item_impl.self_ty.as_ref())?.1;
|
2020-05-19 05:27:01 +00:00
|
|
|
let gql_typename = scalar_args
|
|
|
|
.name
|
|
|
|
.clone()
|
2020-10-14 09:08:57 +00:00
|
|
|
.unwrap_or_else(|| RenameTarget::Type.rename(self_name.clone()));
|
2020-10-26 13:34:05 +00:00
|
|
|
|
|
|
|
let desc = if scalar_args.use_type_description {
|
|
|
|
quote! { ::std::option::Option::Some(<Self as #crate_name::Description>::description()) }
|
|
|
|
} else {
|
|
|
|
get_rustdoc(&item_impl.attrs)?
|
|
|
|
.map(|s| quote!(::std::option::Option::Some(#s)))
|
|
|
|
.unwrap_or_else(|| quote!(::std::option::Option::None))
|
|
|
|
};
|
|
|
|
|
2020-05-19 05:27:01 +00:00
|
|
|
let self_ty = &item_impl.self_ty;
|
|
|
|
let generic = &item_impl.generics;
|
|
|
|
let where_clause = &item_impl.generics.where_clause;
|
2020-12-11 08:03:28 +00:00
|
|
|
let visible = visible_fn(&scalar_args.visible);
|
2021-10-28 07:21:42 +00:00
|
|
|
let specified_by_url = match &scalar_args.specified_by_url {
|
|
|
|
Some(specified_by_url) => quote! { ::std::option::Option::Some(#specified_by_url) },
|
|
|
|
None => quote! { ::std::option::Option::None },
|
|
|
|
};
|
|
|
|
|
2020-05-19 05:27:01 +00:00
|
|
|
let expanded = quote! {
|
|
|
|
#item_impl
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2021-11-09 10:52:10 +00:00
|
|
|
impl #generic #crate_name::InputType for #self_ty #where_clause {
|
2021-11-18 07:43:12 +00:00
|
|
|
type RawValueType = Self;
|
|
|
|
|
2020-10-16 10:37:59 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
2020-06-24 06:04:48 +00:00
|
|
|
::std::borrow::Cow::Borrowed(#gql_typename)
|
2020-05-19 05:27:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 10:37:59 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
2022-03-30 12:54:49 +00:00
|
|
|
registry.create_input_type::<#self_ty, _>(#crate_name::registry::MetaTypeId::Scalar, |_| #crate_name::registry::MetaType::Scalar {
|
2020-10-16 19:21:46 +00:00
|
|
|
name: ::std::borrow::ToOwned::to_owned(#gql_typename),
|
2020-05-19 05:27:01 +00:00
|
|
|
description: #desc,
|
|
|
|
is_valid: |value| <#self_ty as #crate_name::ScalarType>::is_valid(value),
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: #visible,
|
2021-10-28 07:21:42 +00:00
|
|
|
specified_by_url: #specified_by_url,
|
2020-05-19 05:27:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:37:59 +00:00
|
|
|
fn parse(value: ::std::option::Option<#crate_name::Value>) -> #crate_name::InputValueResult<Self> {
|
2020-05-28 07:00:55 +00:00
|
|
|
<#self_ty as #crate_name::ScalarType>::parse(value.unwrap_or_default())
|
2020-05-19 05:27:01 +00:00
|
|
|
}
|
2020-05-26 10:34:43 +00:00
|
|
|
|
2020-05-27 19:16:53 +00:00
|
|
|
fn to_value(&self) -> #crate_name::Value {
|
2020-05-26 10:34:43 +00:00
|
|
|
<#self_ty as #crate_name::ScalarType>::to_value(self)
|
|
|
|
}
|
2021-11-18 07:43:12 +00:00
|
|
|
|
|
|
|
fn as_raw_value(&self) -> ::std::option::Option<&Self::RawValueType> {
|
|
|
|
::std::option::Option::Some(self)
|
|
|
|
}
|
2020-05-19 05:27:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-05-19 05:27:01 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-12-11 07:37:50 +00:00
|
|
|
impl #generic #crate_name::OutputType for #self_ty #where_clause {
|
2021-11-09 10:52:10 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
|
|
|
::std::borrow::Cow::Borrowed(#gql_typename)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
2022-03-30 12:54:49 +00:00
|
|
|
registry.create_output_type::<#self_ty, _>(#crate_name::registry::MetaTypeId::Scalar, |_| #crate_name::registry::MetaType::Scalar {
|
2021-11-09 10:52:10 +00:00
|
|
|
name: ::std::borrow::ToOwned::to_owned(#gql_typename),
|
|
|
|
description: #desc,
|
|
|
|
is_valid: |value| <#self_ty as #crate_name::ScalarType>::is_valid(value),
|
|
|
|
visible: #visible,
|
|
|
|
specified_by_url: #specified_by_url,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:27:01 +00:00
|
|
|
async fn resolve(
|
|
|
|
&self,
|
|
|
|
_: &#crate_name::ContextSelectionSet<'_>,
|
2020-09-06 05:38:31 +00:00
|
|
|
_field: &#crate_name::Positioned<#crate_name::parser::types::Field>
|
2021-06-07 12:51:20 +00:00
|
|
|
) -> #crate_name::ServerResult<#crate_name::Value> {
|
|
|
|
::std::result::Result::Ok(#crate_name::ScalarType::to_value(self))
|
2020-05-19 05:27:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|