diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/serde_with_macros | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serde_with_macros')
-rw-r--r-- | third_party/rust/serde_with_macros/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/serde_with_macros/CHANGELOG.md | 157 | ||||
-rw-r--r-- | third_party/rust/serde_with_macros/Cargo.toml | 62 | ||||
-rw-r--r-- | third_party/rust/serde_with_macros/LICENSE-APACHE | 201 | ||||
-rw-r--r-- | third_party/rust/serde_with_macros/LICENSE-MIT | 25 | ||||
-rw-r--r-- | third_party/rust/serde_with_macros/README.md | 186 | ||||
-rw-r--r-- | third_party/rust/serde_with_macros/src/lib.rs | 1016 | ||||
-rw-r--r-- | third_party/rust/serde_with_macros/src/utils.rs | 77 |
8 files changed, 1725 insertions, 0 deletions
diff --git a/third_party/rust/serde_with_macros/.cargo-checksum.json b/third_party/rust/serde_with_macros/.cargo-checksum.json new file mode 100644 index 0000000000..c147c72da5 --- /dev/null +++ b/third_party/rust/serde_with_macros/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"880c29c4c3ce0b1ff845ab7d65d1d993e16556e8086748f711a1f79c2436815f","Cargo.toml":"bb1a43583596e4ab00d1d584026e0c3f8d442f6e1edbc861ca0754bfbbf4655d","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"7576269ea71f767b99297934c0b2367532690f8c4badc695edf8e04ab6a1e545","README.md":"1bb8ac615b1f41460d5ebb22784ccb6ff0f028fb8756b43e911b3584400b0fe0","src/lib.rs":"e37b7afbce72225a66b5c55b4a51730d4830298949b12d59e0c2718f68fa5cb5","src/utils.rs":"2b64a8ae74104034691e43befba768daf954501d7eed988b12953fa765a14c8d"},"package":"e182d6ec6f05393cc0e5ed1bf81ad6db3a8feedf8ee515ecdd369809bcce8082"}
\ No newline at end of file diff --git a/third_party/rust/serde_with_macros/CHANGELOG.md b/third_party/rust/serde_with_macros/CHANGELOG.md new file mode 100644 index 0000000000..c8153d018b --- /dev/null +++ b/third_party/rust/serde_with_macros/CHANGELOG.md @@ -0,0 +1,157 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [1.5.2] - 2022-04-07 + +### Fixed + +* Account for generics when deriving implementations with `SerializeDisplay` and `DeserializeFromStr` #413 +* Provide better error messages when parsing types fails #423 + +## [1.5.1] - 2021-10-18 + +### Added + +* The minimal supported Rust version (MSRV) is now specified in the `Cargo.toml` via the `rust-version` field. The field is supported in Rust 1.56 and has no effect on versions before. + + More details: https://doc.rust-lang.org/nightly/cargo/reference/manifest.html#the-rust-version-field + +## [1.5.0] - 2021-09-04 + +### Added + +* Add the attribute `#[serde(borrow)]` on a field if `serde_as` is used in combination with the `BorrowCow` type. + +## [1.4.2] - 2021-06-07 + +### Fixed + +* Describe how the `serde_as` macro works on a high level. +* The derive macros `SerializeDisplay` and `DeserializeFromStr` were relying on the prelude where they were used. + Properly name all types and traits required for the expanded code to work. + The tests were improved to be better able to catch such problems. + +## [1.4.2] - 2021-02-16 + +### Fixed + +* Fix compiling when having a struct field without the `serde_as` annotation. + This broke in 1.4.0 [#267](https://github.com/jonasbb/serde_with/issues/267) + +## [1.4.0] - 2021-02-15 + +### Changed + +* Improve error messages when `#[serde_as(..)]` is misused as a field attribute. + Thanks to @Lehona for reporting the bug in #233. +* Internal cleanup for assembling and parsing attributes during `serde_as` processing. +* Change processing on `#[serde_as(...)]` attributes on fields. + + The attributes will no longer be stripped during proc-macro processing. + Instead, a private derive macro is applied to the struct/enum which captures them and makes them inert, thus allowing compilation. + + This should have no effect on the generated code and on the runtime behavior. + It eases integration of third-party crates with `serde_with`, since they can now process the `#[serde_as(...)]` field attributes reliably. + Before this was impossible for derive macros and lead to awkward ordering constraints on the attribute macros. + + Thanks to @Lehona for reporting this problem and to @dtolnay for suggesting the dummy derive macro. + +## [1.3.0] - 2020-11-22 + +### Added + +* Support specifying a path to the `serde_with` crate for the `serde_as` and derive macros. + This is useful when using crate renaming in Cargo.toml or while re-exporting the macros. + + Many thanks to @tobz1000 for raising the issue and contributing fixes. + +### Changed + +* Bump minimum supported rust version to 1.40.0 + +## [1.2.2] - 2020-10-06 + +### Fixed + +* @adwhit contributed an improvement to `DeserializeFromStr` which allows it to deserialize from bytes (#186). + This makes the derived implementation applicable in more situations. + +## [1.2.1] - 2020-10-04 + +### Fixed + +* The derive macros `SerializeDisplay` and `DeserializeFromStr` now use the properly namespaced types and traits. + This solves conflicts with `Result` if `Result` is not `std::result::Result`, e.g., a type alias. + Additionally, the code assumed that `FromStr` was in scope, which is now also not required. + + Thanks goes to @adwhit for reporting and fixing the problem in #186. + +## [1.2.0] - 2020-10-01 + +### Added + +* Add `serde_as` macro. Refer to the `serde_with` crate for details. +* Add two derive macros, `SerializeDisplay` and `DeserializeFromStr`, which implement the `Serialize`/`Deserialize` traits based on `Display` and `FromStr`. + This is in addition to the already existing methods like `DisplayFromStr`, which act locally, whereas the derive macros provide the traits expected by the rest of the ecosystem. + +### Changed + +* Convert the code to use 2018 edition. + +### Fixed + +* The `serde_as` macro now supports serde attributes and no longer panic on unrecognized values in the attribute. + +## [1.2.0-alpha.3] - 2020-08-16 + +### Added + +* Add two derive macros, `SerializeDisplay` and `DeserializeFromStr`, which implement the `Serialize`/`Deserialize` traits based on `Display` and `FromStr`. + This is in addition to the already existing methods like `DisplayFromStr`, which act locally, whereas the derive macros provide the traits expected by the rest of the ecosystem. + +## [1.2.0-alpha.2] - 2020-08-08 + +### Fixed + +* The `serde_as` macro now supports serde attributes and no longer panic on unrecognized values in the attribute. + +## [1.2.0-alpha.1] - 2020-06-27 + +### Added + +* Add `serde_as` macro. Refer to the `serde_with` crate for details. + +### Changed + +* Convert the code to use 2018 edition. + +## [1.1.0] - 2020-01-16 + +### Changed + +* Bump minimal Rust version to 1.36.0 to support Rust Edition 2018 +* Improved CI pipeline by running `cargo audit` and `tarpaulin` in all configurations now. + +## [1.0.1] - 2019-04-09 + +### Fixed + +* Features for the `syn` dependency were missing. + This was hidden due to the dev-dependencies whose features leaked into the normal build. + +## [1.0.0] - 2019-04-02 + +Initial Release + +### Added + +* Add `skip_serializing_none` attribute, which adds `#[serde(skip_serializing_if = "Option::is_none")]` for each Option in a struct. + This is helpful for APIs which have many optional fields. + The effect of can be negated by adding `serialize_always` on those fields, which should always be serialized. + Existing `skip_serializing_if` will never be modified and those fields keep their behavior. diff --git a/third_party/rust/serde_with_macros/Cargo.toml b/third_party/rust/serde_with_macros/Cargo.toml new file mode 100644 index 0000000000..f7bb663141 --- /dev/null +++ b/third_party/rust/serde_with_macros/Cargo.toml @@ -0,0 +1,62 @@ +# 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.46" +name = "serde_with_macros" +version = "1.5.2" +authors = ["Jonas Bushart"] +include = ["src/**/*", "LICENSE-*", "README.md", "CHANGELOG.md"] +description = "proc-macro library for serde_with" +documentation = "https://docs.rs/serde_with_macros/" +readme = "README.md" +keywords = ["serde", "utilities", "serialization", "deserialization"] +categories = ["encoding"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/jonasbb/serde_with/" +[package.metadata.docs.rs] +all-features = true + +[lib] +proc-macro = true +[dependencies.darling] +version = "0.13.4" + +[dependencies.proc-macro2] +version = "1.0.1" + +[dependencies.quote] +version = "1.0.0" + +[dependencies.syn] +version = "1.0.3" +features = ["full", "parsing"] +[dev-dependencies.pretty_assertions] +version = "1.0.0" + +[dev-dependencies.rustversion] +version = "1.0.0" + +[dev-dependencies.serde] +version = "1.0.75" +features = ["derive"] + +[dev-dependencies.serde_json] +version = "1.0.25" + +[dev-dependencies.trybuild] +version = "1.0.14" + +[dev-dependencies.version-sync] +version = "0.9.1" +[badges.maintenance] +status = "actively-developed" diff --git a/third_party/rust/serde_with_macros/LICENSE-APACHE b/third_party/rust/serde_with_macros/LICENSE-APACHE new file mode 100644 index 0000000000..16fe87b06e --- /dev/null +++ b/third_party/rust/serde_with_macros/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/third_party/rust/serde_with_macros/LICENSE-MIT b/third_party/rust/serde_with_macros/LICENSE-MIT new file mode 100644 index 0000000000..9203baa055 --- /dev/null +++ b/third_party/rust/serde_with_macros/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2015 + +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/serde_with_macros/README.md b/third_party/rust/serde_with_macros/README.md new file mode 100644 index 0000000000..d9d466e5c9 --- /dev/null +++ b/third_party/rust/serde_with_macros/README.md @@ -0,0 +1,186 @@ +# Custom de/serialization functions for Rust's [serde](https://serde.rs) + +[![crates.io badge](https://img.shields.io/crates/v/serde_with.svg)](https://crates.io/crates/serde_with/) +[![Build Status](https://github.com/jonasbb/serde_with/workflows/Rust%20CI/badge.svg)](https://github.com/jonasbb/serde_with) +[![codecov](https://codecov.io/gh/jonasbb/serde_with/branch/master/graph/badge.svg)](https://codecov.io/gh/jonasbb/serde_with) +[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/4322/badge)](https://bestpractices.coreinfrastructure.org/projects/4322) +[![Binder](https://img.shields.io/badge/Try%20on%20-binder-579ACA.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFkAAABZCAMAAABi1XidAAAB8lBMVEX///9XmsrmZYH1olJXmsr1olJXmsrmZYH1olJXmsr1olJXmsrmZYH1olL1olJXmsr1olJXmsrmZYH1olL1olJXmsrmZYH1olJXmsr1olL1olJXmsrmZYH1olL1olJXmsrmZYH1olL1olL0nFf1olJXmsrmZYH1olJXmsq8dZb1olJXmsrmZYH1olJXmspXmspXmsr1olL1olJXmsrmZYH1olJXmsr1olL1olJXmsrmZYH1olL1olLeaIVXmsrmZYH1olL1olL1olJXmsrmZYH1olLna31Xmsr1olJXmsr1olJXmsrmZYH1olLqoVr1olJXmsr1olJXmsrmZYH1olL1olKkfaPobXvviGabgadXmsqThKuofKHmZ4Dobnr1olJXmsr1olJXmspXmsr1olJXmsrfZ4TuhWn1olL1olJXmsqBi7X1olJXmspZmslbmMhbmsdemsVfl8ZgmsNim8Jpk8F0m7R4m7F5nLB6jbh7jbiDirOEibOGnKaMhq+PnaCVg6qWg6qegKaff6WhnpKofKGtnomxeZy3noG6dZi+n3vCcpPDcpPGn3bLb4/Mb47UbIrVa4rYoGjdaIbeaIXhoWHmZYHobXvpcHjqdHXreHLroVrsfG/uhGnuh2bwj2Hxk17yl1vzmljzm1j0nlX1olL3AJXWAAAAbXRSTlMAEBAQHx8gICAuLjAwMDw9PUBAQEpQUFBXV1hgYGBkcHBwcXl8gICAgoiIkJCQlJicnJ2goKCmqK+wsLC4usDAwMjP0NDQ1NbW3Nzg4ODi5+3v8PDw8/T09PX29vb39/f5+fr7+/z8/Pz9/v7+zczCxgAABC5JREFUeAHN1ul3k0UUBvCb1CTVpmpaitAGSLSpSuKCLWpbTKNJFGlcSMAFF63iUmRccNG6gLbuxkXU66JAUef/9LSpmXnyLr3T5AO/rzl5zj137p136BISy44fKJXuGN/d19PUfYeO67Znqtf2KH33Id1psXoFdW30sPZ1sMvs2D060AHqws4FHeJojLZqnw53cmfvg+XR8mC0OEjuxrXEkX5ydeVJLVIlV0e10PXk5k7dYeHu7Cj1j+49uKg7uLU61tGLw1lq27ugQYlclHC4bgv7VQ+TAyj5Zc/UjsPvs1sd5cWryWObtvWT2EPa4rtnWW3JkpjggEpbOsPr7F7EyNewtpBIslA7p43HCsnwooXTEc3UmPmCNn5lrqTJxy6nRmcavGZVt/3Da2pD5NHvsOHJCrdc1G2r3DITpU7yic7w/7Rxnjc0kt5GC4djiv2Sz3Fb2iEZg41/ddsFDoyuYrIkmFehz0HR2thPgQqMyQYb2OtB0WxsZ3BeG3+wpRb1vzl2UYBog8FfGhttFKjtAclnZYrRo9ryG9uG/FZQU4AEg8ZE9LjGMzTmqKXPLnlWVnIlQQTvxJf8ip7VgjZjyVPrjw1te5otM7RmP7xm+sK2Gv9I8Gi++BRbEkR9EBw8zRUcKxwp73xkaLiqQb+kGduJTNHG72zcW9LoJgqQxpP3/Tj//c3yB0tqzaml05/+orHLksVO+95kX7/7qgJvnjlrfr2Ggsyx0eoy9uPzN5SPd86aXggOsEKW2Prz7du3VID3/tzs/sSRs2w7ovVHKtjrX2pd7ZMlTxAYfBAL9jiDwfLkq55Tm7ifhMlTGPyCAs7RFRhn47JnlcB9RM5T97ASuZXIcVNuUDIndpDbdsfrqsOppeXl5Y+XVKdjFCTh+zGaVuj0d9zy05PPK3QzBamxdwtTCrzyg/2Rvf2EstUjordGwa/kx9mSJLr8mLLtCW8HHGJc2R5hS219IiF6PnTusOqcMl57gm0Z8kanKMAQg0qSyuZfn7zItsbGyO9QlnxY0eCuD1XL2ys/MsrQhltE7Ug0uFOzufJFE2PxBo/YAx8XPPdDwWN0MrDRYIZF0mSMKCNHgaIVFoBbNoLJ7tEQDKxGF0kcLQimojCZopv0OkNOyWCCg9XMVAi7ARJzQdM2QUh0gmBozjc3Skg6dSBRqDGYSUOu66Zg+I2fNZs/M3/f/Grl/XnyF1Gw3VKCez0PN5IUfFLqvgUN4C0qNqYs5YhPL+aVZYDE4IpUk57oSFnJm4FyCqqOE0jhY2SMyLFoo56zyo6becOS5UVDdj7Vih0zp+tcMhwRpBeLyqtIjlJKAIZSbI8SGSF3k0pA3mR5tHuwPFoa7N7reoq2bqCsAk1HqCu5uvI1n6JuRXI+S1Mco54YmYTwcn6Aeic+kssXi8XpXC4V3t7/ADuTNKaQJdScAAAAAElFTkSuQmCC)](https://mybinder.org/v2/gist/jonasbb/18b9aece4c17f617b1c2b3946d29eeb0/HEAD?filepath=serde-with-demo.ipynb) + +--- + +This crate provides custom de/serialization helpers to use in combination with [serde's with-annotation][with-annotation] and with the improved [`serde_as`][as-annotation]-annotation. +Some common use cases are: + +* De/Serializing a type using the `Display` and `FromStr` traits, e.g., for `u8`, `url::Url`, or `mime::Mime`. + Check [`DisplayFromStr`][] or [`serde_with::rust::display_fromstr`][display_fromstr] for details. +* Support for arrays larger than 32 elements or using const generics. + With `serde_as` large arrays are supported, even if they are nested in other types. + `[bool; 64]`, `Option<[u8; M]>`, and `Box<[[u8; 64]; N]>` are all supported, as [this examples shows](#large-and-const-generic-arrays). +* Skip serializing all empty `Option` types with [`#[skip_serializing_none]`][skip_serializing_none]. +* Apply a prefix to each field name of a struct, without changing the de/serialize implementations of the struct using [`with_prefix!`][]. +* Deserialize a comma separated list like `#hash,#tags,#are,#great` into a `Vec<String>`. + Check the documentation for [`serde_with::rust::StringWithSeparator::<CommaSeparator>`][StringWithSeparator]. + +### Getting Help + +**Check out the [user guide][user guide] to find out more tips and tricks about this crate.** + +For further help using this crate you can [open a new discussion](https://github.com/jonasbb/serde_with/discussions/new) or ask on [users.rust-lang.org](https://users.rust-lang.org/). +For bugs please open a [new issue](https://github.com/jonasbb/serde_with/issues/new) on Github. + +## Use `serde_with` in your Project + +Add this to your `Cargo.toml`: + +```toml +[dependencies.serde_with] +version = "1.12.1" +features = [ "..." ] +``` + +The crate contains different features for integration with other common crates. +Check the [feature flags][] section for information about all available features. + +## Examples + +Annotate your struct or enum to enable the custom de/serializer. + +### `DisplayFromStr` + +```rust +#[serde_as] +#[derive(Deserialize, Serialize)] +struct Foo { + // Serialize with Display, deserialize with FromStr + #[serde_as(as = "DisplayFromStr")] + bar: u8, +} + +// This will serialize +Foo {bar: 12} + +// into this JSON +{"bar": "12"} +``` + +### Large and const-generic arrays + +serde does not support arrays with more than 32 elements or using const-generics. +The `serde_as` attribute allows to circumvent this restriction, even for nested types and nested arrays. + +```rust +#[serde_as] +#[derive(Deserialize, Serialize)] +struct Arrays<const N: usize, const M: usize> { + #[serde_as(as = "[_; N]")] + constgeneric: [bool; N], + + #[serde_as(as = "Box<[[_; 64]; N]>")] + nested: Box<[[u8; 64]; N]>, + + #[serde_as(as = "Option<[_; M]>")] + optional: Option<[u8; M]>, +} + +// This allows us to serialize a struct like this +let arrays: Arrays<100, 128> = Arrays { + constgeneric: [true; 100], + nested: Box::new([[111; 64]; 100]), + optional: Some([222; 128]) +}; +assert!(serde_json::to_string(&arrays).is_ok()); +``` + +### `skip_serializing_none` + +This situation often occurs with JSON, but other formats also support optional fields. +If many fields are optional, putting the annotations on the structs can become tedious. + +```rust +#[skip_serializing_none] +#[derive(Deserialize, Serialize)] +struct Foo { + a: Option<usize>, + b: Option<usize>, + c: Option<usize>, + d: Option<usize>, + e: Option<usize>, + f: Option<usize>, + g: Option<usize>, +} + +// This will serialize +Foo {a: None, b: None, c: None, d: Some(4), e: None, f: None, g: Some(7)} + +// into this JSON +{"d": 4, "g": 7} +``` + +### Advanced `serde_as` usage + +This example is mainly supposed to highlight the flexibility of the `serde_as`-annotation compared to [serde's with-annotation][with-annotation]. +More details about `serde_as` can be found in the [user guide][]. + +```rust +#[serde_as] +#[derive(Deserialize, Serialize)] +struct Foo { + // Serialize them into a list of number as seconds + #[serde_as(as = "Vec<DurationSeconds>")] + durations: Vec<Duration>, + // We can treat a Vec like a map with duplicates. + // JSON only allows string keys, so convert i32 to strings + // The bytes will be hex encoded + #[serde_as(as = "BTreeMap<DisplayFromStr, Hex>")] + bytes: Vec<(i32, Vec<u8>)>, +} + +// This will serialize +Foo { + durations: vec![Duration::new(5, 0), Duration::new(3600, 0), Duration::new(0, 0)], + bytes: vec![ + (1, vec![0, 1, 2]), + (-100, vec![100, 200, 255]), + (1, vec![0, 111, 222]), + ], +} + +// into this JSON +{ + "durations": [5, 3600, 0], + "bytes": { + "1": "000102", + "-100": "64c8ff", + "1": "006fde" + } +} +``` + +[`DisplayFromStr`]: https://docs.rs/serde_with/1.12.1/serde_with/struct.DisplayFromStr.html +[`with_prefix!`]: https://docs.rs/serde_with/1.12.1/serde_with/macro.with_prefix.html +[display_fromstr]: https://docs.rs/serde_with/1.12.1/serde_with/rust/display_fromstr/index.html +[feature flags]: https://docs.rs/serde_with/1.12.1/serde_with/guide/feature_flags/index.html +[skip_serializing_none]: https://docs.rs/serde_with/1.12.1/serde_with/attr.skip_serializing_none.html +[StringWithSeparator]: https://docs.rs/serde_with/1.12.1/serde_with/rust/struct.StringWithSeparator.html +[user guide]: https://docs.rs/serde_with/1.12.1/serde_with/guide/index.html +[with-annotation]: https://serde.rs/field-attrs.html#with +[as-annotation]: https://docs.rs/serde_with/1.12.1/serde_with/guide/serde_as/index.html + +## License + +Licensed under either of + +* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +## Contribution + +For detailed contribution instructions please read [`CONTRIBUTING.md`]. + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall +be dual licensed as above, without any additional terms or conditions. + +[`CONTRIBUTING.md`]: https://github.com/jonasbb/serde_with/blob/master/CONTRIBUTING.md diff --git a/third_party/rust/serde_with_macros/src/lib.rs b/third_party/rust/serde_with_macros/src/lib.rs new file mode 100644 index 0000000000..9798c13d32 --- /dev/null +++ b/third_party/rust/serde_with_macros/src/lib.rs @@ -0,0 +1,1016 @@ +#![forbid(unsafe_code)] +#![warn( + clippy::semicolon_if_nothing_returned, + missing_copy_implementations, + // missing_crate_level_docs, not available in MSRV + missing_debug_implementations, + missing_docs, + rust_2018_idioms, + trivial_casts, + trivial_numeric_casts, + unused_extern_crates, + unused_import_braces, + unused_qualifications, + variant_size_differences +)] +#![doc(test(attr(forbid(unsafe_code))))] +#![doc(test(attr(deny( + missing_copy_implementations, + missing_debug_implementations, + trivial_casts, + trivial_numeric_casts, + unused_extern_crates, + unused_import_braces, + unused_qualifications, +))))] +#![doc(test(attr(warn(rust_2018_idioms))))] +// Not needed for 2018 edition and conflicts with `rust_2018_idioms` +#![doc(test(no_crate_inject))] +#![doc(html_root_url = "https://docs.rs/serde_with_macros/1.5.2")] +// Necessary to silence the warning about clippy::unknown_clippy_lints on nightly +#![allow(renamed_and_removed_lints)] +// Necessary for nightly clippy lints +#![allow(clippy::unknown_clippy_lints)] + +//! proc-macro extensions for [`serde_with`]. +//! +//! This crate should **NEVER** be used alone. +//! All macros **MUST** be used via the re-exports in the [`serde_with`] crate. +//! +//! [`serde_with`]: https://crates.io/crates/serde_with/ + +#[allow(unused_extern_crates)] +extern crate proc_macro; + +mod utils; + +use crate::utils::{split_with_de_lifetime, DeriveOptions, IteratorExt as _}; +use darling::util::Override; +use darling::{Error as DarlingError, FromField, FromMeta}; +use proc_macro::TokenStream; +use proc_macro2::{Span, TokenStream as TokenStream2}; +use quote::quote; +use syn::punctuated::Pair; +use syn::spanned::Spanned; +use syn::{ + parse_macro_input, parse_quote, AttributeArgs, DeriveInput, Error, Field, Fields, + GenericArgument, ItemEnum, ItemStruct, Meta, NestedMeta, Path, PathArguments, ReturnType, Type, +}; + +/// Apply function on every field of structs or enums +fn apply_function_to_struct_and_enum_fields<F>( + input: TokenStream, + function: F, +) -> Result<TokenStream2, Error> +where + F: Copy, + F: Fn(&mut Field) -> Result<(), String>, +{ + /// Handle a single struct or a single enum variant + fn apply_on_fields<F>(fields: &mut Fields, function: F) -> Result<(), Error> + where + F: Fn(&mut Field) -> Result<(), String>, + { + match fields { + // simple, no fields, do nothing + Fields::Unit => Ok(()), + Fields::Named(ref mut fields) => fields + .named + .iter_mut() + .map(|field| function(field).map_err(|err| Error::new(field.span(), err))) + .collect_error(), + Fields::Unnamed(ref mut fields) => fields + .unnamed + .iter_mut() + .map(|field| function(field).map_err(|err| Error::new(field.span(), err))) + .collect_error(), + } + } + + // For each field in the struct given by `input`, add the `skip_serializing_if` attribute, + // if and only if, it is of type `Option` + if let Ok(mut input) = syn::parse::<ItemStruct>(input.clone()) { + apply_on_fields(&mut input.fields, function)?; + Ok(quote!(#input)) + } else if let Ok(mut input) = syn::parse::<ItemEnum>(input) { + input + .variants + .iter_mut() + .map(|variant| apply_on_fields(&mut variant.fields, function)) + .collect_error()?; + Ok(quote!(#input)) + } else { + Err(Error::new( + Span::call_site(), + "The attribute can only be applied to struct or enum definitions.", + )) + } +} + +/// Like [apply_function_to_struct_and_enum_fields] but for darling errors +fn apply_function_to_struct_and_enum_fields_darling<F>( + input: TokenStream, + serde_with_crate_path: &Path, + function: F, +) -> Result<TokenStream2, DarlingError> +where + F: Copy, + F: Fn(&mut Field) -> Result<(), DarlingError>, +{ + /// Handle a single struct or a single enum variant + fn apply_on_fields<F>(fields: &mut Fields, function: F) -> Result<(), DarlingError> + where + F: Fn(&mut Field) -> Result<(), DarlingError>, + { + match fields { + // simple, no fields, do nothing + Fields::Unit => Ok(()), + Fields::Named(ref mut fields) => { + let errors: Vec<DarlingError> = fields + .named + .iter_mut() + .map(|field| function(field).map_err(|err| err.with_span(&field))) + // turn the Err variant into the Some, such that we only collect errors + .filter_map(|res| match res { + Err(e) => Some(e), + Ok(()) => None, + }) + .collect(); + if errors.is_empty() { + Ok(()) + } else { + Err(DarlingError::multiple(errors)) + } + } + Fields::Unnamed(ref mut fields) => { + let errors: Vec<DarlingError> = fields + .unnamed + .iter_mut() + .map(|field| function(field).map_err(|err| err.with_span(&field))) + // turn the Err variant into the Some, such that we only collect errors + .filter_map(|res| match res { + Err(e) => Some(e), + Ok(()) => None, + }) + .collect(); + if errors.is_empty() { + Ok(()) + } else { + Err(DarlingError::multiple(errors)) + } + } + } + } + + // Add a dummy derive macro which consumes (makes inert) all field attributes + let consume_serde_as_attribute = parse_quote!( + #[derive(#serde_with_crate_path::__private_consume_serde_as_attributes)] + ); + + // For each field in the struct given by `input`, add the `skip_serializing_if` attribute, + // if and only if, it is of type `Option` + if let Ok(mut input) = syn::parse::<ItemStruct>(input.clone()) { + apply_on_fields(&mut input.fields, function)?; + input.attrs.push(consume_serde_as_attribute); + Ok(quote!(#input)) + } else if let Ok(mut input) = syn::parse::<ItemEnum>(input) { + let errors: Vec<DarlingError> = input + .variants + .iter_mut() + .map(|variant| apply_on_fields(&mut variant.fields, function)) + // turn the Err variant into the Some, such that we only collect errors + .filter_map(|res| match res { + Err(e) => Some(e), + Ok(()) => None, + }) + .collect(); + if errors.is_empty() { + input.attrs.push(consume_serde_as_attribute); + Ok(quote!(#input)) + } else { + Err(DarlingError::multiple(errors)) + } + } else { + Err(DarlingError::custom( + "The attribute can only be applied to struct or enum definitions.", + ) + .with_span(&Span::call_site())) + } +} + +/// Add `skip_serializing_if` annotations to [`Option`] fields. +/// +/// The attribute can be added to structs and enums. +/// +/// Import this attribute with `use serde_with::skip_serializing_none;`. +/// +/// # Example +/// +/// JSON APIs sometimes have many optional values. +/// Missing values should not be serialized, to keep the serialized format smaller. +/// Such a data type might look like: +/// +/// ```rust +/// # use serde::Serialize; +/// # +/// #[derive(Serialize)] +/// struct Data { +/// #[serde(skip_serializing_if = "Option::is_none")] +/// a: Option<String>, +/// #[serde(skip_serializing_if = "Option::is_none")] +/// b: Option<u64>, +/// #[serde(skip_serializing_if = "Option::is_none")] +/// c: Option<String>, +/// #[serde(skip_serializing_if = "Option::is_none")] +/// d: Option<bool>, +/// } +/// ``` +/// +/// The `skip_serializing_if` annotation is repetitive and harms readability. +/// Instead, the same struct can be written as: +/// +/// ```rust +/// # use serde::Serialize; +/// # use serde_with_macros::skip_serializing_none; +/// #[skip_serializing_none] +/// #[derive(Serialize)] +/// struct Data { +/// a: Option<String>, +/// b: Option<u64>, +/// c: Option<String>, +/// // Always serialize field d even if None +/// #[serialize_always] +/// d: Option<bool>, +/// } +/// ``` +/// +/// Existing `skip_serializing_if` annotations will not be altered. +/// +/// If some values should always be serialized, then the `serialize_always` can be used. +/// +/// # Limitations +/// +/// The `serialize_always` cannot be used together with a manual `skip_serializing_if` annotations, as these conflict in their meaning. +/// A compile error will be generated if this occurs. +/// +/// The `skip_serializing_none` only works if the type is called [`Option`], [`std::option::Option`], or [`core::option::Option`]. +/// Type aliasing an [`Option`] and giving it another name, will cause this field to be ignored. +/// This cannot be supported, as proc-macros run before type checking, thus it is not possible to determine if a type alias refers to an [`Option`]. +/// +/// ```rust +/// # use serde::Serialize; +/// # use serde_with_macros::skip_serializing_none; +/// type MyOption<T> = Option<T>; +/// +/// #[skip_serializing_none] +/// #[derive(Serialize)] +/// struct Data { +/// a: MyOption<String>, // This field will not be skipped +/// } +/// ``` +/// +/// Likewise, if you import a type and name it `Option`, the `skip_serializing_if` attributes will be added and compile errors will occur, if `Option::is_none` is not a valid function. +/// Here the function `Vec::is_none` does not exist and therefore the example fails to compile. +/// +/// ```rust,compile_fail +/// # use serde::Serialize; +/// # use serde_with_macros::skip_serializing_none; +/// use std::vec::Vec as Option; +/// +/// #[skip_serializing_none] +/// #[derive(Serialize)] +/// struct Data { +/// a: Option<String>, +/// } +/// ``` +#[proc_macro_attribute] +pub fn skip_serializing_none(_args: TokenStream, input: TokenStream) -> TokenStream { + let res = match apply_function_to_struct_and_enum_fields( + input, + skip_serializing_none_add_attr_to_field, + ) { + Ok(res) => res, + Err(err) => err.to_compile_error(), + }; + TokenStream::from(res) +} + +/// Add the skip_serializing_if annotation to each field of the struct +fn skip_serializing_none_add_attr_to_field(field: &mut Field) -> Result<(), String> { + if let Type::Path(path) = &field.ty { + if is_std_option(&path.path) { + let has_skip_serializing_if = + field_has_attribute(field, "serde", "skip_serializing_if"); + + // Remove the `serialize_always` attribute + let mut has_always_attr = false; + field.attrs.retain(|attr| { + let has_attr = attr.path.is_ident("serialize_always"); + has_always_attr |= has_attr; + !has_attr + }); + + // Error on conflicting attributes + if has_always_attr && has_skip_serializing_if { + let mut msg = r#"The attributes `serialize_always` and `serde(skip_serializing_if = "...")` cannot be used on the same field"#.to_string(); + if let Some(ident) = &field.ident { + msg += ": `"; + msg += &ident.to_string(); + msg += "`"; + } + msg += "."; + return Err(msg); + } + + // Do nothing if `skip_serializing_if` or `serialize_always` is already present + if has_skip_serializing_if || has_always_attr { + return Ok(()); + } + + // Add the `skip_serializing_if` attribute + let attr = parse_quote!( + #[serde(skip_serializing_if = "Option::is_none")] + ); + field.attrs.push(attr); + } else { + // Warn on use of `serialize_always` on non-Option fields + let has_attr = field + .attrs + .iter() + .any(|attr| attr.path.is_ident("serialize_always")); + if has_attr { + return Err( + "`serialize_always` may only be used on fields of type `Option`.".into(), + ); + } + } + } + Ok(()) +} + +/// Return `true`, if the type path refers to `std::option::Option` +/// +/// Accepts +/// +/// * `Option` +/// * `std::option::Option`, with or without leading `::` +/// * `core::option::Option`, with or without leading `::` +fn is_std_option(path: &Path) -> bool { + (path.leading_colon.is_none() && path.segments.len() == 1 && path.segments[0].ident == "Option") + || (path.segments.len() == 3 + && (path.segments[0].ident == "std" || path.segments[0].ident == "core") + && path.segments[1].ident == "option" + && path.segments[2].ident == "Option") +} + +/// Determine if the `field` has an attribute with given `namespace` and `name` +/// +/// On the example of +/// `#[serde(skip_serializing_if = "Option::is_none")]` +/// +/// * `serde` is the outermost path, here namespace +/// * it contains a Meta::List +/// * which contains in another Meta a Meta::NameValue +/// * with the name being `skip_serializing_if` +fn field_has_attribute(field: &Field, namespace: &str, name: &str) -> bool { + for attr in &field.attrs { + if attr.path.is_ident(namespace) { + // Ignore non parsable attributes, as these are not important for us + if let Ok(Meta::List(expr)) = attr.parse_meta() { + for expr in expr.nested { + if let NestedMeta::Meta(Meta::NameValue(expr)) = expr { + if let Some(ident) = expr.path.get_ident() { + if *ident == name { + return true; + } + } + } + } + } + } + } + false +} + +/// Convenience macro to use the [`serde_as`] system. +/// +/// The [`serde_as`] system is designed as a more flexible alternative to serde's with-annotation. +/// +/// # Example +/// +/// ```rust,ignore +/// use serde_with::{serde_as, DisplayFromStr}; +/// use std::collections::HashMap; +/// +/// #[serde_as] +/// #[derive(Serialize, Deserialize)] +/// struct Data { +/// /// Serialize into number +/// #[serde_as(as = "_")] +/// a: u32, +/// +/// /// Serialize into String +/// #[serde_as(as = "DisplayFromStr")] +/// b: u32, +/// +/// /// Serialize into a map from String to String +/// #[serde_as(as = "HashMap<DisplayFromStr, _>")] +/// c: Vec<(u32, String)>, +/// } +/// ``` +/// +/// # Alternative path to `serde_with` crate +/// +/// If `serde_with` is not available at the default path, its path should be specified with the +/// `crate` argument. See [re-exporting `serde_as`] for more use case information. +/// +/// ```rust,ignore +/// #[serde_as(crate = "::some_other_lib::serde_with")] +/// #[derive(Deserialize)] +/// struct Data { +/// #[serde_as(as = "_")] +/// a: u32, +/// } +/// ``` +/// +/// # What this macro does +/// +/// The `serde_as` macro only serves a convenience function. +/// All the steps it performs, can easily be done manually, in case the cost of an attribute macro is deemed to high. +/// The functionality can best be described with an example. +/// +/// ```rust,ignore +/// #[serde_as] +/// #[derive(serde::Serialize)] +/// struct Foo { +/// #[serde_as(as = "Vec<_>")] +/// bar: Vec<u32>, +/// } +/// ``` +/// +/// 1. All the placeholder type `_` will be replaced with `::serde_with::Same`. +/// The placeholder type `_` marks all the places where the types `Serialize` implementation should be used. +/// In the example, it means that the `u32` values will serialize with the `Serialize` implementation of `u32`. +/// The `Same` type implements `SerializeAs` whenever the underlying type implements `Serialize` and is used to make the two traits compatible. +/// +/// If you specify a custom path for `serde_with` via the `crate` attribute, the path to the `Same` type will be altered accordingly. +/// 2. Wrap the type from the annotation inside a `::serde_with::As`. +/// In the above example we know have something like `::serde_with::As::<Vec<::serde_with::Same>>`. +/// The `As` type acts as the opposite of the `Same` type. +/// It allows using a `SerializeAs` type whenever a `Serialize` is required. +/// 3. Translate the `*as` attributes into the serde equivalent ones. +/// `#[serde_as(as = ...)]` will become `#[serde(with = ...)]`. +/// Similarly, `serialize_as` is translated to `serialize_with`. +/// +/// The field attributes will be kept on the struct/enum such that other macros can use them too. +/// 4. It searches `#[serde_as(as = ...)]` if there is a type named `BorrowCow` under any path. +/// If `BorrowCow` is found, the attribute `#[serde(borrow)]` is added to the field. +/// If `#[serde(borrow)]` or `#[serde(borrow = "...")]` is already present, this step will be skipped. +/// +/// After all these steps, the code snippet will have transformed into roughly this. +/// +/// ```rust,ignore +/// #[derive(serde::Serialize)] +/// struct Foo { +/// #[serde_as(as = "Vec<_>")] +/// #[serde(with = "::serde_with::As::<Vec<::serde_with::Same>>")] +/// bar: Vec<u32>, +/// } +/// ``` +/// +/// [`serde_as`]: https://docs.rs/serde_with/1.12.1/serde_with/guide/index.html +/// [re-exporting `serde_as`]: https://docs.rs/serde_with/1.12.1/serde_with/guide/serde_as/index.html#re-exporting-serde_as +#[proc_macro_attribute] +pub fn serde_as(args: TokenStream, input: TokenStream) -> TokenStream { + #[derive(FromMeta, Debug)] + struct SerdeContainerOptions { + #[darling(rename = "crate", default)] + alt_crate_path: Option<String>, + } + + let args: AttributeArgs = parse_macro_input!(args); + let container_options = match SerdeContainerOptions::from_list(&args) { + Ok(v) => v, + Err(e) => { + return TokenStream::from(e.write_errors()); + } + }; + + let serde_with_crate_path = container_options + .alt_crate_path + .as_deref() + .unwrap_or("::serde_with"); + let serde_with_crate_path = match syn::parse_str(serde_with_crate_path) { + Ok(path) => path, + Err(err) => return TokenStream::from(DarlingError::from(err).write_errors()), + }; + + // Convert any error message into a nice compiler error + let res = match apply_function_to_struct_and_enum_fields_darling( + input, + &serde_with_crate_path, + |field| serde_as_add_attr_to_field(field, &serde_with_crate_path), + ) { + Ok(res) => res, + Err(err) => err.write_errors(), + }; + TokenStream::from(res) +} + +/// Inspect the field and convert the `serde_as` attribute into the classical `serde` +fn serde_as_add_attr_to_field( + field: &mut Field, + serde_with_crate_path: &Path, +) -> Result<(), DarlingError> { + #[derive(FromField, Debug)] + #[darling(attributes(serde_as))] + struct SerdeAsOptions { + #[darling(rename = "as", default)] + r#as: Option<Type>, + #[darling(default)] + deserialize_as: Option<Type>, + #[darling(default)] + serialize_as: Option<Type>, + } + + impl SerdeAsOptions { + fn has_any_set(&self) -> bool { + self.r#as.is_some() || self.deserialize_as.is_some() || self.serialize_as.is_some() + } + } + + #[derive(FromField, Debug)] + #[darling(attributes(serde), allow_unknown_fields)] + struct SerdeOptions { + #[darling(default)] + with: Option<String>, + #[darling(default)] + deserialize_with: Option<String>, + #[darling(default)] + serialize_with: Option<String>, + + #[darling(default)] + borrow: Option<Override<String>>, + } + + impl SerdeOptions { + fn has_any_set(&self) -> bool { + self.with.is_some() || self.deserialize_with.is_some() || self.serialize_with.is_some() + } + } + + // Check if there even is any `serde_as` attribute and exit early if not. + if !field + .attrs + .iter() + .any(|attr| attr.path.is_ident("serde_as")) + { + return Ok(()); + } + let serde_as_options = SerdeAsOptions::from_field(field)?; + let serde_options = SerdeOptions::from_field(field)?; + + let mut errors = Vec::new(); + if !serde_as_options.has_any_set() { + errors.push(DarlingError::custom("An empty `serde_as` attribute on a field has no effect. You are missing an `as`, `serialize_as`, or `deserialize_as` parameter.")); + } + + // Check if there are any conflicting attributes + if serde_as_options.has_any_set() && serde_options.has_any_set() { + errors.push(DarlingError::custom("Cannot combine `serde_as` with serde's `with`, `deserialize_with`, or `serialize_with`.")); + } + + if serde_as_options.r#as.is_some() && serde_as_options.deserialize_as.is_some() { + errors.push(DarlingError::custom("Cannot combine `as` with `deserialize_as`. Use `serialize_as` to specify different serialization code.")); + } else if serde_as_options.r#as.is_some() && serde_as_options.serialize_as.is_some() { + errors.push(DarlingError::custom("Cannot combine `as` with `serialize_as`. Use `deserialize_as` to specify different deserialization code.")); + } + + if !errors.is_empty() { + return Err(DarlingError::multiple(errors)); + } + + let type_same = &syn::parse_quote!(#serde_with_crate_path::Same); + let type_borrowcow = &syn::parse_quote!(BorrowCow); + if let Some(type_) = serde_as_options.r#as { + // If the field is not borrowed yet, check if we need to borrow it. + if serde_options.borrow.is_none() && has_type_embedded(&type_, type_borrowcow) { + let attr_borrow = parse_quote!(#[serde(borrow)]); + field.attrs.push(attr_borrow); + } + + let replacement_type = replace_infer_type_with_type(type_, type_same); + let attr_inner_tokens = quote!(#serde_with_crate_path::As::<#replacement_type>).to_string(); + let attr = parse_quote!(#[serde(with = #attr_inner_tokens)]); + field.attrs.push(attr); + } + if let Some(type_) = serde_as_options.deserialize_as { + // If the field is not borrowed yet, check if we need to borrow it. + if serde_options.borrow.is_none() && has_type_embedded(&type_, type_borrowcow) { + let attr_borrow = parse_quote!(#[serde(borrow)]); + field.attrs.push(attr_borrow); + } + + let replacement_type = replace_infer_type_with_type(type_, type_same); + let attr_inner_tokens = + quote!(#serde_with_crate_path::As::<#replacement_type>::deserialize).to_string(); + let attr = parse_quote!(#[serde(deserialize_with = #attr_inner_tokens)]); + field.attrs.push(attr); + } + if let Some(type_) = serde_as_options.serialize_as { + let replacement_type = replace_infer_type_with_type(type_, type_same); + let attr_inner_tokens = + quote!(#serde_with_crate_path::As::<#replacement_type>::serialize).to_string(); + let attr = parse_quote!(#[serde(serialize_with = #attr_inner_tokens)]); + field.attrs.push(attr); + } + + Ok(()) +} + +/// Recursively replace all occurrences of `_` with `replacement` in a [Type][] +/// +/// The [serde_as][macro@serde_as] macro allows to use the infer type, i.e., `_`, as shortcut for `serde_with::As`. +/// This function replaces all occurrences of the infer type with another type. +fn replace_infer_type_with_type(to_replace: Type, replacement: &Type) -> Type { + match to_replace { + // Base case + // Replace the infer type with the actual replacement type + Type::Infer(_) => replacement.clone(), + + // Recursive cases + // Iterate through all positions where a type could occur and recursively call this function + Type::Array(mut inner) => { + *inner.elem = replace_infer_type_with_type(*inner.elem, replacement); + Type::Array(inner) + } + Type::Group(mut inner) => { + *inner.elem = replace_infer_type_with_type(*inner.elem, replacement); + Type::Group(inner) + } + Type::Never(inner) => Type::Never(inner), + Type::Paren(mut inner) => { + *inner.elem = replace_infer_type_with_type(*inner.elem, replacement); + Type::Paren(inner) + } + Type::Path(mut inner) => { + match inner.path.segments.pop() { + Some(Pair::End(mut t)) | Some(Pair::Punctuated(mut t, _)) => { + t.arguments = match t.arguments { + PathArguments::None => PathArguments::None, + PathArguments::AngleBracketed(mut inner) => { + // Iterate over the args between the angle brackets + inner.args = inner + .args + .into_iter() + .map(|generic_argument| match generic_argument { + // replace types within the generics list, but leave other stuff like lifetimes untouched + GenericArgument::Type(type_) => GenericArgument::Type( + replace_infer_type_with_type(type_, replacement), + ), + ga => ga, + }) + .collect(); + PathArguments::AngleBracketed(inner) + } + PathArguments::Parenthesized(mut inner) => { + inner.inputs = inner + .inputs + .into_iter() + .map(|type_| replace_infer_type_with_type(type_, replacement)) + .collect(); + inner.output = match inner.output { + ReturnType::Type(arrow, mut type_) => { + *type_ = replace_infer_type_with_type(*type_, replacement); + ReturnType::Type(arrow, type_) + } + default => default, + }; + PathArguments::Parenthesized(inner) + } + }; + inner.path.segments.push(t); + } + None => {} + } + Type::Path(inner) + } + Type::Ptr(mut inner) => { + *inner.elem = replace_infer_type_with_type(*inner.elem, replacement); + Type::Ptr(inner) + } + Type::Reference(mut inner) => { + *inner.elem = replace_infer_type_with_type(*inner.elem, replacement); + Type::Reference(inner) + } + Type::Slice(mut inner) => { + *inner.elem = replace_infer_type_with_type(*inner.elem, replacement); + Type::Slice(inner) + } + Type::Tuple(mut inner) => { + inner.elems = inner + .elems + .into_pairs() + .map(|pair| match pair { + Pair::Punctuated(type_, p) => { + Pair::Punctuated(replace_infer_type_with_type(type_, replacement), p) + } + Pair::End(type_) => Pair::End(replace_infer_type_with_type(type_, replacement)), + }) + .collect(); + Type::Tuple(inner) + } + + // Pass unknown types or non-handleable types (e.g., bare Fn) without performing any replacements + type_ => type_, + } +} + +/// Check if a type ending in the `syn::Ident` `embedded_type` is contained in `type_`. +fn has_type_embedded(type_: &Type, embedded_type: &syn::Ident) -> bool { + match type_ { + // Base cases + Type::Infer(_) => false, + Type::Never(_inner) => false, + + // Recursive cases + // Iterate through all positions where a type could occur and recursively call this function + Type::Array(inner) => has_type_embedded(&inner.elem, embedded_type), + Type::Group(inner) => has_type_embedded(&inner.elem, embedded_type), + Type::Paren(inner) => has_type_embedded(&inner.elem, embedded_type), + Type::Path(inner) => { + match inner.path.segments.last() { + Some(t) => { + if t.ident == *embedded_type { + return true; + } + + match &t.arguments { + PathArguments::None => false, + PathArguments::AngleBracketed(inner) => { + // Iterate over the args between the angle brackets + inner + .args + .iter() + .any(|generic_argument| match generic_argument { + // replace types within the generics list, but leave other stuff like lifetimes untouched + GenericArgument::Type(type_) => { + has_type_embedded(type_, embedded_type) + } + _ga => false, + }) + } + PathArguments::Parenthesized(inner) => { + inner + .inputs + .iter() + .any(|type_| has_type_embedded(type_, embedded_type)) + || match &inner.output { + ReturnType::Type(_arrow, type_) => { + has_type_embedded(type_, embedded_type) + } + _default => false, + } + } + } + } + None => false, + } + } + Type::Ptr(inner) => has_type_embedded(&inner.elem, embedded_type), + Type::Reference(inner) => has_type_embedded(&inner.elem, embedded_type), + Type::Slice(inner) => has_type_embedded(&inner.elem, embedded_type), + Type::Tuple(inner) => inner.elems.pairs().any(|pair| match pair { + Pair::Punctuated(type_, _) | Pair::End(type_) => { + has_type_embedded(type_, embedded_type) + } + }), + + // Pass unknown types or non-handleable types (e.g., bare Fn) without performing any replacements + _type_ => false, + } +} + +/// Deserialize value by using its [`FromStr`] implementation +/// +/// This is an alternative way to implement `Deserialize` for types which also implement [`FromStr`] by deserializing the type from string. +/// Ensure that the struct/enum also implements [`FromStr`]. +/// If the implementation is missing, you will get an error message like +/// ```text +/// error[E0277]: the trait bound `Struct: std::str::FromStr` is not satisfied +/// ``` +/// Additionally, `FromStr::Err` **must** implement [`Display`] as otherwise you will see a rather unhelpful error message +/// +/// Serialization with [`Display`] is available with the matching [`SerializeDisplay`] derive. +/// +/// # Attributes +/// +/// Attributes for the derive can be specified via the `#[serde_with(...)]` attribute on the struct or enum. +/// Currently, these arguments to the attribute are possible: +/// +/// * **`#[serde_with(crate = "...")]`**: This allows using `DeserializeFromStr` when `serde_with` is not available from the crate root. +/// This happens while [renaming dependencies in Cargo.toml][cargo-toml-rename] or when re-exporting the macro from a different crate. +/// +/// This argument is analogue to [serde's crate argument][serde-crate] and the [crate argument to `serde_as`][serde-as-crate]. +/// +/// # Example +/// +/// ```rust,ignore +/// use std::str::FromStr; +/// +/// #[derive(DeserializeFromStr)] +/// struct A { +/// a: u32, +/// b: bool, +/// } +/// +/// impl FromStr for A { +/// type Err = String; +/// +/// /// Parse a value like `123<>true` +/// fn from_str(s: &str) -> Result<Self, Self::Err> { +/// let mut parts = s.split("<>"); +/// let number = parts +/// .next() +/// .ok_or_else(|| "Missing first value".to_string())? +/// .parse() +/// .map_err(|err: ParseIntError| err.to_string())?; +/// let bool = parts +/// .next() +/// .ok_or_else(|| "Missing second value".to_string())? +/// .parse() +/// .map_err(|err: ParseBoolError| err.to_string())?; +/// Ok(Self { a: number, b: bool }) +/// } +/// } +/// +/// let a: A = serde_json::from_str("\"159<>true\"").unwrap(); +/// assert_eq!(A { a: 159, b: true }, a); +/// ``` +/// +/// [`Display`]: std::fmt::Display +/// [`FromStr`]: std::str::FromStr +/// [cargo-toml-rename]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml +/// [serde-as-crate]: https://docs.rs/serde_with/1.12.1/serde_with/guide/serde_as/index.html#re-exporting-serde_as +/// [serde-crate]: https://serde.rs/container-attrs.html#crate +#[proc_macro_derive(DeserializeFromStr, attributes(serde_with))] +pub fn derive_deserialize_fromstr(item: TokenStream) -> TokenStream { + let input: DeriveInput = parse_macro_input!(item); + let derive_options = match DeriveOptions::from_derive_input(&input) { + Ok(opt) => opt, + Err(err) => { + return err; + } + }; + TokenStream::from(deserialize_fromstr( + input, + derive_options.get_serde_with_path(), + )) +} + +fn deserialize_fromstr(mut input: DeriveInput, serde_with_crate_path: Path) -> TokenStream2 { + let ident = input.ident; + let where_clause = &mut input.generics.make_where_clause().predicates; + where_clause.push(parse_quote!(Self: ::std::str::FromStr)); + where_clause.push(parse_quote!( + <Self as ::std::str::FromStr>::Err: ::std::fmt::Display + )); + let (de_impl_generics, ty_generics, where_clause) = split_with_de_lifetime(&input.generics); + quote! { + #[automatically_derived] + impl #de_impl_generics #serde_with_crate_path::serde::Deserialize<'de> for #ident #ty_generics #where_clause { + fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error> + where + D: #serde_with_crate_path::serde::Deserializer<'de>, + { + struct Helper<S>(::std::marker::PhantomData<S>); + + impl<'de, S> #serde_with_crate_path::serde::de::Visitor<'de> for Helper<S> + where + S: ::std::str::FromStr, + <S as ::std::str::FromStr>::Err: ::std::fmt::Display, + { + type Value = S; + + fn expecting(&self, formatter: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::std::write!(formatter, "string") + } + + fn visit_str<E>(self, value: &str) -> ::std::result::Result<Self::Value, E> + where + E: #serde_with_crate_path::serde::de::Error, + { + value.parse::<Self::Value>().map_err(#serde_with_crate_path::serde::de::Error::custom) + } + + fn visit_bytes<E>(self, value: &[u8]) -> ::std::result::Result<Self::Value, E> + where + E: #serde_with_crate_path::serde::de::Error, + { + let utf8 = ::std::str::from_utf8(value).map_err(#serde_with_crate_path::serde::de::Error::custom)?; + self.visit_str(utf8) + } + } + + deserializer.deserialize_str(Helper(::std::marker::PhantomData)) + } + } + } +} + +/// Serialize value by using it's [`Display`] implementation +/// +/// This is an alternative way to implement `Serialize` for types which also implement [`Display`] by serializing the type as string. +/// Ensure that the struct/enum also implements [`Display`]. +/// If the implementation is missing, you will get an error message like +/// ```text +/// error[E0277]: `Struct` doesn't implement `std::fmt::Display` +/// ``` +/// +/// Deserialization with [`FromStr`] is available with the matching [`DeserializeFromStr`] derive. +/// +/// # Attributes +/// +/// Attributes for the derive can be specified via the `#[serde_with(...)]` attribute on the struct or enum. +/// Currently, these arguments to the attribute are possible: +/// +/// * **`#[serde_with(crate = "...")]`**: This allows using `SerializeDisplay` when `serde_with` is not available from the crate root. +/// This happens while [renaming dependencies in Cargo.toml][cargo-toml-rename] or when re-exporting the macro from a different crate. +/// +/// This argument is analogue to [serde's crate argument][serde-crate] and the [crate argument to `serde_as`][serde-as-crate]. +/// +/// # Example +/// +/// ```rust,ignore +/// use std::fmt; +/// +/// #[derive(SerializeDisplay)] +/// struct A { +/// a: u32, +/// b: bool, +/// } +/// +/// impl fmt::Display for A { +/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +/// write!(f, "{}<>{}", self.a, self.b) +/// } +/// } +/// +/// let a = A { a: 123, b: false }; +/// assert_eq!(r#""123<>false""#, serde_json::to_string(&a).unwrap()); +/// ``` +/// +/// [`Display`]: std::fmt::Display +/// [`FromStr`]: std::str::FromStr +/// [cargo-toml-rename]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml +/// [serde-as-crate]: https://docs.rs/serde_with/1.12.1/serde_with/guide/serde_as/index.html#re-exporting-serde_as +/// [serde-crate]: https://serde.rs/container-attrs.html#crate +#[proc_macro_derive(SerializeDisplay, attributes(serde_with))] +pub fn derive_serialize_display(item: TokenStream) -> TokenStream { + let input: DeriveInput = parse_macro_input!(item); + let derive_options = match DeriveOptions::from_derive_input(&input) { + Ok(opt) => opt, + Err(err) => { + return err; + } + }; + TokenStream::from(serialize_display( + input, + derive_options.get_serde_with_path(), + )) +} + +fn serialize_display(mut input: DeriveInput, serde_with_crate_path: Path) -> TokenStream2 { + let ident = input.ident; + input + .generics + .make_where_clause() + .predicates + .push(parse_quote!(Self: ::std::fmt::Display)); + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + quote! { + #[automatically_derived] + impl #impl_generics #serde_with_crate_path::serde::Serialize for #ident #ty_generics #where_clause { + fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error> + where + S: #serde_with_crate_path::serde::Serializer, + { + serializer.collect_str(&self) + } + } + } +} + +#[doc(hidden)] +/// Private function. Not part of the public API +/// +/// The only task of this derive macro is to consume any `serde_as` attributes and turn them into inert attributes. +/// This allows the serde_as macro to keep the field attributes without causing compiler errors. +/// The intend is that keeping the field attributes allows downstream crates to consume and act on them without causing an ordering dependency to the serde_as macro. +/// Otherwise, downstream proc-macros would need to be placed *in front of* the main `#[serde_as]` attribute, since otherwise the field attributes would already be stripped off. +/// +/// More details about the use-cases in the Github discussion: <https://github.com/jonasbb/serde_with/discussions/260>. +#[proc_macro_derive(__private_consume_serde_as_attributes, attributes(serde_as))] +pub fn __private_consume_serde_as_attributes(_: TokenStream) -> TokenStream { + TokenStream::new() +} diff --git a/third_party/rust/serde_with_macros/src/utils.rs b/third_party/rust/serde_with_macros/src/utils.rs new file mode 100644 index 0000000000..d7a380af63 --- /dev/null +++ b/third_party/rust/serde_with_macros/src/utils.rs @@ -0,0 +1,77 @@ +use darling::FromDeriveInput; +use proc_macro::TokenStream; +use proc_macro2::TokenStream as TokenStream2; +use quote::ToTokens; +use std::iter::Iterator; +use syn::{parse_quote, Error, Generics, Path, TypeGenerics}; + +/// Merge multiple [`syn::Error`] into one. +pub(crate) trait IteratorExt { + fn collect_error(self) -> Result<(), Error> + where + Self: Iterator<Item = Result<(), Error>> + Sized, + { + let accu = Ok(()); + self.fold(accu, |accu, error| match (accu, error) { + (Ok(()), error) => error, + (accu, Ok(())) => accu, + (Err(mut err), Err(error)) => { + err.combine(error); + Err(err) + } + }) + } +} +impl<I> IteratorExt for I where I: Iterator<Item = Result<(), Error>> + Sized {} + +/// Attributes usable for derive macros +#[derive(FromDeriveInput, Debug)] +#[darling(attributes(serde_with))] +pub(crate) struct DeriveOptions { + /// Path to the crate + #[darling(rename = "crate", default)] + pub(crate) alt_crate_path: Option<Path>, +} + +impl DeriveOptions { + pub(crate) fn from_derive_input(input: &syn::DeriveInput) -> Result<Self, TokenStream> { + match <Self as FromDeriveInput>::from_derive_input(input) { + Ok(v) => Ok(v), + Err(e) => Err(TokenStream::from(e.write_errors())), + } + } + + pub(crate) fn get_serde_with_path(&self) -> Path { + self.alt_crate_path + .clone() + .unwrap_or_else(|| syn::parse_str("::serde_with").unwrap()) + } +} + +// Inspired by https://github.com/serde-rs/serde/blob/fb2fe409c8f7ad6c95e3096e5e9ede865c8cfb49/serde_derive/src/de.rs#L3120 +// Serde is also licences Apache 2 + MIT +pub(crate) fn split_with_de_lifetime( + generics: &Generics, +) -> ( + DeImplGenerics<'_>, + TypeGenerics<'_>, + Option<&syn::WhereClause>, +) { + let de_impl_generics = DeImplGenerics(generics); + let (_, ty_generics, where_clause) = generics.split_for_impl(); + (de_impl_generics, ty_generics, where_clause) +} + +pub(crate) struct DeImplGenerics<'a>(&'a Generics); + +impl<'a> ToTokens for DeImplGenerics<'a> { + fn to_tokens(&self, tokens: &mut TokenStream2) { + let mut generics = self.0.clone(); + generics.params = Some(parse_quote!('de)) + .into_iter() + .chain(generics.params) + .collect(); + let (impl_generics, _, _) = generics.split_for_impl(); + impl_generics.to_tokens(tokens); + } +} |