juniper_codegen/common/deprecation.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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
//! Common functions, definitions and extensions for parsing and code generation
//! of [GraphQL deprecation directive][0].
//!
//! [0]: https://spec.graphql.org/October2021#sec--deprecated
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
spanned::Spanned as _,
token,
};
use crate::common::{parse::ParseBufferExt as _, SpanContainer};
/// [GraphQL deprecation directive][0] defined on a [GraphQL field][1] or a
/// [GraphQL enum value][2] via `#[graphql(deprecated = ...)]` (or
/// `#[deprecated(note = ...)]`) attribute.
///
/// [0]: https://spec.graphql.org/October2021#sec--deprecated
/// [1]: https://spec.graphql.org/October2021#sec-Language.Fields
/// [2]: https://spec.graphql.org/October2021#sec-Enum-Value
#[derive(Debug, Default, Eq, PartialEq)]
pub(crate) struct Directive {
/// Optional [reason][1] attached to this [deprecation][0].
///
/// [0]: https://spec.graphql.org/October2021#sec--deprecated
/// [1]: https://spec.graphql.org/October2021#sel-GAHnBZDACEDDGAA_6L
pub(crate) reason: Option<syn::LitStr>,
}
impl Parse for Directive {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
Ok(Self {
reason: input
.try_parse::<token::Eq>()?
.map(|_| input.parse::<syn::LitStr>())
.transpose()?,
})
}
}
impl Directive {
/// Tries to parse a [`Directive`] from a `#[deprecated(note = ...)]`
/// attribute, by looking up for it in the provided [`syn::Attribute`]s.
///
/// # Errors
///
/// If failed to parse a [`Directive`] from a found
/// `#[deprecated(note = ...)]` attribute.
pub(crate) fn parse_from_deprecated_attr(
attrs: &[syn::Attribute],
) -> syn::Result<Option<SpanContainer<Self>>> {
for attr in attrs {
return Ok(match &attr.meta {
syn::Meta::List(list) if list.path.is_ident("deprecated") => {
let directive = Self::parse_from_deprecated_meta_list(list)?;
Some(SpanContainer::new(
list.path.span(),
directive.reason.as_ref().map(|r| r.span()),
directive,
))
}
syn::Meta::Path(path) if path.is_ident("deprecated") => {
Some(SpanContainer::new(path.span(), None, Self::default()))
}
_ => continue,
});
}
Ok(None)
}
/// Tries to parse a [`Directive`] from the [`syn::MetaList`] of a single
/// `#[deprecated(note = ...)]` attribute.
///
/// # Errors
///
/// If the `#[deprecated(note = ...)]` attribute has incorrect format.
fn parse_from_deprecated_meta_list(list: &syn::MetaList) -> syn::Result<Self> {
for meta in list.parse_args_with(Punctuated::<syn::Meta, token::Comma>::parse_terminated)? {
if let syn::Meta::NameValue(nv) = meta {
return if !nv.path.is_ident("note") {
Err(syn::Error::new(
nv.path.span(),
"unrecognized setting on #[deprecated(..)] attribute",
))
} else if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(strlit),
..
}) = &nv.value
{
Ok(Self {
reason: Some(strlit.clone()),
})
} else {
Err(syn::Error::new(
nv.value.span(),
"only strings are allowed for deprecation",
))
};
}
}
Ok(Self::default())
}
}
impl ToTokens for Directive {
fn to_tokens(&self, into: &mut TokenStream) {
let reason = self
.reason
.as_ref()
.map_or_else(|| quote! { None }, |text| quote! { Some(#text) });
quote! {
.deprecated(::core::option::Option::#reason)
}
.to_tokens(into);
}
}
#[cfg(test)]
mod parse_from_deprecated_attr_test {
use quote::quote;
use syn::parse_quote;
use super::Directive;
#[test]
fn single() {
let desc =
Directive::parse_from_deprecated_attr(&[parse_quote! { #[deprecated(note = "foo")] }])
.unwrap()
.unwrap()
.into_inner();
assert_eq!(
quote! { #desc }.to_string(),
quote! { .deprecated(::core::option::Option::Some("foo")) }.to_string(),
);
}
#[test]
fn no_reason() {
let desc = Directive::parse_from_deprecated_attr(&[parse_quote! { #[deprecated] }])
.unwrap()
.unwrap()
.into_inner();
assert_eq!(
quote! { #desc }.to_string(),
quote! { .deprecated(::core::option::Option::None) }.to_string(),
);
}
#[test]
fn not_deprecation() {
let desc =
Directive::parse_from_deprecated_attr(&[parse_quote! { #[blah = "foo"] }]).unwrap();
assert_eq!(desc, None);
}
}