diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
commit | 10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch) | |
tree | bdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/serde_ignored | |
parent | Releasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff) | |
download | rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip |
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/serde_ignored')
-rw-r--r-- | vendor/serde_ignored/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | vendor/serde_ignored/Cargo.toml | 32 | ||||
-rw-r--r-- | vendor/serde_ignored/LICENSE-APACHE | 201 | ||||
-rw-r--r-- | vendor/serde_ignored/LICENSE-MIT | 25 | ||||
-rw-r--r-- | vendor/serde_ignored/README.md | 93 | ||||
-rw-r--r-- | vendor/serde_ignored/src/lib.rs | 1360 | ||||
-rw-r--r-- | vendor/serde_ignored/tests/test.rs | 133 |
7 files changed, 1845 insertions, 0 deletions
diff --git a/vendor/serde_ignored/.cargo-checksum.json b/vendor/serde_ignored/.cargo-checksum.json new file mode 100644 index 000000000..a0a56de16 --- /dev/null +++ b/vendor/serde_ignored/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"fae104c28a04d607c0996cdb9f792a82d5b0781cb31044142b56b760fdca4392","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"8dc5d3a00ef6fa428f2ca3d9bf5e99a7e2569a876d28f2ac0b31f82cf3b9d612","src/lib.rs":"6987322716f81282caa6c1109ab2beb715299e0cbd49103e066805894eb982e1","tests/test.rs":"3dbb831ab9bfc8e91529d95d7c61ebbe1844a409a240d1b777c5f304aa00e216"},"package":"1c2c7d39d14f2f2ea82239de71594782f186fd03501ac81f0ce08e674819ff2f"}
\ No newline at end of file diff --git a/vendor/serde_ignored/Cargo.toml b/vendor/serde_ignored/Cargo.toml new file mode 100644 index 000000000..9b93fc748 --- /dev/null +++ b/vendor/serde_ignored/Cargo.toml @@ -0,0 +1,32 @@ +# 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 believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +edition = "2018" +name = "serde_ignored" +version = "0.1.2" +authors = ["David Tolnay <dtolnay@gmail.com>"] +description = "Find out about keys that are ignored when deserializing data" +readme = "README.md" +keywords = ["serde"] +categories = ["encoding"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/dtolnay/serde-ignored" +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] +[dependencies.serde] +version = "1.0" +[dev-dependencies.serde_derive] +version = "1.0" + +[dev-dependencies.serde_json] +version = "1.0" diff --git a/vendor/serde_ignored/LICENSE-APACHE b/vendor/serde_ignored/LICENSE-APACHE new file mode 100644 index 000000000..16fe87b06 --- /dev/null +++ b/vendor/serde_ignored/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_ignored/LICENSE-MIT b/vendor/serde_ignored/LICENSE-MIT new file mode 100644 index 000000000..39d4bdb5a --- /dev/null +++ b/vendor/serde_ignored/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2014 The Rust Project Developers + +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_ignored/README.md b/vendor/serde_ignored/README.md new file mode 100644 index 000000000..659e3aeac --- /dev/null +++ b/vendor/serde_ignored/README.md @@ -0,0 +1,93 @@ +# Serde ignored + +[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/serde--ignored-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/serde-ignored) +[<img alt="crates.io" src="https://img.shields.io/crates/v/serde_ignored.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/serde_ignored) +[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-serde__ignored-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/serde_ignored) +[<img alt="build status" src="https://img.shields.io/github/workflow/status/dtolnay/serde-ignored/CI/master?style=for-the-badge" height="20">](https://github.com/dtolnay/serde-ignored/actions?query=branch%3Amaster) + +Find out about keys that are ignored when deserializing data. This crate +provides a wrapper that works with any existing Serde `Deserializer` and invokes +a callback on every ignored field. + +You can use this to warn users about extraneous keys in a config file, for +example. + +Note that if you want unrecognized fields to be an error, consider using the +`#[serde(deny_unknown_fields)]` [attribute] instead. + +[attribute]: https://serde.rs/attributes.html + +```toml +[dependencies] +serde = "1.0" +serde_ignored = "0.1" +``` + +```rust +use serde::Deserialize; +use std::collections::{BTreeSet as Set, BTreeMap as Map}; + +#[derive(Debug, PartialEq, Deserialize)] +struct Package { + name: String, + dependencies: Map<String, Dependency>, +} + +#[derive(Debug, PartialEq, Deserialize)] +struct Dependency { + version: String, +} + +fn main() { + let j = r#"{ + "name": "demo", + "dependencies": { + "serde": { + "version": "1.0", + "typo1": "" + } + }, + "typo2": { + "inner": "" + }, + "typo3": {} + }"#; + + // Some Deserializer. + let jd = &mut serde_json::Deserializer::from_str(j); + + // We will build a set of paths to the unused elements. + let mut unused = Set::new(); + + let p: Package = serde_ignored::deserialize(jd, |path| { + unused.insert(path.to_string()); + }).unwrap(); + + // Deserialized as normal. + println!("{:?}", p); + + // There were three ignored keys. + let mut expected = Set::new(); + expected.insert("dependencies.serde.typo1".to_owned()); + expected.insert("typo2".to_owned()); + expected.insert("typo3".to_owned()); + assert_eq!(unused, expected); +} +``` + +<br> + +#### License + +<sup> +Licensed under either of <a href="LICENSE-APACHE">Apache License, Version +2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. +</sup> + +<br> + +<sub> +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. +</sub> diff --git a/vendor/serde_ignored/src/lib.rs b/vendor/serde_ignored/src/lib.rs new file mode 100644 index 000000000..c415dca07 --- /dev/null +++ b/vendor/serde_ignored/src/lib.rs @@ -0,0 +1,1360 @@ +//! [![github]](https://github.com/dtolnay/serde-ignored) [![crates-io]](https://crates.io/crates/serde_ignored) [![docs-rs]](https://docs.rs/serde_ignored) +//! +//! [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 +//! +//! <br> +//! +//! Find out about keys that are ignored when deserializing data. This crate +//! provides a wrapper that works with any existing Serde `Deserializer` and +//! invokes a callback on every ignored field. +//! +//! You can use this to warn users about extraneous keys in a config file, for +//! example. +//! +//! Note that if you want unrecognized fields to be an error, consider using the +//! `#[serde(deny_unknown_fields)]` [attribute] instead. +//! +//! [attribute]: https://serde.rs/attributes.html +//! +//! # Example +//! +//! ``` +//! # use serde_derive::Deserialize; +//! # +//! use serde::Deserialize; +//! use std::collections::{BTreeSet as Set, BTreeMap as Map}; +//! +//! #[derive(Debug, PartialEq, Deserialize)] +//! struct Package { +//! name: String, +//! dependencies: Map<String, Dependency>, +//! } +//! +//! #[derive(Debug, PartialEq, Deserialize)] +//! struct Dependency { +//! version: String, +//! } +//! +//! # fn try_main() -> Result<(), Box<::std::error::Error>> { +//! let j = r#"{ +//! "name": "demo", +//! "dependencies": { +//! "serde": { +//! "version": "1.0", +//! "typo1": "" +//! } +//! }, +//! "typo2": { +//! "inner": "" +//! }, +//! "typo3": {} +//! }"#; +//! +//! // Some Deserializer. +//! let jd = &mut serde_json::Deserializer::from_str(j); +//! +//! // We will build a set of paths to the unused elements. +//! let mut unused = Set::new(); +//! +//! let p: Package = serde_ignored::deserialize(jd, |path| { +//! unused.insert(path.to_string()); +//! })?; +//! +//! assert_eq!(p, Package { +//! name: "demo".to_owned(), +//! dependencies: { +//! let mut map = Map::new(); +//! map.insert("serde".to_owned(), Dependency { +//! version: "1.0".to_owned(), +//! }); +//! map +//! }, +//! }); +//! +//! assert_eq!(unused, { +//! let mut expected = Set::new(); +//! expected.insert("dependencies.serde.typo1".to_owned()); +//! expected.insert("typo2".to_owned()); +//! expected.insert("typo3".to_owned()); +//! expected +//! }); +//! +//! # Ok(()) } +//! # fn main() { try_main().unwrap() } +//! ``` + +#![doc(html_root_url = "https://docs.rs/serde_ignored/0.1.2")] + +use serde::de::{self, Deserialize, DeserializeSeed, Visitor}; +use std::fmt::{self, Display}; + +/// Entry point. See crate documentation for an example. +pub fn deserialize<'de, D, F, T>(deserializer: D, mut callback: F) -> Result<T, D::Error> +where + D: de::Deserializer<'de>, + F: FnMut(Path), + T: Deserialize<'de>, +{ + T::deserialize(Deserializer::new(deserializer, &mut callback)) +} + +/// Deserializer adapter that invokes a callback with the path to every unused +/// field of the input. +pub struct Deserializer<'a, 'b, D, F: 'b> { + de: D, + callback: &'b mut F, + path: Path<'a>, +} + +impl<'a, 'b, D, F> Deserializer<'a, 'b, D, F> +where + F: FnMut(Path), +{ + // The structs in this crate all hold their closure by &mut F. If they were + // to contain F by value, any method taking &mut self (for example + // SeqAccess::next_element_seed) would be forced to recurse with &mut + // self.callback, even if F is instantiated with a &mut already. This way + // they contain &mut F and the &mut self methods can recurse with + // self.callback unchanged. This avoids blowing the recursion limit in + // Cargo's use of this crate. + // + // https://github.com/dtolnay/serde-ignored/pull/1 + pub fn new(de: D, callback: &'b mut F) -> Self { + Deserializer { + de, + callback, + path: Path::Root, + } + } +} + +/// Path to the current value in the input, like `dependencies.serde.typo1`. +pub enum Path<'a> { + Root, + Seq { parent: &'a Path<'a>, index: usize }, + Map { parent: &'a Path<'a>, key: String }, + Some { parent: &'a Path<'a> }, + NewtypeStruct { parent: &'a Path<'a> }, + NewtypeVariant { parent: &'a Path<'a> }, +} + +impl<'a> Display for Path<'a> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + struct Parent<'a>(&'a Path<'a>); + + impl<'a> Display for Parent<'a> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match *self.0 { + Path::Root => Ok(()), + ref path => write!(formatter, "{}.", path), + } + } + } + + match *self { + Path::Root => formatter.write_str("."), + Path::Seq { parent, index } => write!(formatter, "{}{}", Parent(parent), index), + Path::Map { parent, ref key } => write!(formatter, "{}{}", Parent(parent), key), + Path::Some { parent } + | Path::NewtypeStruct { parent } + | Path::NewtypeVariant { parent } => write!(formatter, "{}?", Parent(parent)), + } + } +} + +/// Plain old forwarding impl except for `deserialize_ignored_any` which invokes +/// the callback. +impl<'a, 'b, 'de, D, F> de::Deserializer<'de> for Deserializer<'a, 'b, D, F> +where + D: de::Deserializer<'de>, + F: FnMut(Path), +{ + type Error = D::Error; + + fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_any(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_bool(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_u8(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_u16(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_u32(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_u64(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_i8(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_i16(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_i32(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_i64(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_f32(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_f64(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_char(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_str(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_string(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_bytes(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_byte_buf(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_option(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_unit(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_unit_struct<V>( + self, + name: &'static str, + visitor: V, + ) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_unit_struct(name, Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_newtype_struct<V>( + self, + name: &'static str, + visitor: V, + ) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_newtype_struct(name, Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_seq(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_tuple(len, Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_tuple_struct<V>( + self, + name: &'static str, + len: usize, + visitor: V, + ) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_tuple_struct(name, len, Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_map(Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_struct<V>( + self, + name: &'static str, + fields: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_struct(name, fields, Wrap::new(visitor, self.callback, &self.path)) + } + + fn deserialize_enum<V>( + self, + name: &'static str, + variants: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de.deserialize_enum( + name, + variants, + Wrap::new(visitor, self.callback, &self.path), + ) + } + + fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + (self.callback)(self.path); + self.de.deserialize_ignored_any(visitor) + } + + fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, D::Error> + where + V: Visitor<'de>, + { + self.de + .deserialize_identifier(Wrap::new(visitor, self.callback, &self.path)) + } +} + +/// Wrapper that attaches context to a `Visitor`, `SeqAccess`, `EnumAccess` or +/// `VariantAccess`. +struct Wrap<'a, 'b, X, F: 'b> { + delegate: X, + callback: &'b mut F, + path: &'a Path<'a>, +} + +impl<'a, 'b, X, F> Wrap<'a, 'b, X, F> { + fn new(delegate: X, callback: &'b mut F, path: &'a Path<'a>) -> Self { + Wrap { + delegate, + callback, + path, + } + } +} + +/// Forwarding impl to preserve context. +impl<'a, 'b, 'de, X, F> Visitor<'de> for Wrap<'a, 'b, X, F> +where + X: Visitor<'de>, + F: FnMut(Path), +{ + type Value = X::Value; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + self.delegate.expecting(formatter) + } + + fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_bool(v) + } + + fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_i8(v) + } + + fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_i16(v) + } + + fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_i32(v) + } + + fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_i64(v) + } + + fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_u8(v) + } + + fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_u16(v) + } + + fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_u32(v) + } + + fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_u64(v) + } + + fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_f32(v) + } + + fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_f64(v) + } + + fn visit_char<E>(self, v: char) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_char(v) + } + + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_str(v) + } + + fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_borrowed_str(v) + } + + fn visit_string<E>(self, v: String) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_string(v) + } + + fn visit_unit<E>(self) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_unit() + } + + fn visit_none<E>(self) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_none() + } + + fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> + where + D: de::Deserializer<'de>, + { + self.delegate.visit_some(Deserializer { + de: deserializer, + callback: self.callback, + path: Path::Some { parent: self.path }, + }) + } + + fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> + where + D: de::Deserializer<'de>, + { + self.delegate.visit_newtype_struct(Deserializer { + de: deserializer, + callback: self.callback, + path: Path::NewtypeStruct { parent: self.path }, + }) + } + + fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error> + where + V: de::SeqAccess<'de>, + { + self.delegate + .visit_seq(SeqAccess::new(visitor, self.callback, self.path)) + } + + fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error> + where + V: de::MapAccess<'de>, + { + self.delegate + .visit_map(MapAccess::new(visitor, self.callback, self.path)) + } + + fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error> + where + V: de::EnumAccess<'de>, + { + self.delegate + .visit_enum(Wrap::new(visitor, self.callback, self.path)) + } + + fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_bytes(v) + } + + fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_borrowed_bytes(v) + } + + fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_byte_buf(v) + } +} + +/// Forwarding impl to preserve context. +impl<'a, 'b, 'de, X: 'a, F: 'b> de::EnumAccess<'de> for Wrap<'a, 'b, X, F> +where + X: de::EnumAccess<'de>, + F: FnMut(Path), +{ + type Error = X::Error; + type Variant = Wrap<'a, 'b, X::Variant, F>; + + fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), X::Error> + where + V: DeserializeSeed<'de>, + { + let callback = self.callback; + let path = self.path; + self.delegate + .variant_seed(seed) + .map(move |(v, vis)| (v, Wrap::new(vis, callback, path))) + } +} + +/// Forwarding impl to preserve context. +impl<'a, 'b, 'de, X, F> de::VariantAccess<'de> for Wrap<'a, 'b, X, F> +where + X: de::VariantAccess<'de>, + F: FnMut(Path), +{ + type Error = X::Error; + + fn unit_variant(self) -> Result<(), X::Error> { + self.delegate.unit_variant() + } + + fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, X::Error> + where + T: DeserializeSeed<'de>, + { + let path = Path::NewtypeVariant { parent: self.path }; + self.delegate + .newtype_variant_seed(TrackedSeed::new(seed, self.callback, path)) + } + + fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .tuple_variant(len, Wrap::new(visitor, self.callback, self.path)) + } + + fn struct_variant<V>( + self, + fields: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .struct_variant(fields, Wrap::new(visitor, self.callback, self.path)) + } +} + +/// Seed that saves the string into the given optional during `visit_str` and +/// `visit_string`. +struct CaptureKey<'a, X> { + delegate: X, + key: &'a mut Option<String>, +} + +impl<'a, X> CaptureKey<'a, X> { + fn new(delegate: X, key: &'a mut Option<String>) -> Self { + CaptureKey { delegate, key } + } +} + +/// Forwarding impl. +impl<'a, 'de, X> DeserializeSeed<'de> for CaptureKey<'a, X> +where + X: DeserializeSeed<'de>, +{ + type Value = X::Value; + + fn deserialize<D>(self, deserializer: D) -> Result<X::Value, D::Error> + where + D: de::Deserializer<'de>, + { + self.delegate + .deserialize(CaptureKey::new(deserializer, self.key)) + } +} + +/// Forwarding impl. +impl<'a, 'de, X> de::Deserializer<'de> for CaptureKey<'a, X> +where + X: de::Deserializer<'de>, +{ + type Error = X::Error; + + fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_any(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_bool(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_u8(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_u16(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_u32(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_u64(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_i8(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_i16(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_i32(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_i64(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_f32(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_f64(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_char(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_str(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_string(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_bytes(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_byte_buf(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_option(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_unit(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_unit_struct<V>( + self, + name: &'static str, + visitor: V, + ) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_unit_struct(name, CaptureKey::new(visitor, self.key)) + } + + fn deserialize_newtype_struct<V>( + self, + name: &'static str, + visitor: V, + ) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_newtype_struct(name, CaptureKey::new(visitor, self.key)) + } + + fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_seq(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_tuple(len, CaptureKey::new(visitor, self.key)) + } + + fn deserialize_tuple_struct<V>( + self, + name: &'static str, + len: usize, + visitor: V, + ) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_tuple_struct(name, len, CaptureKey::new(visitor, self.key)) + } + + fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_map(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_struct<V>( + self, + name: &'static str, + fields: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_struct(name, fields, CaptureKey::new(visitor, self.key)) + } + + fn deserialize_enum<V>( + self, + name: &'static str, + variants: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_enum(name, variants, CaptureKey::new(visitor, self.key)) + } + + fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_ignored_any(CaptureKey::new(visitor, self.key)) + } + + fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, X::Error> + where + V: Visitor<'de>, + { + self.delegate + .deserialize_identifier(CaptureKey::new(visitor, self.key)) + } +} + +/// Forwarding impl that also saves the value of integers and strings. +impl<'a, 'de, X> Visitor<'de> for CaptureKey<'a, X> +where + X: Visitor<'de>, +{ + type Value = X::Value; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + self.delegate.expecting(formatter) + } + + fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_string()); + self.delegate.visit_bool(v) + } + + fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_string()); + self.delegate.visit_i8(v) + } + + fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_string()); + self.delegate.visit_i16(v) + } + + fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_string()); + self.delegate.visit_i32(v) + } + + fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_string()); + self.delegate.visit_i64(v) + } + + fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_string()); + self.delegate.visit_u8(v) + } + + fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_string()); + self.delegate.visit_u16(v) + } + + fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_string()); + self.delegate.visit_u32(v) + } + + fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_string()); + self.delegate.visit_u64(v) + } + + fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_f32(v) + } + + fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_f64(v) + } + + fn visit_char<E>(self, v: char) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_char(v) + } + + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_owned()); + self.delegate.visit_str(v) + } + + fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.to_owned()); + self.delegate.visit_borrowed_str(v) + } + + fn visit_string<E>(self, v: String) -> Result<Self::Value, E> + where + E: de::Error, + { + *self.key = Some(v.clone()); + self.delegate.visit_string(v) + } + + fn visit_unit<E>(self) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_unit() + } + + fn visit_none<E>(self) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_none() + } + + fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> + where + D: de::Deserializer<'de>, + { + self.delegate.visit_some(deserializer) + } + + fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> + where + D: de::Deserializer<'de>, + { + self.delegate.visit_newtype_struct(CaptureKey::new(deserializer, self.key)) + } + + fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error> + where + V: de::SeqAccess<'de>, + { + self.delegate.visit_seq(visitor) + } + + fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error> + where + V: de::MapAccess<'de>, + { + self.delegate.visit_map(visitor) + } + + fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error> + where + V: de::EnumAccess<'de>, + { + self.delegate.visit_enum(CaptureKey::new(visitor, self.key)) + } + + fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_bytes(v) + } + + fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_borrowed_bytes(v) + } + + fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_byte_buf(v) + } +} + +impl<'a, 'de, X> de::EnumAccess<'de> for CaptureKey<'a, X> +where + X: de::EnumAccess<'de>, +{ + type Error = X::Error; + type Variant = X::Variant; + + fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), X::Error> + where + V: DeserializeSeed<'de>, + { + self.delegate.variant_seed(CaptureKey::new(seed, self.key)) + } +} + +/// Seed used for map values, sequence elements and newtype variants to track +/// their path. +struct TrackedSeed<'a, X, F: 'a> { + seed: X, + callback: &'a mut F, + path: Path<'a>, +} + +impl<'a, X, F> TrackedSeed<'a, X, F> { + fn new(seed: X, callback: &'a mut F, path: Path<'a>) -> Self { + TrackedSeed { + seed, + callback, + path, + } + } +} + +impl<'a, 'de, X, F> DeserializeSeed<'de> for TrackedSeed<'a, X, F> +where + X: DeserializeSeed<'de>, + F: FnMut(Path), +{ + type Value = X::Value; + + fn deserialize<D>(self, deserializer: D) -> Result<X::Value, D::Error> + where + D: de::Deserializer<'de>, + { + self.seed.deserialize(Deserializer { + de: deserializer, + callback: self.callback, + path: self.path, + }) + } +} + +/// Seq visitor that tracks the index of its elements. +struct SeqAccess<'a, 'b, X, F: 'b> { + delegate: X, + callback: &'b mut F, + path: &'a Path<'a>, + index: usize, +} + +impl<'a, 'b, X, F> SeqAccess<'a, 'b, X, F> { + fn new(delegate: X, callback: &'b mut F, path: &'a Path<'a>) -> Self { + SeqAccess { + delegate, + callback, + path, + index: 0, + } + } +} + +/// Forwarding impl to preserve context. +impl<'a, 'b, 'de, X, F> de::SeqAccess<'de> for SeqAccess<'a, 'b, X, F> +where + X: de::SeqAccess<'de>, + F: FnMut(Path), +{ + type Error = X::Error; + + fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, X::Error> + where + T: DeserializeSeed<'de>, + { + let path = Path::Seq { + parent: self.path, + index: self.index, + }; + self.index += 1; + self.delegate + .next_element_seed(TrackedSeed::new(seed, self.callback, path)) + } + + fn size_hint(&self) -> Option<usize> { + self.delegate.size_hint() + } +} + +/// Map visitor that captures the string value of its keys and uses that to +/// track the path to its values. +struct MapAccess<'a, 'b, X, F: 'b> { + delegate: X, + callback: &'b mut F, + path: &'a Path<'a>, + key: Option<String>, +} + +impl<'a, 'b, X, F> MapAccess<'a, 'b, X, F> { + fn new(delegate: X, callback: &'b mut F, path: &'a Path<'a>) -> Self { + MapAccess { + delegate, + callback, + path, + key: None, + } + } + + fn key<E>(&mut self) -> Result<String, E> + where + E: de::Error, + { + self.key.take().ok_or_else(|| E::custom("non-string key")) + } +} + +impl<'a, 'b, 'de, X, F> de::MapAccess<'de> for MapAccess<'a, 'b, X, F> +where + X: de::MapAccess<'de>, + F: FnMut(Path), +{ + type Error = X::Error; + + fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, X::Error> + where + K: DeserializeSeed<'de>, + { + self.delegate + .next_key_seed(CaptureKey::new(seed, &mut self.key)) + } + + fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, X::Error> + where + V: DeserializeSeed<'de>, + { + let path = Path::Map { + parent: self.path, + key: self.key()?, + }; + self.delegate + .next_value_seed(TrackedSeed::new(seed, self.callback, path)) + } + + fn size_hint(&self) -> Option<usize> { + self.delegate.size_hint() + } +} diff --git a/vendor/serde_ignored/tests/test.rs b/vendor/serde_ignored/tests/test.rs new file mode 100644 index 000000000..414fe691b --- /dev/null +++ b/vendor/serde_ignored/tests/test.rs @@ -0,0 +1,133 @@ +use serde::Deserialize; +use serde_derive::Deserialize; +use std::collections::{BTreeMap as Map, BTreeSet as Set}; +use std::iter::FromIterator; + +fn assert_ignored<'de, T>(json: &'de str, expected: &[&str]) -> T +where + T: Deserialize<'de>, +{ + let de = &mut serde_json::Deserializer::from_str(json); + + let mut unused = Set::new(); + + let value: T = serde_ignored::deserialize(de, |path| { + unused.insert(path.to_string()); + }) + .unwrap(); + + let expected = Set::from_iter(expected.into_iter().cloned().map(str::to_owned)); + assert_eq!(unused, expected); + + value +} + +#[derive(Debug, Deserialize)] +struct V { + used: (), +} + +#[test] +fn test_readme() { + #[derive(Debug, PartialEq, Deserialize)] + struct Package { + name: String, + dependencies: Map<String, Dependency>, + } + + #[derive(Debug, PartialEq, Deserialize)] + struct Dependency { + version: String, + } + + let json = r#"{ + "name": "demo", + "dependencies": { + "serde": { + "version": "1.0", + "typo1": "" + } + }, + "typo2": { + "inner": "" + }, + "typo3": {} + }"#; + + let ignored = &["dependencies.serde.typo1", "typo2", "typo3"]; + let p: Package = assert_ignored(json, ignored); + + let expected = Package { + name: "demo".to_owned(), + dependencies: { + let mut map = Map::new(); + map.insert( + "serde".to_owned(), + Dependency { + version: "1.0".to_owned(), + }, + ); + map + }, + }; + assert_eq!(p, expected); +} + +#[test] +fn test_int_key() { + #[derive(Debug, Deserialize)] + struct Test { + a: Map<usize, V>, + } + + let json = r#"{ + "a": { + "2": { + "used": null, + "unused": null + } + } + }"#; + + let ignored = &["a.2.unused"]; + assert_ignored::<Test>(json, ignored); +} + +#[test] +fn test_newtype_key() { + type Test = Map<Key, V>; + + #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Deserialize)] + struct Key(&'static str); + + let json = r#"{ + "k": { + "used": null, + "unused": null + } + }"#; + + let ignored = &["k.unused"]; + assert_ignored::<Test>(json, ignored); +} + +#[test] +fn test_unit_variant_key() { + type Test = Map<Key, V>; + + #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Deserialize)] + enum Key { + First, + Second, + } + + let json = r#"{ + "First": { + "used": null, + "unused": null + } + }"#; + + let ignored = &["First.unused"]; + assert_ignored::<Test>(json, ignored); +} |