juniper_codegen/common/default.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
//! Common functions, definitions and extensions for parsing and code generation
//! of [GraphQL default values][0]
//!
//! [0]: https://spec.graphql.org/October2021#DefaultValue
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream},
token,
};
use crate::common::parse::ParseBufferExt as _;
/// Representation of a [GraphQL default value][0] for code generation.
///
/// [0]: https://spec.graphql.org/October2021#DefaultValue
#[derive(Clone, Debug, Default)]
pub(crate) enum Value {
/// [`Default`] implementation should be used.
#[default]
Default,
/// Explicit [`Expr`]ession to be used as the [default value][0].
///
/// [`Expr`]: syn::Expr
/// [0]: https://spec.graphql.org/October2021#DefaultValue
Expr(Box<syn::Expr>),
}
impl From<Option<syn::Expr>> for Value {
fn from(opt: Option<syn::Expr>) -> Self {
match opt {
Some(expr) => Self::Expr(Box::new(expr)),
None => Self::Default,
}
}
}
impl Parse for Value {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
Ok(input
.try_parse::<token::Eq>()?
.map(|_| input.parse::<syn::Expr>())
.transpose()?
.into())
}
}
impl ToTokens for Value {
fn to_tokens(&self, into: &mut TokenStream) {
match self {
Self::Default => quote! {
::std::default::Default::default()
},
Self::Expr(expr) => quote! {
(#expr).into()
},
}
.to_tokens(into)
}
}