async-graphql/derive/src/args.rs

800 lines
20 KiB
Rust
Raw Normal View History

2021-11-19 10:49:37 +00:00
use std::fmt::{self, Display, Formatter};
2022-04-19 04:25:11 +00:00
use darling::{
ast::{Data, Fields},
util::{Ignored, SpannedValue},
FromDeriveInput, FromField, FromMeta, FromVariant,
};
2020-10-14 09:08:57 +00:00
use inflector::Inflector;
use syn::{
Attribute, Generics, Ident, Lit, LitBool, LitStr, Meta, NestedMeta, Path, Type, Visibility,
};
2020-03-22 08:45:59 +00:00
2021-11-14 13:09:14 +00:00
use crate::validators::Validators;
#[derive(FromMeta, Clone)]
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-03-22 08:45:59 +00:00
pub struct CacheControl {
2020-09-28 09:44:00 +00:00
public: bool,
private: bool,
2020-03-22 08:45:59 +00:00
pub max_age: usize,
}
impl Default for CacheControl {
fn default() -> Self {
Self {
public: true,
2020-09-28 09:44:00 +00:00
private: false,
2020-03-22 08:45:59 +00:00
max_age: 0,
}
}
}
impl CacheControl {
2020-09-28 09:44:00 +00:00
pub fn is_public(&self) -> bool {
!self.private && self.public
}
}
#[derive(Debug)]
pub enum DefaultValue {
Default,
Value(Lit),
}
impl FromMeta for DefaultValue {
fn from_word() -> darling::Result<Self> {
Ok(DefaultValue::Default)
}
2020-03-22 08:45:59 +00:00
2020-09-28 09:44:00 +00:00
fn from_value(value: &Lit) -> darling::Result<Self> {
Ok(DefaultValue::Value(value.clone()))
2020-03-22 08:45:59 +00:00
}
}
2020-03-01 10:54:34 +00:00
#[derive(Debug, Clone)]
pub enum Visible {
None,
HiddenAlways,
2021-07-18 12:14:22 +00:00
FnName(Path),
}
impl FromMeta for Visible {
fn from_value(value: &Lit) -> darling::Result<Self> {
match value {
Lit::Bool(LitBool { value: true, .. }) => Ok(Visible::None),
Lit::Bool(LitBool { value: false, .. }) => Ok(Visible::HiddenAlways),
2021-07-18 12:14:22 +00:00
Lit::Str(str) => Ok(Visible::FnName(syn::parse_str::<Path>(&str.value())?)),
_ => Err(darling::Error::unexpected_lit_type(value)),
}
}
}
pub struct PathList(pub Vec<Path>);
impl FromMeta for PathList {
fn from_list(items: &[NestedMeta]) -> darling::Result<Self> {
let mut res = Vec::new();
for item in items {
if let NestedMeta::Meta(Meta::Path(p)) = item {
res.push(p.clone());
} else {
return Err(darling::Error::custom("Invalid path list"));
}
}
Ok(PathList(res))
}
}
#[derive(FromMeta)]
pub struct ConcreteType {
pub name: String,
pub params: PathList,
}
#[derive(Debug, Clone)]
pub enum Deprecation {
NoDeprecated,
Deprecated { reason: Option<String> },
}
impl Default for Deprecation {
fn default() -> Self {
Deprecation::NoDeprecated
}
}
impl FromMeta for Deprecation {
fn from_word() -> darling::Result<Self> {
Ok(Deprecation::Deprecated { reason: None })
}
fn from_value(value: &Lit) -> darling::Result<Self> {
match value {
Lit::Bool(LitBool { value: true, .. }) => Ok(Deprecation::Deprecated { reason: None }),
Lit::Bool(LitBool { value: false, .. }) => Ok(Deprecation::NoDeprecated),
Lit::Str(str) => Ok(Deprecation::Deprecated {
reason: Some(str.value()),
}),
_ => Err(darling::Error::unexpected_lit_type(value)),
}
}
}
#[derive(FromField)]
2020-09-28 09:44:00 +00:00
#[darling(attributes(graphql), forward_attrs(doc))]
pub struct SimpleObjectField {
pub ident: Option<Ident>,
pub ty: Type,
pub vis: Visibility,
pub attrs: Vec<Attribute>,
#[darling(default)]
pub skip: bool,
#[darling(default)]
pub skip_output: bool,
// for InputObject
#[darling(default)]
pub skip_input: bool,
#[darling(default)]
2020-03-01 10:54:34 +00:00
pub name: Option<String>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
pub deprecation: Deprecation,
2020-09-28 09:44:00 +00:00
#[darling(default)]
pub owned: bool,
#[darling(default)]
2020-03-22 08:45:59 +00:00
pub cache_control: CacheControl,
2020-09-28 09:44:00 +00:00
#[darling(default)]
pub external: bool,
#[darling(default)]
pub provides: Option<String>,
#[darling(default)]
pub requires: Option<String>,
#[darling(default)]
2021-11-16 02:22:39 +00:00
pub guard: Option<SpannedValue<String>>,
#[darling(default)]
pub visible: Option<Visible>,
2021-10-25 10:56:33 +00:00
#[darling(default, multiple)]
pub derived: Vec<DerivedField>,
2022-04-06 07:10:56 +00:00
#[darling(default)]
pub process_with: Option<String>,
// for InputObject
#[darling(default)]
pub default: Option<DefaultValue>,
#[darling(default)]
pub default_with: Option<LitStr>,
#[darling(default)]
2021-11-14 13:09:14 +00:00
pub validator: Option<Validators>,
#[darling(default)]
pub flatten: bool,
#[darling(default)]
pub secret: bool,
2020-03-01 10:54:34 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromDeriveInput)]
#[darling(attributes(graphql), forward_attrs(doc))]
pub struct SimpleObject {
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
pub data: Data<Ignored, SimpleObjectField>,
2020-03-01 10:54:34 +00:00
2020-09-28 09:44:00 +00:00
#[darling(default)]
pub internal: bool,
#[darling(default)]
pub fake: bool,
#[darling(default)]
2021-03-17 12:51:30 +00:00
pub complex: bool,
#[darling(default)]
2020-09-28 09:44:00 +00:00
pub name: Option<String>,
#[darling(default)]
2022-04-19 04:25:11 +00:00
pub name_type: bool,
#[darling(default)]
2020-10-14 09:08:57 +00:00
pub rename_fields: Option<RenameRule>,
#[darling(default)]
pub rename_args: Option<RenameRule>,
#[darling(default)]
2020-09-28 09:44:00 +00:00
pub cache_control: CacheControl,
#[darling(default)]
pub extends: bool,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default, multiple, rename = "concrete")]
pub concretes: Vec<ConcreteType>,
#[darling(default)]
pub serial: bool,
// for InputObject
#[darling(default)]
pub input_name: Option<String>,
#[darling(default)]
pub guard: Option<SpannedValue<String>>,
2020-03-01 10:54:34 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromMeta, Default)]
#[darling(default)]
2020-03-01 10:54:34 +00:00
pub struct Argument {
2020-03-05 06:23:55 +00:00
pub name: Option<String>,
2020-03-01 10:54:34 +00:00
pub desc: Option<String>,
2020-09-28 09:44:00 +00:00
pub default: Option<DefaultValue>,
pub default_with: Option<LitStr>,
2021-11-14 13:09:14 +00:00
pub validator: Option<Validators>,
2022-04-06 07:10:56 +00:00
#[darling(default)]
pub process_with: Option<String>,
pub key: bool, // for entity
pub visible: Option<Visible>,
pub secret: bool,
2020-03-01 10:54:34 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct Object {
pub internal: bool,
pub name: Option<String>,
2022-04-19 04:25:11 +00:00
pub name_type: bool,
2020-10-14 09:08:57 +00:00
pub rename_fields: Option<RenameRule>,
pub rename_args: Option<RenameRule>,
2020-09-28 09:44:00 +00:00
pub cache_control: CacheControl,
pub extends: bool,
pub use_type_description: bool,
pub visible: Option<Visible>,
pub serial: bool,
#[darling(multiple, rename = "concrete")]
pub concretes: Vec<ConcreteType>,
#[darling(default)]
pub guard: Option<SpannedValue<String>>,
2020-03-05 06:23:55 +00:00
}
2020-12-18 06:59:37 +00:00
pub enum ComplexityType {
Const(usize),
Fn(String),
}
impl FromMeta for ComplexityType {
fn from_value(value: &Lit) -> darling::Result<Self> {
match value {
Lit::Int(n) => {
let n = n.base10_parse::<i32>().unwrap();
if n < 0 {
return Err(darling::Error::custom(
"The complexity must be greater than or equal to 0.",
));
}
Ok(ComplexityType::Const(n as usize))
}
Lit::Str(s) => Ok(ComplexityType::Fn(s.value())),
_ => Err(darling::Error::unexpected_lit_type(value)),
}
}
}
2020-09-28 09:44:00 +00:00
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct ObjectField {
pub skip: bool,
pub entity: bool,
2020-03-05 06:23:55 +00:00
pub name: Option<String>,
pub deprecation: Deprecation,
2020-03-22 08:45:59 +00:00
pub cache_control: CacheControl,
2020-04-09 14:03:09 +00:00
pub external: bool,
pub provides: Option<String>,
pub requires: Option<String>,
2021-11-16 02:22:39 +00:00
pub guard: Option<SpannedValue<String>>,
pub visible: Option<Visible>,
2020-12-18 06:59:37 +00:00
pub complexity: Option<ComplexityType>,
#[darling(default, multiple)]
pub derived: Vec<DerivedField>,
pub flatten: bool,
}
#[derive(FromMeta, Default, Clone)]
#[darling(default)]
/// Derivied fields arguments: are used to generate derivied fields.
pub struct DerivedField {
pub name: Option<Ident>,
pub into: Option<String>,
2021-10-28 10:22:39 +00:00
pub with: Option<Path>,
#[darling(default)]
pub owned: Option<bool>,
2020-03-01 10:54:34 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromDeriveInput)]
#[darling(attributes(graphql), forward_attrs(doc))]
2020-03-01 10:54:34 +00:00
pub struct Enum {
2020-09-28 09:44:00 +00:00
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
pub data: Data<EnumItem, Ignored>,
#[darling(default)]
2020-03-02 00:24:49 +00:00
pub internal: bool,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-03-01 10:54:34 +00:00
pub name: Option<String>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-10-14 09:08:57 +00:00
pub rename_items: Option<RenameRule>,
#[darling(default)]
pub remote: Option<String>,
#[darling(default)]
pub visible: Option<Visible>,
2020-03-01 10:54:34 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromVariant)]
#[darling(attributes(graphql), forward_attrs(doc))]
2020-03-01 10:54:34 +00:00
pub struct EnumItem {
2020-09-28 09:44:00 +00:00
pub ident: Ident,
pub attrs: Vec<Attribute>,
pub fields: Fields<Ignored>,
#[darling(default)]
2020-03-01 10:54:34 +00:00
pub name: Option<String>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
pub deprecation: Deprecation,
#[darling(default)]
pub visible: Option<Visible>,
2020-03-01 10:54:34 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromDeriveInput)]
#[darling(attributes(graphql), forward_attrs(doc))]
pub struct Union {
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
pub data: Data<UnionItem, Ignored>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
pub internal: bool,
#[darling(default)]
pub name: Option<String>,
#[darling(default)]
pub visible: Option<Visible>,
2020-03-01 10:54:34 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromVariant)]
#[darling(attributes(graphql))]
pub struct UnionItem {
2020-09-28 09:44:00 +00:00
pub ident: Ident,
pub fields: Fields<syn::Type>,
#[darling(default)]
pub flatten: bool,
}
2020-09-28 09:44:00 +00:00
#[derive(FromField)]
#[darling(attributes(graphql), forward_attrs(doc))]
pub struct InputObjectField {
pub ident: Option<Ident>,
pub ty: Type,
pub vis: Visibility,
pub attrs: Vec<Attribute>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-03-01 10:54:34 +00:00
pub name: Option<String>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
pub default: Option<DefaultValue>,
#[darling(default)]
pub default_with: Option<LitStr>,
#[darling(default)]
2021-11-14 13:09:14 +00:00
pub validator: Option<Validators>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
pub flatten: bool,
#[darling(default)]
pub skip: bool,
#[darling(default)]
pub skip_input: bool,
#[darling(default)]
pub process_with: Option<String>,
// for SimpleObject
#[darling(default)]
pub skip_output: bool,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default)]
pub secret: bool,
2020-03-01 10:54:34 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromDeriveInput)]
#[darling(attributes(graphql), forward_attrs(doc))]
2020-03-03 03:48:00 +00:00
pub struct InputObject {
2020-09-28 09:44:00 +00:00
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
pub data: Data<Ignored, InputObjectField>,
#[darling(default)]
2020-03-03 03:48:00 +00:00
pub internal: bool,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-03-03 03:48:00 +00:00
pub name: Option<String>,
2020-10-14 09:08:57 +00:00
#[darling(default)]
pub input_name: Option<String>,
#[darling(default)]
2020-10-14 09:08:57 +00:00
pub rename_fields: Option<RenameRule>,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default, multiple, rename = "concrete")]
pub concretes: Vec<ConcreteType>,
// for SimpleObject
#[darling(default)]
pub complex: bool,
2020-03-01 10:54:34 +00:00
}
2020-03-06 15:58:43 +00:00
#[derive(FromVariant)]
#[darling(attributes(graphql), forward_attrs(doc))]
pub struct OneofObjectField {
pub ident: Ident,
pub attrs: Vec<Attribute>,
pub fields: Fields<syn::Type>,
#[darling(default)]
pub name: Option<String>,
#[darling(default)]
pub validator: Option<Validators>,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default)]
pub secret: bool,
}
#[derive(FromDeriveInput)]
#[darling(attributes(graphql), forward_attrs(doc))]
pub struct OneofObject {
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
pub data: Data<OneofObjectField, Ignored>,
#[darling(default)]
pub internal: bool,
#[darling(default)]
pub name: Option<String>,
#[darling(default)]
pub rename_fields: Option<RenameRule>,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default, multiple, rename = "concrete")]
pub concretes: Vec<ConcreteType>,
}
2020-09-28 09:44:00 +00:00
#[derive(FromMeta)]
2020-03-06 15:58:43 +00:00
pub struct InterfaceFieldArgument {
pub name: String,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-03-06 15:58:43 +00:00
pub desc: Option<String>,
2020-09-28 09:44:00 +00:00
#[darling(rename = "type")]
pub ty: LitStr,
#[darling(default)]
pub default: Option<DefaultValue>,
#[darling(default)]
pub default_with: Option<LitStr>,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default)]
pub secret: bool,
2020-03-06 15:58:43 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromMeta)]
2020-03-06 15:58:43 +00:00
pub struct InterfaceField {
pub name: SpannedValue<String>,
2020-09-28 09:44:00 +00:00
#[darling(rename = "type")]
pub ty: LitStr,
#[darling(default)]
pub method: Option<String>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-03-06 15:58:43 +00:00
pub desc: Option<String>,
2020-09-28 09:44:00 +00:00
#[darling(default, multiple, rename = "arg")]
2020-03-06 15:58:43 +00:00
pub args: Vec<InterfaceFieldArgument>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
pub deprecation: Deprecation,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-04-09 14:03:09 +00:00
pub external: bool,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-04-09 14:03:09 +00:00
pub provides: Option<String>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-04-09 14:03:09 +00:00
pub requires: Option<String>,
#[darling(default)]
pub visible: Option<Visible>,
2020-03-06 15:58:43 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromVariant)]
pub struct InterfaceMember {
pub ident: Ident,
pub fields: Fields<syn::Type>,
2020-03-06 15:58:43 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromDeriveInput)]
#[darling(attributes(graphql), forward_attrs(doc))]
2020-03-06 15:58:43 +00:00
pub struct Interface {
2020-09-28 09:44:00 +00:00
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
pub data: Data<InterfaceMember, Ignored>,
#[darling(default)]
2020-03-06 15:58:43 +00:00
pub internal: bool,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-03-06 15:58:43 +00:00
pub name: Option<String>,
2020-10-14 09:08:57 +00:00
#[darling(default)]
pub rename_fields: Option<RenameRule>,
#[darling(default)]
pub rename_args: Option<RenameRule>,
2020-09-28 09:44:00 +00:00
#[darling(default, multiple, rename = "field")]
2020-03-06 15:58:43 +00:00
pub fields: Vec<InterfaceField>,
2020-09-28 09:44:00 +00:00
#[darling(default)]
2020-04-09 14:03:09 +00:00
pub extends: bool,
#[darling(default)]
pub visible: Option<Visible>,
2020-03-06 15:58:43 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct Scalar {
pub internal: bool,
pub name: Option<String>,
pub use_type_description: bool,
pub visible: Option<Visible>,
pub specified_by_url: Option<String>,
2020-03-06 15:58:43 +00:00
}
2020-04-17 03:06:33 +00:00
2020-09-28 09:44:00 +00:00
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct Subscription {
pub internal: bool,
pub name: Option<String>,
2020-10-14 09:08:57 +00:00
pub rename_fields: Option<RenameRule>,
pub rename_args: Option<RenameRule>,
pub use_type_description: bool,
pub extends: bool,
pub visible: Option<Visible>,
#[darling(default)]
pub guard: Option<SpannedValue<String>>,
2020-09-28 09:44:00 +00:00
}
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct SubscriptionFieldArgument {
pub name: Option<String>,
pub desc: Option<String>,
2020-09-28 09:44:00 +00:00
pub default: Option<DefaultValue>,
pub default_with: Option<LitStr>,
2021-11-14 13:09:14 +00:00
pub validator: Option<Validators>,
2022-04-06 07:10:56 +00:00
#[darling(default)]
pub process_with: Option<String>,
pub visible: Option<Visible>,
pub secret: bool,
}
2020-09-28 09:44:00 +00:00
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct SubscriptionField {
pub skip: bool,
pub name: Option<String>,
pub deprecation: Deprecation,
2021-11-16 02:22:39 +00:00
pub guard: Option<SpannedValue<String>>,
pub visible: Option<Visible>,
pub complexity: Option<ComplexityType>,
2020-09-28 09:44:00 +00:00
}
2020-09-28 09:44:00 +00:00
#[derive(FromField)]
pub struct MergedObjectField {
pub ty: Type,
}
2020-05-01 23:57:34 +00:00
2020-09-28 09:44:00 +00:00
#[derive(FromDeriveInput)]
#[darling(attributes(graphql), forward_attrs(doc))]
pub struct MergedObject {
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
pub data: Data<Ignored, MergedObjectField>,
#[darling(default)]
pub internal: bool,
#[darling(default)]
pub name: Option<String>,
#[darling(default)]
pub cache_control: CacheControl,
#[darling(default)]
pub extends: bool,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default)]
pub serial: bool,
2020-09-28 09:44:00 +00:00
}
#[derive(FromField)]
pub struct MergedSubscriptionField {
pub ty: Type,
}
#[derive(FromDeriveInput)]
#[darling(attributes(graphql), forward_attrs(doc))]
pub struct MergedSubscription {
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
pub data: Data<Ignored, MergedSubscriptionField>,
#[darling(default)]
pub internal: bool,
#[darling(default)]
pub name: Option<String>,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default)]
pub extends: bool,
2020-05-01 23:57:34 +00:00
}
2020-10-14 09:08:57 +00:00
2020-10-16 02:45:27 +00:00
#[derive(Debug, Copy, Clone, FromMeta)]
2020-10-14 09:08:57 +00:00
pub enum RenameRule {
2020-10-16 02:45:27 +00:00
#[darling(rename = "lowercase")]
2020-10-14 09:08:57 +00:00
Lower,
2020-10-16 02:45:27 +00:00
#[darling(rename = "UPPERCASE")]
2020-10-14 09:08:57 +00:00
Upper,
2020-10-16 02:45:27 +00:00
#[darling(rename = "PascalCase")]
2020-10-14 09:08:57 +00:00
Pascal,
2020-10-16 02:45:27 +00:00
#[darling(rename = "camelCase")]
2020-10-14 09:08:57 +00:00
Camel,
2020-10-16 02:45:27 +00:00
#[darling(rename = "snake_case")]
2020-10-14 09:08:57 +00:00
Snake,
2020-10-16 02:45:27 +00:00
#[darling(rename = "SCREAMING_SNAKE_CASE")]
2020-10-14 09:08:57 +00:00
ScreamingSnake,
}
impl RenameRule {
fn rename(&self, name: impl AsRef<str>) -> String {
match self {
Self::Lower => name.as_ref().to_lowercase(),
Self::Upper => name.as_ref().to_uppercase(),
Self::Pascal => name.as_ref().to_pascal_case(),
Self::Camel => name.as_ref().to_camel_case(),
Self::Snake => name.as_ref().to_snake_case(),
Self::ScreamingSnake => name.as_ref().to_screaming_snake_case(),
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum RenameTarget {
Type,
EnumItem,
Field,
Argument,
}
impl RenameTarget {
fn rule(&self) -> RenameRule {
match self {
RenameTarget::Type => RenameRule::Pascal,
RenameTarget::EnumItem => RenameRule::ScreamingSnake,
RenameTarget::Field => RenameRule::Camel,
RenameTarget::Argument => RenameRule::Camel,
}
}
pub fn rename(&self, name: impl AsRef<str>) -> String {
self.rule().rename(name)
}
}
pub trait RenameRuleExt {
fn rename(&self, name: impl AsRef<str>, target: RenameTarget) -> String;
}
impl RenameRuleExt for Option<RenameRule> {
fn rename(&self, name: impl AsRef<str>, target: RenameTarget) -> String {
self.unwrap_or(target.rule()).rename(name)
}
}
#[derive(FromDeriveInput)]
#[darling(forward_attrs(doc))]
pub struct Description {
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
#[darling(default)]
pub internal: bool,
}
2021-01-15 02:29:03 +00:00
#[derive(Debug)]
pub enum NewTypeName {
2021-07-31 15:54:16 +00:00
New(String),
Rust,
Original,
}
impl Default for NewTypeName {
fn default() -> Self {
2021-07-31 15:54:16 +00:00
Self::Original
}
}
impl FromMeta for NewTypeName {
fn from_word() -> darling::Result<Self> {
2021-07-31 15:54:16 +00:00
Ok(Self::Rust)
}
fn from_string(value: &str) -> darling::Result<Self> {
2021-07-31 15:54:16 +00:00
Ok(Self::New(value.to_string()))
}
fn from_bool(value: bool) -> darling::Result<Self> {
if value {
2021-07-31 15:54:16 +00:00
Ok(Self::Rust)
} else {
2021-07-31 15:54:16 +00:00
Ok(Self::Original)
}
}
}
2021-01-15 02:29:03 +00:00
#[derive(FromDeriveInput)]
#[darling(attributes(graphql), forward_attrs(doc))]
2021-01-15 02:29:03 +00:00
pub struct NewType {
pub ident: Ident,
pub generics: Generics,
pub attrs: Vec<Attribute>,
2021-01-15 02:29:03 +00:00
pub data: Data<Ignored, syn::Type>,
#[darling(default)]
pub internal: bool,
#[darling(default)]
pub name: NewTypeName,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default)]
pub specified_by_url: Option<String>,
2021-01-15 02:29:03 +00:00
}
2021-03-17 12:51:30 +00:00
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct ComplexObject {
pub internal: bool,
pub name: Option<String>,
pub rename_fields: Option<RenameRule>,
pub rename_args: Option<RenameRule>,
pub guard: Option<SpannedValue<String>>,
2021-03-17 12:51:30 +00:00
}
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct ComplexObjectField {
pub skip: bool,
pub name: Option<String>,
pub deprecation: Deprecation,
pub cache_control: CacheControl,
pub external: bool,
pub provides: Option<String>,
pub requires: Option<String>,
2021-11-16 02:22:39 +00:00
pub guard: Option<SpannedValue<String>>,
2021-03-17 12:51:30 +00:00
pub visible: Option<Visible>,
pub complexity: Option<ComplexityType>,
#[darling(multiple)]
pub derived: Vec<DerivedField>,
pub flatten: bool,
2021-03-17 12:51:30 +00:00
}
2021-11-19 10:49:37 +00:00
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct Directive {
pub internal: bool,
pub name: Option<String>,
pub visible: Option<Visible>,
pub repeatable: bool,
pub rename_args: Option<RenameRule>,
#[darling(multiple, rename = "location")]
pub locations: Vec<DirectiveLocation>,
}
#[derive(Debug, Copy, Clone, FromMeta)]
#[darling(rename_all = "lowercase")]
pub enum DirectiveLocation {
Field,
}
impl Display for DirectiveLocation {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
DirectiveLocation::Field => write!(f, "FIELD"),
}
}
}