From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/serde_repr/.cargo-checksum.json | 1 + vendor/serde_repr/Cargo.toml | 50 +++++ vendor/serde_repr/LICENSE-APACHE | 201 +++++++++++++++++++++ vendor/serde_repr/LICENSE-MIT | 23 +++ vendor/serde_repr/README.md | 57 ++++++ vendor/serde_repr/src/lib.rs | 140 ++++++++++++++ vendor/serde_repr/src/parse.rs | 114 ++++++++++++ vendor/serde_repr/tests/compiletest.rs | 7 + vendor/serde_repr/tests/test.rs | 80 ++++++++ vendor/serde_repr/tests/ui/empty_enum.rs | 6 + vendor/serde_repr/tests/ui/empty_enum.stderr | 7 + vendor/serde_repr/tests/ui/missing_repr.rs | 11 ++ vendor/serde_repr/tests/ui/missing_repr.stderr | 7 + vendor/serde_repr/tests/ui/multiple_others.rs | 12 ++ vendor/serde_repr/tests/ui/multiple_others.stderr | 7 + vendor/serde_repr/tests/ui/non_unit_variant.rs | 12 ++ vendor/serde_repr/tests/ui/non_unit_variant.stderr | 5 + vendor/serde_repr/tests/ui/not_enum.rs | 11 ++ vendor/serde_repr/tests/ui/not_enum.stderr | 7 + 19 files changed, 758 insertions(+) create mode 100644 vendor/serde_repr/.cargo-checksum.json create mode 100644 vendor/serde_repr/Cargo.toml create mode 100644 vendor/serde_repr/LICENSE-APACHE create mode 100644 vendor/serde_repr/LICENSE-MIT create mode 100644 vendor/serde_repr/README.md create mode 100644 vendor/serde_repr/src/lib.rs create mode 100644 vendor/serde_repr/src/parse.rs create mode 100644 vendor/serde_repr/tests/compiletest.rs create mode 100644 vendor/serde_repr/tests/test.rs create mode 100644 vendor/serde_repr/tests/ui/empty_enum.rs create mode 100644 vendor/serde_repr/tests/ui/empty_enum.stderr create mode 100644 vendor/serde_repr/tests/ui/missing_repr.rs create mode 100644 vendor/serde_repr/tests/ui/missing_repr.stderr create mode 100644 vendor/serde_repr/tests/ui/multiple_others.rs create mode 100644 vendor/serde_repr/tests/ui/multiple_others.stderr create mode 100644 vendor/serde_repr/tests/ui/non_unit_variant.rs create mode 100644 vendor/serde_repr/tests/ui/non_unit_variant.stderr create mode 100644 vendor/serde_repr/tests/ui/not_enum.rs create mode 100644 vendor/serde_repr/tests/ui/not_enum.stderr (limited to 'vendor/serde_repr') diff --git a/vendor/serde_repr/.cargo-checksum.json b/vendor/serde_repr/.cargo-checksum.json new file mode 100644 index 000000000..4012e22d8 --- /dev/null +++ b/vendor/serde_repr/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"99cae9ec2495b733e77b78630bc8cfc312afac866b84c71618d896a7c90e555d","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","README.md":"18848980d7a8fc120c0c03fbf02a79ea7cd9e6f446f4c76aad48782c76967634","src/lib.rs":"6991484c16826a6b5bf50924448d5b1a858e832f64770c95bd2ac24a757e632a","src/parse.rs":"687cf1436d12c0c26b1c6d06f80e56b4ec0d63a38d71e1f175b981c0fdc10c68","tests/compiletest.rs":"022a8e400ef813d7ea1875b944549cee5125f6a995dc33e93b48cba3e1b57bd1","tests/test.rs":"a5c4df255ded38d91a3abb4b6c59fde6710a17b0832c8a5bba2a04b455a14bfa","tests/ui/empty_enum.rs":"fe1166f2f92ee213d26a23e57572a99c65c163d446fd8d67e1520bab34f4b859","tests/ui/empty_enum.stderr":"2c8907fc146bb4dcdb926b72de4f823dae4c1c0d8de8a636c512539408f26cc7","tests/ui/missing_repr.rs":"b7ba9341111582cad52e761b82f14778c60352014c4265566e4d4d01ccdcb306","tests/ui/missing_repr.stderr":"dfcb65527963fb8962e1442e10d3c71e141f238b1732483e1739b528ae0eca43","tests/ui/multiple_others.rs":"569658516619719f21e5071873a37125d5390edb77558e4381401f03efda0c83","tests/ui/multiple_others.stderr":"a300f94d2a2049d6718866d17b5673201edc275164b9e8eba25a9ffcd71fd30f","tests/ui/non_unit_variant.rs":"0d8295ae08d882fc3ef4164857240038c5b0674ff0811da9b6ea7343e8bb955c","tests/ui/non_unit_variant.stderr":"18f2900e501b8c64b980445758ca1cb59594dc8d24a1a76abb08a48d8beb3f35","tests/ui/not_enum.rs":"ada7637821c924a6b99175363c820375991be60223f96ca553d304fde2721386","tests/ui/not_enum.stderr":"814525f9a1495225511a02ad4149c9371ea622230b8002564fe83cf52300c728"},"package":"a2ad84e47328a31223de7fed7a4f5087f2d6ddfe586cf3ca25b7a165bc0a5aed"} \ No newline at end of file diff --git a/vendor/serde_repr/Cargo.toml b/vendor/serde_repr/Cargo.toml new file mode 100644 index 000000000..41cb6a15c --- /dev/null +++ b/vendor/serde_repr/Cargo.toml @@ -0,0 +1,50 @@ +# 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" +rust-version = "1.31" +name = "serde_repr" +version = "0.1.8" +authors = ["David Tolnay "] +description = "Derive Serialize and Deserialize that delegates to the underlying repr of a C-like enum." +documentation = "https://docs.rs/serde_repr" +readme = "README.md" +license = "MIT OR Apache-2.0" +repository = "https://github.com/dtolnay/serde-repr" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[lib] +proc-macro = true + +[dependencies.proc-macro2] +version = "1.0" + +[dependencies.quote] +version = "1.0" + +[dependencies.syn] +version = "1.0" + +[dev-dependencies.rustversion] +version = "1.0" + +[dev-dependencies.serde] +version = "1.0" + +[dev-dependencies.serde_json] +version = "1.0" + +[dev-dependencies.trybuild] +version = "1.0.49" +features = ["diff"] diff --git a/vendor/serde_repr/LICENSE-APACHE b/vendor/serde_repr/LICENSE-APACHE new file mode 100644 index 000000000..16fe87b06 --- /dev/null +++ b/vendor/serde_repr/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/vendor/serde_repr/LICENSE-MIT b/vendor/serde_repr/LICENSE-MIT new file mode 100644 index 000000000..31aa79387 --- /dev/null +++ b/vendor/serde_repr/LICENSE-MIT @@ -0,0 +1,23 @@ +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/vendor/serde_repr/README.md b/vendor/serde_repr/README.md new file mode 100644 index 000000000..c87b3cedc --- /dev/null +++ b/vendor/serde_repr/README.md @@ -0,0 +1,57 @@ +Serde repr derive +================= + +[github](https://github.com/dtolnay/serde-repr) +[crates.io](https://crates.io/crates/serde_repr) +[docs.rs](https://docs.rs/serde_repr) +[build status](https://github.com/dtolnay/serde-repr/actions?query=branch%3Amaster) + +This crate provides a derive macro to derive Serde's `Serialize` and +`Deserialize` traits in a way that delegates to the underlying repr of a C-like +enum. + +```toml +[dependencies] +serde = "1.0" +serde_repr = "0.1" +``` + +```rust +use serde_repr::{Serialize_repr, Deserialize_repr}; + +#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] +#[repr(u8)] +enum SmallPrime { + Two = 2, + Three = 3, + Five = 5, + Seven = 7, +} + +fn main() -> serde_json::Result<()> { + let j = serde_json::to_string(&SmallPrime::Seven)?; + assert_eq!(j, "7"); + + let p: SmallPrime = serde_json::from_str("2")?; + assert_eq!(p, SmallPrime::Two); + + Ok(()) +} +``` + +
+ +#### License + + +Licensed under either of Apache License, Version +2.0 or MIT license at your option. + + +
+ + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in this crate by you, as defined in the Apache-2.0 license, shall +be dual licensed as above, without any additional terms or conditions. + diff --git a/vendor/serde_repr/src/lib.rs b/vendor/serde_repr/src/lib.rs new file mode 100644 index 000000000..72e2c2bdd --- /dev/null +++ b/vendor/serde_repr/src/lib.rs @@ -0,0 +1,140 @@ +//! [![github]](https://github.com/dtolnay/serde-repr) [![crates-io]](https://crates.io/crates/serde_repr) [![docs-rs]](https://docs.rs/serde_repr) +//! +//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github +//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust +//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K +//! +//!
+//! +//! Derive `Serialize` and `Deserialize` that delegates to the underlying repr +//! of a C-like enum. +//! +//! # Examples +//! +//! ``` +//! use serde_repr::{Serialize_repr, Deserialize_repr}; +//! +//! #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] +//! #[repr(u8)] +//! enum SmallPrime { +//! Two = 2, +//! Three = 3, +//! Five = 5, +//! Seven = 7, +//! } +//! +//! fn main() -> serde_json::Result<()> { +//! let j = serde_json::to_string(&SmallPrime::Seven)?; +//! assert_eq!(j, "7"); +//! +//! let p: SmallPrime = serde_json::from_str("2")?; +//! assert_eq!(p, SmallPrime::Two); +//! +//! Ok(()) +//! } +//! ``` + +#![allow(clippy::single_match_else)] + +extern crate proc_macro; + +mod parse; + +use proc_macro::TokenStream; +use quote::quote; +use syn::parse_macro_input; + +use crate::parse::Input; + +#[proc_macro_derive(Serialize_repr)] +pub fn derive_serialize(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as Input); + let ident = input.ident; + let repr = input.repr; + + let match_variants = input.variants.iter().map(|variant| { + let variant = &variant.ident; + quote! { + #ident::#variant => #ident::#variant as #repr, + } + }); + + TokenStream::from(quote! { + impl serde::Serialize for #ident { + #[allow(clippy::use_self)] + fn serialize(&self, serializer: S) -> core::result::Result + where + S: serde::Serializer + { + let value: #repr = match *self { + #(#match_variants)* + }; + serde::Serialize::serialize(&value, serializer) + } + } + }) +} + +#[proc_macro_derive(Deserialize_repr, attributes(serde))] +pub fn derive_deserialize(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as Input); + let ident = input.ident; + let repr = input.repr; + let variants = input.variants.iter().map(|variant| &variant.ident); + + let declare_discriminants = input.variants.iter().map(|variant| { + let variant = &variant.ident; + quote! { + const #variant: #repr = #ident::#variant as #repr; + } + }); + + let match_discriminants = input.variants.iter().map(|variant| { + let variant = &variant.ident; + quote! { + discriminant::#variant => core::result::Result::Ok(#ident::#variant), + } + }); + + let error_format = match input.variants.len() { + 1 => "invalid value: {}, expected {}".to_owned(), + 2 => "invalid value: {}, expected {} or {}".to_owned(), + n => "invalid value: {}, expected one of: {}".to_owned() + &", {}".repeat(n - 1), + }; + + let other_arm = match input.default_variant { + Some(variant) => { + let variant = &variant.ident; + quote! { + core::result::Result::Ok(#ident::#variant) + } + } + None => quote! { + core::result::Result::Err(serde::de::Error::custom( + format_args!(#error_format, other #(, discriminant::#variants)*) + )) + }, + }; + + TokenStream::from(quote! { + impl<'de> serde::Deserialize<'de> for #ident { + #[allow(clippy::use_self)] + fn deserialize(deserializer: D) -> core::result::Result + where + D: serde::Deserializer<'de>, + { + struct discriminant; + + #[allow(non_upper_case_globals)] + impl discriminant { + #(#declare_discriminants)* + } + + match <#repr as serde::Deserialize>::deserialize(deserializer)? { + #(#match_discriminants)* + other => #other_arm, + } + } + } + }) +} diff --git a/vendor/serde_repr/src/parse.rs b/vendor/serde_repr/src/parse.rs new file mode 100644 index 000000000..2c0ad1e7d --- /dev/null +++ b/vendor/serde_repr/src/parse.rs @@ -0,0 +1,114 @@ +use proc_macro2::Span; +use syn::parse::{Error, Parse, ParseStream, Parser, Result}; +use syn::{parenthesized, Data, DeriveInput, Fields, Ident, Meta, NestedMeta}; + +pub struct Input { + pub ident: Ident, + pub repr: Ident, + pub variants: Vec, + pub default_variant: Option, +} + +#[derive(Clone)] +pub struct Variant { + pub ident: Ident, + pub attrs: VariantAttrs, +} + +#[derive(Clone)] +pub struct VariantAttrs { + pub is_default: bool, +} + +fn parse_meta(attrs: &mut VariantAttrs, meta: &Meta) { + if let Meta::List(value) = meta { + for meta in &value.nested { + if let NestedMeta::Meta(Meta::Path(path)) = meta { + if path.is_ident("other") { + attrs.is_default = true; + } + } + } + } +} + +fn parse_attrs(variant: &syn::Variant) -> Result { + let mut attrs = VariantAttrs { is_default: false }; + for attr in &variant.attrs { + if attr.path.is_ident("serde") { + parse_meta(&mut attrs, &attr.parse_meta()?); + } + } + Ok(attrs) +} + +impl Parse for Input { + fn parse(input: ParseStream) -> Result { + let call_site = Span::call_site(); + let derive_input = DeriveInput::parse(input)?; + + let data = match derive_input.data { + Data::Enum(data) => data, + _ => { + return Err(Error::new(call_site, "input must be an enum")); + } + }; + + let variants = data + .variants + .into_iter() + .map(|variant| match variant.fields { + Fields::Unit => { + let attrs = parse_attrs(&variant)?; + Ok(Variant { + ident: variant.ident, + attrs, + }) + } + Fields::Named(_) | Fields::Unnamed(_) => { + Err(Error::new(variant.ident.span(), "must be a unit variant")) + } + }) + .collect::>>()?; + + if variants.is_empty() { + return Err(Error::new(call_site, "there must be at least one variant")); + } + + let generics = derive_input.generics; + if !generics.params.is_empty() || generics.where_clause.is_some() { + return Err(Error::new(call_site, "generic enum is not supported")); + } + + let mut repr = None; + for attr in derive_input.attrs { + if attr.path.is_ident("repr") { + fn repr_arg(input: ParseStream) -> Result { + let content; + parenthesized!(content in input); + content.parse() + } + let ty = repr_arg.parse2(attr.tokens)?; + repr = Some(ty); + break; + } + } + let repr = repr.ok_or_else(|| Error::new(call_site, "missing #[repr(...)] attribute"))?; + + let mut default_variants = variants.iter().filter(|x| x.attrs.is_default); + let default_variant = default_variants.next().cloned(); + if default_variants.next().is_some() { + return Err(Error::new( + call_site, + "only one variant can be #[serde(other)]", + )); + } + + Ok(Input { + ident: derive_input.ident, + repr, + variants, + default_variant, + }) + } +} diff --git a/vendor/serde_repr/tests/compiletest.rs b/vendor/serde_repr/tests/compiletest.rs new file mode 100644 index 000000000..7974a6249 --- /dev/null +++ b/vendor/serde_repr/tests/compiletest.rs @@ -0,0 +1,7 @@ +#[rustversion::attr(not(nightly), ignore)] +#[cfg_attr(miri, ignore)] +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/ui/*.rs"); +} diff --git a/vendor/serde_repr/tests/test.rs b/vendor/serde_repr/tests/test.rs new file mode 100644 index 000000000..8c44827dd --- /dev/null +++ b/vendor/serde_repr/tests/test.rs @@ -0,0 +1,80 @@ +#![allow( + // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 + clippy::nonstandard_macro_braces, + clippy::wildcard_imports, +)] + +use serde_repr::{Deserialize_repr, Serialize_repr}; + +mod small_prime { + use super::*; + + #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] + #[repr(u8)] + enum SmallPrime { + Two = 2, + Three = 3, + Five = 5, + Seven = 7, + } + + #[test] + fn test_serialize() { + let j = serde_json::to_string(&SmallPrime::Seven).unwrap(); + assert_eq!(j, "7"); + } + + #[test] + fn test_deserialize() { + let p: SmallPrime = serde_json::from_str("2").unwrap(); + assert_eq!(p, SmallPrime::Two); + } +} + +mod other { + use super::*; + + #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] + #[repr(u8)] + enum TestOther { + A, + B, + #[serde(other, rename = "useless")] + Other, + } + + #[test] + fn test_deserialize() { + let p: TestOther = serde_json::from_str("0").unwrap(); + assert_eq!(p, TestOther::A); + let p: TestOther = serde_json::from_str("1").unwrap(); + assert_eq!(p, TestOther::B); + let p: TestOther = serde_json::from_str("5").unwrap(); + assert_eq!(p, TestOther::Other); + } +} + +mod implicit_discriminant { + use super::*; + + #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] + #[repr(u8)] + enum ImplicitDiscriminant { + Zero, + One, + Two, + Three, + } + + #[test] + fn test_serialize() { + let j = serde_json::to_string(&ImplicitDiscriminant::Three).unwrap(); + assert_eq!(j, "3"); + } + + #[test] + fn test_deserialize() { + let p: ImplicitDiscriminant = serde_json::from_str("2").unwrap(); + assert_eq!(p, ImplicitDiscriminant::Two); + } +} diff --git a/vendor/serde_repr/tests/ui/empty_enum.rs b/vendor/serde_repr/tests/ui/empty_enum.rs new file mode 100644 index 000000000..f1fb6f9d5 --- /dev/null +++ b/vendor/serde_repr/tests/ui/empty_enum.rs @@ -0,0 +1,6 @@ +use serde_repr::Serialize_repr; + +#[derive(Serialize_repr)] +enum SmallPrime {} + +fn main() {} diff --git a/vendor/serde_repr/tests/ui/empty_enum.stderr b/vendor/serde_repr/tests/ui/empty_enum.stderr new file mode 100644 index 000000000..d7c4de19b --- /dev/null +++ b/vendor/serde_repr/tests/ui/empty_enum.stderr @@ -0,0 +1,7 @@ +error: there must be at least one variant + --> tests/ui/empty_enum.rs:3:10 + | +3 | #[derive(Serialize_repr)] + | ^^^^^^^^^^^^^^ + | + = note: this error originates in the derive macro `Serialize_repr` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/serde_repr/tests/ui/missing_repr.rs b/vendor/serde_repr/tests/ui/missing_repr.rs new file mode 100644 index 000000000..00836356b --- /dev/null +++ b/vendor/serde_repr/tests/ui/missing_repr.rs @@ -0,0 +1,11 @@ +use serde_repr::Serialize_repr; + +#[derive(Serialize_repr)] +enum SmallPrime { + Two = 2, + Three = 3, + Five = 5, + Seven = 7, +} + +fn main() {} diff --git a/vendor/serde_repr/tests/ui/missing_repr.stderr b/vendor/serde_repr/tests/ui/missing_repr.stderr new file mode 100644 index 000000000..e011be6f1 --- /dev/null +++ b/vendor/serde_repr/tests/ui/missing_repr.stderr @@ -0,0 +1,7 @@ +error: missing #[repr(...)] attribute + --> tests/ui/missing_repr.rs:3:10 + | +3 | #[derive(Serialize_repr)] + | ^^^^^^^^^^^^^^ + | + = note: this error originates in the derive macro `Serialize_repr` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/serde_repr/tests/ui/multiple_others.rs b/vendor/serde_repr/tests/ui/multiple_others.rs new file mode 100644 index 000000000..fdb552b10 --- /dev/null +++ b/vendor/serde_repr/tests/ui/multiple_others.rs @@ -0,0 +1,12 @@ +use serde_repr::Deserialize_repr; + +#[derive(Deserialize_repr)] +#[repr(u8)] +enum MultipleOthers { + #[serde(other)] + A, + #[serde(other)] + B, +} + +fn main() {} diff --git a/vendor/serde_repr/tests/ui/multiple_others.stderr b/vendor/serde_repr/tests/ui/multiple_others.stderr new file mode 100644 index 000000000..599d5e172 --- /dev/null +++ b/vendor/serde_repr/tests/ui/multiple_others.stderr @@ -0,0 +1,7 @@ +error: only one variant can be #[serde(other)] + --> tests/ui/multiple_others.rs:3:10 + | +3 | #[derive(Deserialize_repr)] + | ^^^^^^^^^^^^^^^^ + | + = note: this error originates in the derive macro `Deserialize_repr` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/serde_repr/tests/ui/non_unit_variant.rs b/vendor/serde_repr/tests/ui/non_unit_variant.rs new file mode 100644 index 000000000..9cdc80d0a --- /dev/null +++ b/vendor/serde_repr/tests/ui/non_unit_variant.rs @@ -0,0 +1,12 @@ +use serde_repr::Serialize_repr; + +#[derive(Serialize_repr)] +#[repr(u8)] +enum SmallPrime { + Two(u8), + Three(u8), + Five(u8), + Seven(u8), +} + +fn main() {} diff --git a/vendor/serde_repr/tests/ui/non_unit_variant.stderr b/vendor/serde_repr/tests/ui/non_unit_variant.stderr new file mode 100644 index 000000000..8eaf0c1d9 --- /dev/null +++ b/vendor/serde_repr/tests/ui/non_unit_variant.stderr @@ -0,0 +1,5 @@ +error: must be a unit variant + --> tests/ui/non_unit_variant.rs:6:5 + | +6 | Two(u8), + | ^^^ diff --git a/vendor/serde_repr/tests/ui/not_enum.rs b/vendor/serde_repr/tests/ui/not_enum.rs new file mode 100644 index 000000000..92a1d746d --- /dev/null +++ b/vendor/serde_repr/tests/ui/not_enum.rs @@ -0,0 +1,11 @@ +use serde_repr::Serialize_repr; + +#[derive(Serialize_repr)] +struct SmallPrime { + two: u8, + three: u8, + five: u8, + seven: u8, +} + +fn main() {} diff --git a/vendor/serde_repr/tests/ui/not_enum.stderr b/vendor/serde_repr/tests/ui/not_enum.stderr new file mode 100644 index 000000000..26ad4c313 --- /dev/null +++ b/vendor/serde_repr/tests/ui/not_enum.stderr @@ -0,0 +1,7 @@ +error: input must be an enum + --> tests/ui/not_enum.rs:3:10 + | +3 | #[derive(Serialize_repr)] + | ^^^^^^^^^^^^^^ + | + = note: this error originates in the derive macro `Serialize_repr` (in Nightly builds, run with -Z macro-backtrace for more info) -- cgit v1.2.3