| 1 | //! Deserializing TOML into Rust structures. |
| 2 | //! |
| 3 | //! This module contains all the Serde support for deserializing TOML documents |
| 4 | //! into Rust structures. Note that some top-level functions here are also |
| 5 | //! provided at the top of the crate. |
| 6 | |
| 7 | #[cfg(feature = "parse")] |
| 8 | #[cfg(feature = "serde")] |
| 9 | mod deserializer; |
| 10 | mod errorrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/error/; |
| 11 | #[cfg(feature = "parse")] |
| 12 | mod parserrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/; |
| 13 | |
| 14 | #[cfg(feature = "parse")] |
| 15 | #[cfg(feature = "serde")] |
| 16 | pub use deserializer::Deserializer; |
| 17 | #[cfg(feature = "parse")] |
| 18 | #[cfg(feature = "serde")] |
| 19 | pub use deserializer::ValueDeserializer; |
| 20 | #[cfg(feature = "parse")] |
| 21 | pub use parserrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/::DeArrayrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/dearray/DeArray#; |
| 22 | #[cfg(feature = "parse")] |
| 23 | pub use parserrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/::DeFloatrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/devalue/DeFloat#; |
| 24 | #[cfg(feature = "parse")] |
| 25 | pub use parserrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/::DeIntegerrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/devalue/DeInteger#; |
| 26 | #[cfg(feature = "parse")] |
| 27 | pub use parserrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/::DeStringrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/devalue/DeString#; |
| 28 | #[cfg(feature = "parse")] |
| 29 | pub use parserrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/::DeTablerust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/detable/DeTable#; |
| 30 | #[cfg(feature = "parse")] |
| 31 | pub use parserrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/::DeValuerust-analyzer cargo toml 0.9.11+spec-1.1.0 de/parser/devalue/DeValue#; |
| 32 | |
| 33 | pub use errorrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/error/::Errorrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/error/Error#; |
| 34 | |
| 35 | use craterust-analyzer cargo toml 0.9.11+spec-1.1.0 crate/::alloc_preluderust-analyzer cargo toml 0.9.11+spec-1.1.0 alloc_prelude/::*; |
| 36 | |
| 37 | /// Deserializes a string into a type. |
| 38 | /// |
| 39 | /// This function will attempt to interpret `s` as a TOML document and |
| 40 | /// deserialize `T` from the document. |
| 41 | /// |
| 42 | /// To deserializes TOML values, instead of documents, see [`ValueDeserializer`]. |
| 43 | /// |
| 44 | /// # Examples |
| 45 | /// |
| 46 | /// ``` |
| 47 | /// use serde::Deserialize; |
| 48 | /// |
| 49 | /// #[derive(Deserialize)] |
| 50 | /// struct Config { |
| 51 | /// title: String, |
| 52 | /// owner: Owner, |
| 53 | /// } |
| 54 | /// |
| 55 | /// #[derive(Deserialize)] |
| 56 | /// struct Owner { |
| 57 | /// name: String, |
| 58 | /// } |
| 59 | /// |
| 60 | /// let config: Config = toml::from_str(r#" |
| 61 | /// title = 'TOML Example' |
| 62 | /// |
| 63 | /// [owner] |
| 64 | /// name = 'Lisa' |
| 65 | /// "#).unwrap(); |
| 66 | /// |
| 67 | /// assert_eq!(config.title, "TOML Example"); |
| 68 | /// assert_eq!(config.owner.name, "Lisa"); |
| 69 | /// ``` |
| 70 | #[cfg(feature = "parse")] |
| 71 | #[cfg(feature = "serde")] |
| 72 | pub fn from_str<'de, T>(s: &'de str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/error/Error#> |
| 73 | where |
| 74 | T: serde_corerust-analyzer cargo serde_core 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Deserializerust-analyzer cargo serde_core 1.0.228 de/Deserialize#<'de>, |
| 75 | { |
| 76 | T::deserialize(Deserializer::parse(s)?) |
| 77 | } |
| 78 | |
| 79 | /// Deserializes bytes into a type. |
| 80 | /// |
| 81 | /// This function will attempt to interpret `s` as a TOML document and |
| 82 | /// deserialize `T` from the document. |
| 83 | /// |
| 84 | /// To deserializes TOML values, instead of documents, see [`ValueDeserializer`]. |
| 85 | #[cfg(feature = "parse")] |
| 86 | #[cfg(feature = "serde")] |
| 87 | pub fn from_slice<'de, T>(s: &'de [u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/error/Error#> |
| 88 | where |
| 89 | T: serde_corerust-analyzer cargo serde_core 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Deserializerust-analyzer cargo serde_core 1.0.228 de/Deserialize#<'de>, |
| 90 | { |
| 91 | let s = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(s).map_err(|e| Errorrust-analyzer cargo toml 0.9.11+spec-1.1.0 de/error/Error#::custom(e.to_string(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#))?; |
| 92 | from_str(s) |
| 93 | } |