| 1 | //! [![github]](https://github.com/dtolnay/path-to-error) [![crates-io]](https://crates.io/crates/serde_path_to_error) [![docs-rs]](https://docs.rs/serde_path_to_error) |
| 2 | //! |
| 3 | //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github |
| 4 | //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust |
| 5 | //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs |
| 6 | //! |
| 7 | //! <br> |
| 8 | //! |
| 9 | //! Find out the path at which a deserialization error occurred. This crate |
| 10 | //! provides a wrapper that works with any existing Serde `Deserializer` and |
| 11 | //! exposes the chain of field names leading to the error. |
| 12 | //! |
| 13 | //! # Example |
| 14 | //! |
| 15 | //! ``` |
| 16 | //! # use serde_derive::Deserialize; |
| 17 | //! # |
| 18 | //! use serde::Deserialize; |
| 19 | //! use std::collections::BTreeMap as Map; |
| 20 | //! |
| 21 | //! #[derive(Deserialize)] |
| 22 | //! struct Package { |
| 23 | //! name: String, |
| 24 | //! dependencies: Map<String, Dependency>, |
| 25 | //! } |
| 26 | //! |
| 27 | //! #[derive(Deserialize)] |
| 28 | //! struct Dependency { |
| 29 | //! version: String, |
| 30 | //! } |
| 31 | //! |
| 32 | //! fn main() { |
| 33 | //! let j = r#"{ |
| 34 | //! "name": "demo", |
| 35 | //! "dependencies": { |
| 36 | //! "serde": { |
| 37 | //! "version": 1 |
| 38 | //! } |
| 39 | //! } |
| 40 | //! }"#; |
| 41 | //! |
| 42 | //! // Some Deserializer. |
| 43 | //! let jd = &mut serde_json::Deserializer::from_str(j); |
| 44 | //! |
| 45 | //! let result: Result<Package, _> = serde_path_to_error::deserialize(jd); |
| 46 | //! match result { |
| 47 | //! Ok(_) => panic!("expected a type error"), |
| 48 | //! Err(err) => { |
| 49 | //! let path = err.path().to_string(); |
| 50 | //! assert_eq!(path, "dependencies.serde.version"); |
| 51 | //! } |
| 52 | //! } |
| 53 | //! } |
| 54 | //! ``` |
| 55 | |
| 56 | #![doc(html_root_url = "https://docs.rs/serde_path_to_error/0.1.20")] |
| 57 | #![no_std] |
| 58 | #![allow( |
| 59 | clippy::doc_link_with_quotes, // https://github.com/rust-lang/rust-clippy/issues/8961 |
| 60 | clippy::elidable_lifetime_names, |
| 61 | clippy::iter_not_returning_iterator, // https://github.com/rust-lang/rust-clippy/issues/8285 |
| 62 | clippy::missing_errors_doc, |
| 63 | clippy::module_name_repetitions, |
| 64 | clippy::must_use_candidate, |
| 65 | clippy::needless_lifetimes, |
| 66 | clippy::new_without_default, |
| 67 | clippy::uninlined_format_args |
| 68 | )] |
| 69 | #![allow(unknown_lints, mismatched_lifetime_syntaxes)] |
| 70 | |
| 71 | extern crate allocrust-analyzer cargo serde_path_to_error 0.1.20 alloc/; |
| 72 | extern crate serde_core as serderust-analyzer cargo serde_path_to_error 0.1.20 serde/; |
| 73 | |
| 74 | mod derust-analyzer cargo serde_path_to_error 0.1.20 de/; |
| 75 | mod pathrust-analyzer cargo serde_path_to_error 0.1.20 path/; |
| 76 | mod serrust-analyzer cargo serde_path_to_error 0.1.20 ser/; |
| 77 | mod wraprust-analyzer cargo serde_path_to_error 0.1.20 wrap/; |
| 78 | |
| 79 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#; |
| 80 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#; |
| 81 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::{selfrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/, Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#}; |
| 82 | use serderust-analyzer cargo serde_core 1.0.222 crate/::serrust-analyzer cargo serde_core 1.0.222 ser/::StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 83 | |
| 84 | pub use craterust-analyzer cargo serde_path_to_error 0.1.20 crate/::derust-analyzer cargo serde_path_to_error 0.1.20 de/::{deserializerust-analyzer cargo serde_path_to_error 0.1.20 de/deserialize()., Deserializerrust-analyzer cargo serde_path_to_error 0.1.20 de/Deserializer#}; |
| 85 | pub use craterust-analyzer cargo serde_path_to_error 0.1.20 crate/::pathrust-analyzer cargo serde_path_to_error 0.1.20 path/::{Pathrust-analyzer cargo serde_path_to_error 0.1.20 path/Path#, Segmentrust-analyzer cargo serde_path_to_error 0.1.20 path/Segment#, Segmentsrust-analyzer cargo serde_path_to_error 0.1.20 path/Segments#}; |
| 86 | pub use craterust-analyzer cargo serde_path_to_error 0.1.20 crate/::serrust-analyzer cargo serde_path_to_error 0.1.20 ser/::{serializerust-analyzer cargo serde_path_to_error 0.1.20 ser/serialize()., Serializerrust-analyzer cargo serde_path_to_error 0.1.20 ser/Serializer#}; |
| 87 | |
| 88 | /// Original deserializer error together with the path at which it occurred. |
| 89 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 90 | pub struct Errorrust-analyzer cargo serde_path_to_error 0.1.20 Error#<E> { |
| 91 | pathrust-analyzer cargo serde_path_to_error 0.1.20 Error#path.: Pathrust-analyzer cargo serde_path_to_error 0.1.20 path/Path#, |
| 92 | originalrust-analyzer cargo serde_path_to_error 0.1.20 Error#original.: E, |
| 93 | } |
| 94 | |
| 95 | impl<E> Errorrust-analyzer cargo serde_path_to_error 0.1.20 Error#<E> { |
| 96 | pub fn newrust-analyzer cargo serde_path_to_error 0.1.20 impl#[`Error<E>`]new().(path: Pathrust-analyzer cargo serde_path_to_error 0.1.20 path/Path#, inner: E) -> Selfrust-analyzer cargo serde_path_to_error 0.1.20 impl#[`Error<E>`] { |
| 97 | Errorrust-analyzer cargo serde_path_to_error 0.1.20 Error# { |
| 98 | pathrust-analyzer cargo serde_path_to_error 0.1.20 Error#path., |
| 99 | originalrust-analyzer cargo serde_path_to_error 0.1.20 Error#original.: inner, |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Element path at which this deserialization error occurred. |
| 104 | pub fn pathrust-analyzer cargo serde_path_to_error 0.1.20 impl#[`Error<E>`]path().(&self) -> &Pathrust-analyzer cargo serde_path_to_error 0.1.20 path/Path# { |
| 105 | &self.pathrust-analyzer cargo serde_path_to_error 0.1.20 Error#path. |
| 106 | } |
| 107 | |
| 108 | /// The Deserializer's underlying error that occurred. |
| 109 | pub fn into_innerrust-analyzer cargo serde_path_to_error 0.1.20 impl#[`Error<E>`]into_inner().(self) -> E { |
| 110 | self.originalrust-analyzer cargo serde_path_to_error 0.1.20 Error#original. |
| 111 | } |
| 112 | |
| 113 | /// Reference to the Deserializer's underlying error that occurred. |
| 114 | pub fn innerrust-analyzer cargo serde_path_to_error 0.1.20 impl#[`Error<E>`]inner().(&self) -> &E { |
| 115 | &self.originalrust-analyzer cargo serde_path_to_error 0.1.20 Error#original. |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl<E: Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#> Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errorrust-analyzer cargo serde_path_to_error 0.1.20 Error#<E> { |
| 120 | fn fmtrust-analyzer cargo serde_path_to_error 0.1.20 impl#[`Error<E>`][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 121 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.pathrust-analyzer cargo serde_path_to_error 0.1.20 Error#path..is_only_unknownrust-analyzer cargo serde_path_to_error 0.1.20 path/impl#[Path]is_only_unknown().() { |
| 122 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}: ", self.pathrust-analyzer cargo serde_path_to_error 0.1.20 Error#path.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 123 | } |
| 124 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}", self.originalrust-analyzer cargo serde_path_to_error 0.1.20 Error#original.) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl<E: StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#> StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Errorrust-analyzer cargo serde_path_to_error 0.1.20 Error#<E> { |
| 129 | fn sourcerust-analyzer cargo serde_path_to_error 0.1.20 impl#[`Error<E>`][Error]source().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&(dyn StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# + 'static)> { |
| 130 | self.originalrust-analyzer cargo serde_path_to_error 0.1.20 Error#original..sourcerust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#source().() |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /// State for bookkeeping across nested deserializer calls. |
| 135 | /// |
| 136 | /// You don't need this if you are using `serde_path_to_error::deserializer`. If |
| 137 | /// you are managing your own `Deserializer`, see the usage example on |
| 138 | /// [`Deserializer`]. |
| 139 | pub struct Trackrust-analyzer cargo serde_path_to_error 0.1.20 Track# { |
| 140 | pathrust-analyzer cargo serde_path_to_error 0.1.20 Track#path.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Pathrust-analyzer cargo serde_path_to_error 0.1.20 path/Path#>>, |
| 141 | } |
| 142 | |
| 143 | impl Trackrust-analyzer cargo serde_path_to_error 0.1.20 Track# { |
| 144 | /// Empty state with no error having happened yet. |
| 145 | pub const fn newrust-analyzer cargo serde_path_to_error 0.1.20 impl#[Track]new().() -> Selfrust-analyzer cargo serde_path_to_error 0.1.20 impl#[Track] { |
| 146 | Trackrust-analyzer cargo serde_path_to_error 0.1.20 Track# { |
| 147 | pathrust-analyzer cargo serde_path_to_error 0.1.20 Track#path.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]new().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#), |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /// Gets path at which the error occurred. Only meaningful after we know |
| 152 | /// that an error has occurred. Returns an empty path otherwise. |
| 153 | pub fn pathrust-analyzer cargo serde_path_to_error 0.1.20 impl#[Track]path().(self) -> Pathrust-analyzer cargo serde_path_to_error 0.1.20 path/Path# { |
| 154 | self.pathrust-analyzer cargo serde_path_to_error 0.1.20 Track#path..into_innerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]into_inner().().unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(Pathrust-analyzer cargo serde_path_to_error 0.1.20 path/Path#::emptyrust-analyzer cargo serde_path_to_error 0.1.20 path/impl#[Path]empty().) |
| 155 | } |
| 156 | |
| 157 | #[inline] |
| 158 | fn triggerrust-analyzer cargo serde_path_to_error 0.1.20 impl#[Track]trigger().<E>(&self, chain: &Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#, err: E) -> E { |
| 159 | self.trigger_implrust-analyzer cargo serde_path_to_error 0.1.20 impl#[Track]trigger_impl().(chain); |
| 160 | err |
| 161 | } |
| 162 | |
| 163 | fn trigger_implrust-analyzer cargo serde_path_to_error 0.1.20 impl#[Track]trigger_impl().(&self, chain: &Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#) { |
| 164 | self.pathrust-analyzer cargo serde_path_to_error 0.1.20 Track#path..setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(match self.pathrust-analyzer cargo serde_path_to_error 0.1.20 Track#path..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]take().() { |
| 165 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(already_set) => already_set, |
| 166 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Pathrust-analyzer cargo serde_path_to_error 0.1.20 path/Path#::from_chainrust-analyzer cargo serde_path_to_error 0.1.20 path/impl#[Path]from_chain().(chain), |
| 167 | })); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 172 | enum Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#<'a> { |
| 173 | Rootrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Root#, |
| 174 | Seqrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Seq# { |
| 175 | parentrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Seq#parent.: &'a Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#<'a>, |
| 176 | indexrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Seq#index.: usize, |
| 177 | }, |
| 178 | Maprust-analyzer cargo serde_path_to_error 0.1.20 Chain#Map# { |
| 179 | parentrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Map#parent.: &'a Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#<'a>, |
| 180 | keyrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Map#key.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 181 | }, |
| 182 | Structrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Struct# { |
| 183 | parentrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Struct#parent.: &'a Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#<'a>, |
| 184 | keyrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Struct#key.: &'static str, |
| 185 | }, |
| 186 | Enumrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Enum# { |
| 187 | parentrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Enum#parent.: &'a Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#<'a>, |
| 188 | variantrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Enum#variant.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 189 | }, |
| 190 | Somerust-analyzer cargo serde_path_to_error 0.1.20 Chain#Some# { |
| 191 | parentrust-analyzer cargo serde_path_to_error 0.1.20 Chain#Some#parent.: &'a Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#<'a>, |
| 192 | }, |
| 193 | NewtypeStructrust-analyzer cargo serde_path_to_error 0.1.20 Chain#NewtypeStruct# { |
| 194 | parentrust-analyzer cargo serde_path_to_error 0.1.20 Chain#NewtypeStruct#parent.: &'a Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#<'a>, |
| 195 | }, |
| 196 | NewtypeVariantrust-analyzer cargo serde_path_to_error 0.1.20 Chain#NewtypeVariant# { |
| 197 | parentrust-analyzer cargo serde_path_to_error 0.1.20 Chain#NewtypeVariant#parent.: &'a Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#<'a>, |
| 198 | }, |
| 199 | NonStringKeyrust-analyzer cargo serde_path_to_error 0.1.20 Chain#NonStringKey# { |
| 200 | parentrust-analyzer cargo serde_path_to_error 0.1.20 Chain#NonStringKey#parent.: &'a Chainrust-analyzer cargo serde_path_to_error 0.1.20 Chain#<'a>, |
| 201 | }, |
| 202 | } |