From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- third_party/rust/darling_core/.cargo-checksum.json | 1 + third_party/rust/darling_core/Cargo.toml | 49 ++ third_party/rust/darling_core/LICENSE | 21 + third_party/rust/darling_core/src/ast/data.rs | 477 ++++++++++++ third_party/rust/darling_core/src/ast/generics.rs | 192 +++++ third_party/rust/darling_core/src/ast/mod.rs | 7 + .../darling_core/src/codegen/attr_extractor.rs | 116 +++ .../rust/darling_core/src/codegen/default_expr.rs | 53 ++ third_party/rust/darling_core/src/codegen/error.rs | 46 ++ third_party/rust/darling_core/src/codegen/field.rs | 199 +++++ .../src/codegen/from_attributes_impl.rs | 109 +++ .../darling_core/src/codegen/from_derive_impl.rs | 147 ++++ .../rust/darling_core/src/codegen/from_field.rs | 114 +++ .../darling_core/src/codegen/from_meta_impl.rs | 135 ++++ .../darling_core/src/codegen/from_type_param.rs | 113 +++ .../darling_core/src/codegen/from_variant_impl.rs | 139 ++++ third_party/rust/darling_core/src/codegen/mod.rs | 30 + .../darling_core/src/codegen/outer_from_impl.rs | 60 ++ .../darling_core/src/codegen/postfix_transform.rs | 30 + .../rust/darling_core/src/codegen/trait_impl.rs | 136 ++++ .../rust/darling_core/src/codegen/variant.rs | 158 ++++ .../rust/darling_core/src/codegen/variant_data.rs | 99 +++ third_party/rust/darling_core/src/derive.rs | 61 ++ third_party/rust/darling_core/src/error/kind.rs | 211 +++++ third_party/rust/darling_core/src/error/mod.rs | 831 ++++++++++++++++++++ .../rust/darling_core/src/from_attributes.rs | 27 + .../rust/darling_core/src/from_derive_input.rs | 26 + third_party/rust/darling_core/src/from_field.rs | 38 + .../rust/darling_core/src/from_generic_param.rs | 19 + third_party/rust/darling_core/src/from_generics.rs | 27 + third_party/rust/darling_core/src/from_meta.rs | 853 +++++++++++++++++++++ .../rust/darling_core/src/from_type_param.rs | 32 + third_party/rust/darling_core/src/from_variant.rs | 33 + third_party/rust/darling_core/src/lib.rs | 42 + .../rust/darling_core/src/macros_private.rs | 6 + third_party/rust/darling_core/src/macros_public.rs | 96 +++ third_party/rust/darling_core/src/options/core.rs | 173 +++++ .../rust/darling_core/src/options/forward_attrs.rs | 31 + .../darling_core/src/options/from_attributes.rs | 69 ++ .../rust/darling_core/src/options/from_derive.rs | 100 +++ .../rust/darling_core/src/options/from_field.rs | 79 ++ .../rust/darling_core/src/options/from_meta.rs | 50 ++ .../darling_core/src/options/from_type_param.rs | 79 ++ .../rust/darling_core/src/options/from_variant.rs | 85 ++ .../rust/darling_core/src/options/input_field.rs | 180 +++++ .../rust/darling_core/src/options/input_variant.rs | 104 +++ third_party/rust/darling_core/src/options/mod.rs | 145 ++++ .../rust/darling_core/src/options/outer_from.rs | 87 +++ third_party/rust/darling_core/src/options/shape.rs | 258 +++++++ .../rust/darling_core/src/usage/generics_ext.rs | 24 + .../rust/darling_core/src/usage/ident_set.rs | 8 + .../rust/darling_core/src/usage/lifetimes.rs | 327 ++++++++ third_party/rust/darling_core/src/usage/mod.rs | 111 +++ third_party/rust/darling_core/src/usage/options.rs | 58 ++ .../rust/darling_core/src/usage/type_params.rs | 357 +++++++++ third_party/rust/darling_core/src/util/flag.rs | 100 +++ .../rust/darling_core/src/util/ident_string.rs | 157 ++++ third_party/rust/darling_core/src/util/ignored.rs | 50 ++ third_party/rust/darling_core/src/util/mod.rs | 23 + .../rust/darling_core/src/util/over_ride.rs | 147 ++++ .../rust/darling_core/src/util/parse_attribute.rs | 73 ++ .../rust/darling_core/src/util/path_list.rs | 104 +++ .../rust/darling_core/src/util/path_to_string.rs | 36 + third_party/rust/darling_core/src/util/shape.rs | 285 +++++++ .../rust/darling_core/src/util/spanned_value.rs | 133 ++++ .../rust/darling_core/src/util/with_original.rs | 35 + 66 files changed, 8201 insertions(+) create mode 100644 third_party/rust/darling_core/.cargo-checksum.json create mode 100644 third_party/rust/darling_core/Cargo.toml create mode 100644 third_party/rust/darling_core/LICENSE create mode 100644 third_party/rust/darling_core/src/ast/data.rs create mode 100644 third_party/rust/darling_core/src/ast/generics.rs create mode 100644 third_party/rust/darling_core/src/ast/mod.rs create mode 100644 third_party/rust/darling_core/src/codegen/attr_extractor.rs create mode 100644 third_party/rust/darling_core/src/codegen/default_expr.rs create mode 100644 third_party/rust/darling_core/src/codegen/error.rs create mode 100644 third_party/rust/darling_core/src/codegen/field.rs create mode 100644 third_party/rust/darling_core/src/codegen/from_attributes_impl.rs create mode 100644 third_party/rust/darling_core/src/codegen/from_derive_impl.rs create mode 100644 third_party/rust/darling_core/src/codegen/from_field.rs create mode 100644 third_party/rust/darling_core/src/codegen/from_meta_impl.rs create mode 100644 third_party/rust/darling_core/src/codegen/from_type_param.rs create mode 100644 third_party/rust/darling_core/src/codegen/from_variant_impl.rs create mode 100644 third_party/rust/darling_core/src/codegen/mod.rs create mode 100644 third_party/rust/darling_core/src/codegen/outer_from_impl.rs create mode 100644 third_party/rust/darling_core/src/codegen/postfix_transform.rs create mode 100644 third_party/rust/darling_core/src/codegen/trait_impl.rs create mode 100644 third_party/rust/darling_core/src/codegen/variant.rs create mode 100644 third_party/rust/darling_core/src/codegen/variant_data.rs create mode 100644 third_party/rust/darling_core/src/derive.rs create mode 100644 third_party/rust/darling_core/src/error/kind.rs create mode 100644 third_party/rust/darling_core/src/error/mod.rs create mode 100644 third_party/rust/darling_core/src/from_attributes.rs create mode 100644 third_party/rust/darling_core/src/from_derive_input.rs create mode 100644 third_party/rust/darling_core/src/from_field.rs create mode 100644 third_party/rust/darling_core/src/from_generic_param.rs create mode 100644 third_party/rust/darling_core/src/from_generics.rs create mode 100644 third_party/rust/darling_core/src/from_meta.rs create mode 100644 third_party/rust/darling_core/src/from_type_param.rs create mode 100644 third_party/rust/darling_core/src/from_variant.rs create mode 100644 third_party/rust/darling_core/src/lib.rs create mode 100644 third_party/rust/darling_core/src/macros_private.rs create mode 100644 third_party/rust/darling_core/src/macros_public.rs create mode 100644 third_party/rust/darling_core/src/options/core.rs create mode 100644 third_party/rust/darling_core/src/options/forward_attrs.rs create mode 100644 third_party/rust/darling_core/src/options/from_attributes.rs create mode 100644 third_party/rust/darling_core/src/options/from_derive.rs create mode 100644 third_party/rust/darling_core/src/options/from_field.rs create mode 100644 third_party/rust/darling_core/src/options/from_meta.rs create mode 100644 third_party/rust/darling_core/src/options/from_type_param.rs create mode 100644 third_party/rust/darling_core/src/options/from_variant.rs create mode 100644 third_party/rust/darling_core/src/options/input_field.rs create mode 100644 third_party/rust/darling_core/src/options/input_variant.rs create mode 100644 third_party/rust/darling_core/src/options/mod.rs create mode 100644 third_party/rust/darling_core/src/options/outer_from.rs create mode 100644 third_party/rust/darling_core/src/options/shape.rs create mode 100644 third_party/rust/darling_core/src/usage/generics_ext.rs create mode 100644 third_party/rust/darling_core/src/usage/ident_set.rs create mode 100644 third_party/rust/darling_core/src/usage/lifetimes.rs create mode 100644 third_party/rust/darling_core/src/usage/mod.rs create mode 100644 third_party/rust/darling_core/src/usage/options.rs create mode 100644 third_party/rust/darling_core/src/usage/type_params.rs create mode 100644 third_party/rust/darling_core/src/util/flag.rs create mode 100644 third_party/rust/darling_core/src/util/ident_string.rs create mode 100644 third_party/rust/darling_core/src/util/ignored.rs create mode 100644 third_party/rust/darling_core/src/util/mod.rs create mode 100644 third_party/rust/darling_core/src/util/over_ride.rs create mode 100644 third_party/rust/darling_core/src/util/parse_attribute.rs create mode 100644 third_party/rust/darling_core/src/util/path_list.rs create mode 100644 third_party/rust/darling_core/src/util/path_to_string.rs create mode 100644 third_party/rust/darling_core/src/util/shape.rs create mode 100644 third_party/rust/darling_core/src/util/spanned_value.rs create mode 100644 third_party/rust/darling_core/src/util/with_original.rs (limited to 'third_party/rust/darling_core') diff --git a/third_party/rust/darling_core/.cargo-checksum.json b/third_party/rust/darling_core/.cargo-checksum.json new file mode 100644 index 0000000000..fbb4b2461e --- /dev/null +++ b/third_party/rust/darling_core/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"26ffc472d7fcaf591b6e2783dd724dd5952a3a4d3e49fabcef50de7163fadd0c","LICENSE":"8ea93490d74a5a1b1af3ff71d786271b3f1e5f0bea79ac16e02ec533cef040d6","src/ast/data.rs":"67360d2688d92be168e12e990792ea598091e29b47804b9919e38d75df8ac9e6","src/ast/generics.rs":"82d542307508f9053804511bc331131b037ec64d1ad50d8551a3f991dbbdfef8","src/ast/mod.rs":"58a58eeb1db7682e5994732cc8eb04088f6ca662805460ef443cf089e5c24e2c","src/codegen/attr_extractor.rs":"d801966bfdff0cd15c1db339384879115bd41b3d2298d612ff5429fbcd1763a8","src/codegen/default_expr.rs":"5486c8dc264e3bb8be2a871f3258f56ed2812ae39b1eb9f0d16006605407027f","src/codegen/error.rs":"4ef086317234a161bec493413ba38d842cbbecd0b9610dad0a25fe8883c75d3b","src/codegen/field.rs":"40d3f61507440ee62bb1c2098aca39be84ec74e2cecbd11340e925bd68a3cbfa","src/codegen/from_attributes_impl.rs":"f404e32f68bca0097157a23a2d098365d66ba0c3fad6cfdf10ec50d367916415","src/codegen/from_derive_impl.rs":"0f62e61566202f224bbfb8d251141ca64ace5022c5e85e312739da08f3ef51e2","src/codegen/from_field.rs":"10f18dfdae9e9cc295121585862b2f30ed2f7f1124675fe39ac0dd7921ede62a","src/codegen/from_meta_impl.rs":"15bfc28a3053d6750a2153943a951fcb8558b540ac6593529e4b1f2ec521002f","src/codegen/from_type_param.rs":"d301fabd5b0024b1267a6c7887f6214fb86783fd4c0bb9a15867d5f3f58fc4b8","src/codegen/from_variant_impl.rs":"e2cc68ad5d0a45989830d144760d55b3fb62297c5aea097fa085dabb22e3897f","src/codegen/mod.rs":"77b194876541e8ced86efebb534fc8a4f70c06d92c8b493aa90ef936bcc14273","src/codegen/outer_from_impl.rs":"c6ef8aad7f9c817005e236b3bebf56319455f578b109bdb47b23c0a1dc1149f5","src/codegen/postfix_transform.rs":"41c84a239c15671e5a9cb8ce9fbb1b907f20aebe30d5dea4f04f004e3aa584c1","src/codegen/trait_impl.rs":"22d307450c56abf29dc2fdf1ced8de2220cc5f544f5297452e829a30093e906e","src/codegen/variant.rs":"2a52652c623fc27bf0c3c4706b678a7226a65eb9be6967b656ed3755339b26c6","src/codegen/variant_data.rs":"27c2ef03521254fc071af9586ff75c3b4f263d6f75ff737f472ee376f9ea059b","src/derive.rs":"d35f35233eea28bc66bf6f738358415d650741560ca56f8deaee9465b3e88b2a","src/error/kind.rs":"862fb877500ceb95e89d5b53bef987a0836e8754bdcc5c30d074c0f53e94d715","src/error/mod.rs":"010cc51eedfca91fa63f9def97a42bb552e503448a875b7d29f63af465e67967","src/from_attributes.rs":"9c98a34eccdb66526711793d6d00bd5216de1d0e9d3755133996242a27efe272","src/from_derive_input.rs":"a60aba72ebd4deddd6bb9919995652f90aafdf88f6109c00c1a1c4459e439049","src/from_field.rs":"1e22b7750843b6a018fe75ae1510fb9252343be99ab02d2093881421e87d867d","src/from_generic_param.rs":"bdabc80f49212501e6e3574c7e6c03b4e453d0fe241ac46d465a5f004381f4c9","src/from_generics.rs":"b0a156179b2d1d88467183649afc97405403b01a7fdae39e4df8392f6b9f500c","src/from_meta.rs":"f0975b219d3baea104b2a307f7a850b2a2ce9786f033348e83dff0bec70ca996","src/from_type_param.rs":"0c8930a42e1f0720429aa968f97f938e207460232dd2169cbf8efec12e7c6f5a","src/from_variant.rs":"dd27ffef65ceea0e68809afde7df82d3751cdedc248773062637aa8c146da6e7","src/lib.rs":"b066b0747dcec5150d6962a80850a8fb7690b24700f62537893ebb2d4c88a4a8","src/macros_private.rs":"4373c36e75d516c340fc9b13c52b471d823fff9997cb7faeeb5f93b11d40d0b4","src/macros_public.rs":"7d2ce0c5026227ef7854db11d7a885ad891255438b2e49bbdfda56fa2f92feec","src/options/core.rs":"cd84b2405f4c43bfef720123dbefecb65220313b79a3f3d617392ce909382609","src/options/forward_attrs.rs":"c6a960f74957854eabd5a5c87a15dbee897ea9a2f48d0094fbaec110f3a785cf","src/options/from_attributes.rs":"f73c4dc53cced30e60812a4247f3252ea0630618b1d162058cce0bd1666c43c9","src/options/from_derive.rs":"00e9c5bbbe9682ef2a3283ccf59818c348e3fd9d85e8ef75913028ac99c1e459","src/options/from_field.rs":"006e127a648d9ad095fa92ffc6f9e106fdc1a784dd82be88dc73ed80198fd429","src/options/from_meta.rs":"1a543110dc4ab2e716a914bafced9e1f4035794613117708fc9f37a1036bcf75","src/options/from_type_param.rs":"c6f907d3bceef3a918822b75e019f520650d66c523acc2ffac37d03016234659","src/options/from_variant.rs":"6b426a453710f6e3d40d8fbdce4dbb8aa6470c98f8d385654f0eae98ab7e4be3","src/options/input_field.rs":"93000e7705a0a5f0c2ed3354d6585be1987f0a1ef297bb82ffd140440e26808a","src/options/input_variant.rs":"1ebc013018abb07670f13c71d5c819d079a4e824235ede43fb568239efcbf1f7","src/options/mod.rs":"719373f25a35c3bfab670d0669b1e0f41e7a7a58a98d957e4b34331e8dac1f9e","src/options/outer_from.rs":"96a0abb1e324f7e9295cababf29aef63f1cad9e5fddac08768d7042cc62fd73c","src/options/shape.rs":"75b8fc47a2f8b2b50b40b4c578c2d17fc999b42ad403ef036f24d0b37da25623","src/usage/generics_ext.rs":"b2d15e2ff75c47f43f3ce305b2108e800dd3d89e3a21fadf82bda455837b8090","src/usage/ident_set.rs":"30edb2f0a599284967e3c6b579da31e5f9b15f3dd67bc9a82d6335eb44133df0","src/usage/lifetimes.rs":"4fe4009e9ff901e12aee204270adaad250e15f4a023a0bf497006977ee1a0ddf","src/usage/mod.rs":"e49adadfa8ffed27299d5bbf3c33e68e2c8c7411f89aef655f77e158be9dd642","src/usage/options.rs":"0491c995aad0d55783b24cce8a4e40f0f4435988c54ce2ded34763ac9b199fcf","src/usage/type_params.rs":"4817447aa2eddcc6c5fe24d13623152cac2dfc0763524ca6111750f3b512f4db","src/util/flag.rs":"3cf473760b0c23d4b2e0144e5da23f858ade7e1b86504827f74dfe123f73c604","src/util/ident_string.rs":"2a2d32f9ba3a5b46ddcf430891d62f47d0a65782b592cccbfd0e88bb1361cbc9","src/util/ignored.rs":"099c015023b9be0870fb1c4c44ad00f87789ea13da25e2ca952132443e497214","src/util/mod.rs":"f8214b2e5fb114092e9e83875168449bccc26967637b13a21cbff835681ad256","src/util/over_ride.rs":"5ce783a1fdfa7281038c03294572956f22efb1ae9b0794728db5b6d9fbcead68","src/util/parse_attribute.rs":"17f7f26ca2f459a3651ef24e85808e5318795c0ec4f719cabc98ccad65e3257a","src/util/path_list.rs":"ec9f39cb274b5b1218ffb3f6ebf9f2d3f26e047a4934b5be884f8aaa57257526","src/util/path_to_string.rs":"c093354f91ab45e0be3c50d2d4356408f415c910f1569fb9275c5087110736f6","src/util/shape.rs":"dda17aa8a063b5023ae1f055bec2859f984ec32633f6cc55938a442252f07204","src/util/spanned_value.rs":"f161bc6ed45351e5c715dbd55e33ee2f52ba5dbd906eb09e391075aa0ca093cf","src/util/with_original.rs":"56ea20c03d07ebe1a8bc0ed220258f6cee5a3cf7192c0e07601fafda99404062"},"package":"001d80444f28e193f30c2f293455da62dcf9a6b29918a4253152ae2b1de592cb"} \ No newline at end of file diff --git a/third_party/rust/darling_core/Cargo.toml b/third_party/rust/darling_core/Cargo.toml new file mode 100644 index 0000000000..9e5b5365d5 --- /dev/null +++ b/third_party/rust/darling_core/Cargo.toml @@ -0,0 +1,49 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +name = "darling_core" +version = "0.14.3" +authors = ["Ted Driggs "] +description = """ +Helper crate for proc-macro library for reading attributes into structs when +implementing custom derives. Use https://crates.io/crates/darling in your code. +""" +license = "MIT" +repository = "https://github.com/TedDriggs/darling" + +[dependencies.fnv] +version = "1.0.7" + +[dependencies.ident_case] +version = "1.0.1" + +[dependencies.proc-macro2] +version = "1.0.37" + +[dependencies.quote] +version = "1.0.18" + +[dependencies.strsim] +version = "0.10.0" +optional = true + +[dependencies.syn] +version = "1.0.91" +features = [ + "full", + "extra-traits", +] + +[features] +diagnostics = [] +suggestions = ["strsim"] diff --git a/third_party/rust/darling_core/LICENSE b/third_party/rust/darling_core/LICENSE new file mode 100644 index 0000000000..0b48eadc9d --- /dev/null +++ b/third_party/rust/darling_core/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Ted Driggs + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/rust/darling_core/src/ast/data.rs b/third_party/rust/darling_core/src/ast/data.rs new file mode 100644 index 0000000000..958c7be9ab --- /dev/null +++ b/third_party/rust/darling_core/src/ast/data.rs @@ -0,0 +1,477 @@ +use std::{slice, vec}; + +use proc_macro2::{Span, TokenStream}; +use quote::{quote, quote_spanned, ToTokens}; +use syn::spanned::Spanned; + +use crate::usage::{ + self, IdentRefSet, IdentSet, LifetimeRefSet, LifetimeSet, UsesLifetimes, UsesTypeParams, +}; +use crate::{Error, FromField, FromVariant, Result}; + +/// A struct or enum body. +/// +/// `V` is the type which receives any encountered variants, and `F` receives struct fields. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Data { + Enum(Vec), + Struct(Fields), +} + +impl Data { + /// Creates an empty body of the same shape as the passed-in body. + /// + /// # Panics + /// This function will panic if passed `syn::Data::Union`. + pub fn empty_from(src: &syn::Data) -> Self { + match *src { + syn::Data::Enum(_) => Data::Enum(vec![]), + syn::Data::Struct(ref vd) => Data::Struct(Fields::empty_from(&vd.fields)), + syn::Data::Union(_) => panic!("Unions are not supported"), + } + } + + /// Creates an empty body of the same shape as the passed-in body. + /// + /// `darling` does not support unions; calling this function with a union body will return an error. + pub fn try_empty_from(src: &syn::Data) -> Result { + match *src { + syn::Data::Enum(_) => Ok(Data::Enum(vec![])), + syn::Data::Struct(ref vd) => Ok(Data::Struct(Fields::empty_from(&vd.fields))), + // This deliberately doesn't set a span on the error message, as the error is most useful if + // applied to the call site of the offending macro. Given that the message is very generic, + // putting it on the union keyword ends up being confusing. + syn::Data::Union(_) => Err(Error::custom("Unions are not supported")), + } + } + + /// Creates a new `Data<&'a V, &'a F>` instance from `Data`. + pub fn as_ref(&self) -> Data<&V, &F> { + match *self { + Data::Enum(ref variants) => Data::Enum(variants.iter().collect()), + Data::Struct(ref data) => Data::Struct(data.as_ref()), + } + } + + /// Applies a function `V -> U` on enum variants, if this is an enum. + pub fn map_enum_variants(self, map: T) -> Data + where + T: FnMut(V) -> U, + { + match self { + Data::Enum(v) => Data::Enum(v.into_iter().map(map).collect()), + Data::Struct(f) => Data::Struct(f), + } + } + + /// Applies a function `F -> U` on struct fields, if this is a struct. + pub fn map_struct_fields(self, map: T) -> Data + where + T: FnMut(F) -> U, + { + match self { + Data::Enum(v) => Data::Enum(v), + Data::Struct(f) => Data::Struct(f.map(map)), + } + } + + /// Applies a function to the `Fields` if this is a struct. + pub fn map_struct(self, mut map: T) -> Data + where + T: FnMut(Fields) -> Fields, + { + match self { + Data::Enum(v) => Data::Enum(v), + Data::Struct(f) => Data::Struct(map(f)), + } + } + + /// Consumes the `Data`, returning `Fields` if it was a struct. + pub fn take_struct(self) -> Option> { + match self { + Data::Enum(_) => None, + Data::Struct(f) => Some(f), + } + } + + /// Consumes the `Data`, returning `Vec` if it was an enum. + pub fn take_enum(self) -> Option> { + match self { + Data::Enum(v) => Some(v), + Data::Struct(_) => None, + } + } + + /// Returns `true` if this instance is `Data::Enum`. + pub fn is_enum(&self) -> bool { + match *self { + Data::Enum(_) => true, + Data::Struct(_) => false, + } + } + + /// Returns `true` if this instance is `Data::Struct`. + pub fn is_struct(&self) -> bool { + !self.is_enum() + } +} + +impl Data { + /// Attempt to convert from a `syn::Data` instance. + pub fn try_from(body: &syn::Data) -> Result { + match *body { + syn::Data::Enum(ref data) => { + let mut errors = Error::accumulator(); + let items = data + .variants + .iter() + .filter_map(|v| errors.handle(FromVariant::from_variant(v))) + .collect(); + + errors.finish_with(Data::Enum(items)) + } + syn::Data::Struct(ref data) => Ok(Data::Struct(Fields::try_from(&data.fields)?)), + // This deliberately doesn't set a span on the error message, as the error is most useful if + // applied to the call site of the offending macro. Given that the message is very generic, + // putting it on the union keyword ends up being confusing. + syn::Data::Union(_) => Err(Error::custom("Unions are not supported")), + } + } +} + +impl UsesTypeParams for Data { + fn uses_type_params<'a>( + &self, + options: &usage::Options, + type_set: &'a IdentSet, + ) -> IdentRefSet<'a> { + match *self { + Data::Struct(ref v) => v.uses_type_params(options, type_set), + Data::Enum(ref v) => v.uses_type_params(options, type_set), + } + } +} + +impl UsesLifetimes for Data { + fn uses_lifetimes<'a>( + &self, + options: &usage::Options, + lifetimes: &'a LifetimeSet, + ) -> LifetimeRefSet<'a> { + match *self { + Data::Struct(ref v) => v.uses_lifetimes(options, lifetimes), + Data::Enum(ref v) => v.uses_lifetimes(options, lifetimes), + } + } +} + +/// Equivalent to `syn::Fields`, but replaces the AST element with a generic. +#[derive(Debug, Clone)] +pub struct Fields { + pub style: Style, + pub fields: Vec, + span: Option, + __nonexhaustive: (), +} + +impl Fields { + /// Creates a new [`Fields`] struct. + pub fn new(style: Style, fields: Vec) -> Self { + Self { + style, + fields, + span: None, + __nonexhaustive: (), + } + } + + /// Adds a [`Span`] to [`Fields`]. + pub fn with_span(mut self, span: Span) -> Self { + if self.span.is_none() { + self.span = Some(span); + } + self + } + + pub fn empty_from(vd: &syn::Fields) -> Self { + Self::new(vd.into(), Vec::new()) + } + + /// Splits the `Fields` into its style and fields for further processing. + /// Returns an empty `Vec` for `Unit` data. + pub fn split(self) -> (Style, Vec) { + (self.style, self.fields) + } + + /// Returns true if this variant's data makes it a newtype. + pub fn is_newtype(&self) -> bool { + self.style == Style::Tuple && self.len() == 1 + } + + pub fn is_unit(&self) -> bool { + self.style.is_unit() + } + + pub fn is_tuple(&self) -> bool { + self.style.is_tuple() + } + + pub fn is_struct(&self) -> bool { + self.style.is_struct() + } + + pub fn as_ref(&self) -> Fields<&T> { + Fields { + style: self.style, + fields: self.fields.iter().collect(), + span: self.span, + __nonexhaustive: (), + } + } + + pub fn map(self, map: F) -> Fields + where + F: FnMut(T) -> U, + { + Fields { + style: self.style, + fields: self.fields.into_iter().map(map).collect(), + span: self.span, + __nonexhaustive: (), + } + } + + pub fn iter(&self) -> slice::Iter { + self.fields.iter() + } + + /// Returns the number of fields in the structure. + pub fn len(&self) -> usize { + self.fields.len() + } + + /// Returns `true` if the `Fields` contains no fields. + pub fn is_empty(&self) -> bool { + self.fields.is_empty() + } +} + +impl Fields { + pub fn try_from(fields: &syn::Fields) -> Result { + let mut errors = Error::accumulator(); + let items = { + match &fields { + syn::Fields::Named(fields) => fields + .named + .iter() + .filter_map(|field| { + errors.handle(FromField::from_field(field).map_err(|err| { + // There should always be an ident here, since this is a collection + // of named fields, but `syn` doesn't prevent someone from manually + // constructing an invalid collection so a guard is still warranted. + if let Some(ident) = &field.ident { + err.at(ident) + } else { + err + } + })) + }) + .collect(), + syn::Fields::Unnamed(fields) => fields + .unnamed + .iter() + .filter_map(|field| errors.handle(FromField::from_field(field))) + .collect(), + syn::Fields::Unit => vec![], + } + }; + + errors.finish()?; + + Ok(Self::new(fields.into(), items).with_span(fields.span())) + } +} + +impl ToTokens for Fields { + fn to_tokens(&self, tokens: &mut TokenStream) { + let fields = &self.fields; + // An unknown Span should be `Span::call_site()`; + // https://docs.rs/syn/1.0.12/syn/spanned/trait.Spanned.html#tymethod.span + let span = self.span.unwrap_or_else(Span::call_site); + + match self.style { + Style::Struct => { + let trailing_comma = { + if fields.is_empty() { + quote!() + } else { + quote!(,) + } + }; + + tokens.extend(quote_spanned![span => { #(#fields),* #trailing_comma }]); + } + Style::Tuple => { + tokens.extend(quote_spanned![span => ( #(#fields),* )]); + } + Style::Unit => {} + } + } +} + +impl PartialEq for Fields { + fn eq(&self, other: &Self) -> bool { + self.style == other.style && self.fields == other.fields + } +} + +impl Eq for Fields {} + +impl IntoIterator for Fields { + type Item = T; + type IntoIter = vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.fields.into_iter() + } +} + +impl From