toml-0.9.8/.cargo_vcs_info.json0000644000000001510000000000100120220ustar { "git": { "sha1": "93e9146aea8ddae1c9d46f1d576c73a836011f59" }, "path_in_vcs": "crates/toml" }toml-0.9.8/Cargo.lock0000644000000445470000000000100100160ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "aho-corasick" version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] [[package]] name = "anstream" version = "0.6.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ae563653d1938f79b1ab1b5e668c87c76a9930414574a6583a7b7e11a8e6192" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd" [[package]] name = "anstyle-parse" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e231f6134f61b71076a3eab506c379d4f36122f2af15a9ff04415ea4c3339e2" dependencies = [ "windows-sys 0.60.2", ] [[package]] name = "anstyle-wincon" version = "3.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e0633414522a32ffaac8ac6cc8f748e090c5717661fddeea04219e2344f5f2a" dependencies = [ "anstyle", "once_cell_polyfill", "windows-sys 0.60.2", ] [[package]] name = "autocfg" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" [[package]] name = "bstr" version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "234113d19d0d7d613b40e86fb654acf958910802bcceab913a4f9e7cda03b1a4" dependencies = [ "memchr", "serde", ] [[package]] name = "chrono" version = "0.4.42" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" dependencies = [ "num-traits", ] [[package]] name = "colorchoice" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" [[package]] name = "crossbeam-deque" version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" dependencies = [ "crossbeam-epoch", "crossbeam-utils", ] [[package]] name = "crossbeam-epoch" version = "0.9.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" dependencies = [ "crossbeam-utils", ] [[package]] name = "crossbeam-utils" version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] name = "either" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "equivalent" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" [[package]] name = "erased-serde" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e004d887f51fcb9fef17317a2f3525c887d8aa3f4f50fed920816a688284a5b7" dependencies = [ "serde", "typeid", ] [[package]] name = "foldhash" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" [[package]] name = "globset" version = "0.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54a1028dfc5f5df5da8a56a73e6c153c9a9708ec57232470703592a3f18e49f5" dependencies = [ "aho-corasick", "bstr", "log", "regex-automata", "regex-syntax", ] [[package]] name = "hashbrown" version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d" [[package]] name = "ignore" version = "0.4.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d89fd380afde86567dfba715db065673989d6253f42b88179abd3eae47bda4b" dependencies = [ "crossbeam-deque", "globset", "log", "memchr", "regex-automata", "same-file", "walkdir", "winapi-util", ] [[package]] name = "include_dir" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd" dependencies = [ "include_dir_macros", ] [[package]] name = "include_dir_macros" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75" dependencies = [ "proc-macro2", "quote", ] [[package]] name = "indexmap" version = "2.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5" dependencies = [ "equivalent", "hashbrown", ] [[package]] name = "is_terminal_polyfill" version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "itertools" version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" dependencies = [ "either", ] [[package]] name = "itoa" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "json-write" version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4085027557b9a870495ab8b7b411e14576055491d7220c88b11b7e2ba198b297" [[package]] name = "lexarg" version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84373d01a60bb462a2e7caa28796669692d38ba075028a656bd626ac211062d4" dependencies = [ "lexarg-error", "lexarg-parser", ] [[package]] name = "lexarg-error" version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "178daf11ee95fa4bf31ff1af878dda4c637dc08a37aabdd63ae3683481fd13a9" dependencies = [ "lexarg-parser", ] [[package]] name = "lexarg-parser" version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67367be9ace12a2d51c03741b5280ff3029a833c49a4ec1193223a1a8cfbc863" [[package]] name = "libtest-json" version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f58e30343a6bfe0fb896b0bb92cf482c34ed047c637aac4b561d0b251b638c" dependencies = [ "json-write", ] [[package]] name = "libtest-lexarg" version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "914e11e515e22a1b2a0aac76c0615e6417d470789c2f0433a0598a5b6aae491f" dependencies = [ "lexarg", "lexarg-error", ] [[package]] name = "libtest2-harness" version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ca907aef7f70aceecb01e88588062946dd0af0727d80a68d9a1e928dacf07c9" dependencies = [ "anstream", "anstyle", "lexarg-error", "lexarg-parser", "libtest-json", "libtest-lexarg", ] [[package]] name = "libtest2-mimic" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a91f49bdcba4db89919ae87029787d2102f051e5dcf635df29e3bfdf55b2a4ac" dependencies = [ "libtest-json", "libtest2-harness", ] [[package]] name = "log" version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" [[package]] name = "memchr" version = "2.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" [[package]] name = "normalize-line-endings" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" [[package]] name = "num-traits" version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] [[package]] name = "once_cell_polyfill" version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" [[package]] name = "proc-macro2" version = "1.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" dependencies = [ "proc-macro2", ] [[package]] name = "regex-automata" version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b9458fa0bfeeac22b5ca447c63aaf45f28439a709ccd244698632f9aa6394d6" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caf4aa5b0f434c91fe5c7f1ecb6a5ece2130b02ad2a590589dda5146df959001" [[package]] name = "ryu" version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" [[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] [[package]] name = "serde" version = "1.0.225" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6c24dee235d0da097043389623fb913daddf92c76e9f5a1db88607a0bcbd1d" dependencies = [ "serde_core", "serde_derive", ] [[package]] name = "serde-untagged" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9faf48a4a2d2693be24c6289dbe26552776eb7737074e6722891fadbe6c5058" dependencies = [ "erased-serde", "serde", "serde_core", "typeid", ] [[package]] name = "serde_core" version = "1.0.225" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "659356f9a0cb1e529b24c01e43ad2bdf520ec4ceaf83047b83ddcc2251f96383" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.225" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ea936adf78b1f766949a4977b91d2f5595825bd6ec079aa9543ad2685fc4516" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.145" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" dependencies = [ "itoa", "memchr", "ryu", "serde", "serde_core", ] [[package]] name = "serde_spanned" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e24345aa0fe688594e73770a5f6d1b216508b4f93484c0026d521acd30134392" dependencies = [ "serde_core", ] [[package]] name = "similar" version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa" [[package]] name = "snapbox" version = "0.6.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96dcfc4581e3355d70ac2ee14cfdf81dce3d85c85f1ed9e2c1d3013f53b3436b" dependencies = [ "anstream", "anstyle", "normalize-line-endings", "similar", "snapbox-macros", ] [[package]] name = "snapbox-macros" version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "16569f53ca23a41bb6f62e0a5084aa1661f4814a67fa33696a79073e03a664af" dependencies = [ "anstream", ] [[package]] name = "syn" version = "2.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "toml" version = "0.9.8" dependencies = [ "anstream", "anstyle", "foldhash", "indexmap", "itertools", "serde", "serde-untagged", "serde_core", "serde_json", "serde_spanned", "snapbox", "toml-test-data", "toml-test-harness", "toml_datetime", "toml_parser", "toml_writer", "walkdir", "winnow", ] [[package]] name = "toml-test" version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36ecba8b17b5b0e3fe952335b981ce2fb477b3bff362d01d7db18d52b1b6733b" dependencies = [ "chrono", "ryu", "serde", "serde_json", ] [[package]] name = "toml-test-data" version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea600d3ed690c00626705b301d30e25787300d80d9dc0b582097f4d6308599c3" dependencies = [ "include_dir", ] [[package]] name = "toml-test-harness" version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99a0b9e5066013f8b5ac9c5c9402605b222636e1ab115074977bf27e745e4252" dependencies = [ "ignore", "libtest2-mimic", "snapbox", "toml-test", "toml-test-data", ] [[package]] name = "toml_datetime" version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2cdb639ebbc97961c51720f858597f7f24c4fc295327923af55b74c3c724533" dependencies = [ "serde_core", ] [[package]] name = "toml_parser" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0cbe268d35bdb4bb5a56a2de88d0ad0eb70af5384a99d648cd4b3d04039800e" dependencies = [ "anstream", "anstyle", "winnow", ] [[package]] name = "toml_writer" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df8b2b54733674ad286d16267dcfc7a71ed5c776e4ac7aa3c3e2561f7c637bf2" [[package]] name = "typeid" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" [[package]] name = "unicode-ident" version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" [[package]] name = "utf8parse" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "walkdir" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", ] [[package]] name = "winapi-util" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ "windows-sys 0.61.0", ] [[package]] name = "windows-link" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" [[package]] name = "windows-link" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "45e46c0661abb7180e7b9c281db115305d49ca1709ab8242adf09666d2173c65" [[package]] name = "windows-sys" version = "0.60.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" dependencies = [ "windows-targets", ] [[package]] name = "windows-sys" version = "0.61.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e201184e40b2ede64bc2ea34968b28e33622acdbbf37104f0e4a33f7abe657aa" dependencies = [ "windows-link 0.2.0", ] [[package]] name = "windows-targets" version = "0.53.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5fe6031c4041849d7c496a8ded650796e7b6ecc19df1a431c1a363342e5dc91" dependencies = [ "windows-link 0.1.3", "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" [[package]] name = "windows_aarch64_msvc" version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" [[package]] name = "windows_i686_gnu" version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" [[package]] name = "windows_i686_gnullvm" version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" [[package]] name = "windows_i686_msvc" version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" [[package]] name = "windows_x86_64_gnu" version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" [[package]] name = "windows_x86_64_gnullvm" version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" [[package]] name = "windows_x86_64_msvc" version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" [[package]] name = "winnow" version = "0.7.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf" toml-0.9.8/Cargo.toml0000644000000146270000000000100100350ustar # 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 = "2021" rust-version = "1.76" name = "toml" version = "0.9.8" build = false include = [ "build.rs", "src/**/*", "Cargo.toml", "Cargo.lock", "LICENSE*", "README.md", "examples/**/*", ] autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = """ A native Rust encoder and decoder of TOML-formatted files and streams. Provides implementations of the standard Serialize/Deserialize traits for TOML data to facilitate deserializing and serializing Rust structures. """ readme = "README.md" keywords = [ "encoding", "toml", "no_std", ] categories = [ "encoding", "parser-implementations", "parsing", "config", ] license = "MIT OR Apache-2.0" repository = "https://github.com/toml-rs/toml" [package.metadata.docs.rs] all-features = true rustdoc-args = ["--generate-link-to-definition"] [[package.metadata.release.pre-release-replacements]] file = "CHANGELOG.md" search = "Unreleased" replace = "{{version}}" min = 1 [[package.metadata.release.pre-release-replacements]] file = "CHANGELOG.md" search = '\.\.\.HEAD' replace = "...{{tag_name}}" exactly = 1 [[package.metadata.release.pre-release-replacements]] file = "CHANGELOG.md" search = "ReleaseDate" replace = "{{date}}" min = 1 [[package.metadata.release.pre-release-replacements]] file = "CHANGELOG.md" search = "" replace = """ ## [Unreleased] - ReleaseDate """ exactly = 1 [[package.metadata.release.pre-release-replacements]] file = "CHANGELOG.md" search = "" replace = """ [Unreleased]: https://github.com/toml-rs/toml/compare/{{tag_name}}...HEAD""" exactly = 1 [features] debug = [ "std", "toml_parser?/debug", "dep:anstream", "dep:anstyle", ] default = [ "std", "serde", "parse", "display", ] display = ["dep:toml_writer"] fast_hash = [ "preserve_order", "dep:foldhash", ] parse = [ "dep:toml_parser", "dep:winnow", ] preserve_order = [ "dep:indexmap", "std", ] serde = [ "dep:serde_core", "toml_datetime/serde", "serde_spanned/serde", ] std = [ "indexmap?/std", "serde_core?/std", "toml_parser?/std", "toml_writer?/std", "toml_datetime/std", "serde_spanned/std", ] unbounded = [] [lib] name = "toml" path = "src/lib.rs" [[example]] name = "decode" path = "examples/decode.rs" required-features = [ "parse", "display", "serde", ] [[example]] name = "enum_external" path = "examples/enum_external.rs" required-features = [ "parse", "display", "serde", ] [[example]] name = "toml2json" path = "examples/toml2json.rs" required-features = [ "parse", "display", "serde", ] [dependencies.anstream] version = "0.6.20" optional = true [dependencies.anstyle] version = "1.0.11" optional = true [dependencies.foldhash] version = "0.2.0" optional = true default-features = false [dependencies.indexmap] version = "2.11.4" optional = true default-features = false [dependencies.serde_core] version = "1.0.225" features = ["alloc"] optional = true default-features = false [dependencies.serde_spanned] version = "1.0.3" features = ["alloc"] default-features = false [dependencies.toml_datetime] version = "0.7.3" features = ["alloc"] default-features = false [dependencies.toml_parser] version = "1.0.4" features = ["alloc"] optional = true default-features = false [dependencies.toml_writer] version = "1.0.4" features = ["alloc"] optional = true default-features = false [dependencies.winnow] version = "0.7.13" optional = true default-features = false [dev-dependencies.itertools] version = "0.14.0" [dev-dependencies.serde] version = "1.0.225" features = ["derive"] [dev-dependencies.serde-untagged] version = "0.1.9" [dev-dependencies.serde_json] version = "1.0.145" [dev-dependencies.snapbox] version = "0.6.21" [dev-dependencies.toml-test-data] version = "2.3.3" [dev-dependencies.toml-test-harness] version = "1.3.3" features = ["snapshot"] [dev-dependencies.walkdir] version = "2.5.0" [lints.clippy] bool_assert_comparison = "allow" branches_sharing_code = "allow" checked_conversions = "warn" collapsible_else_if = "allow" create_dir = "warn" dbg_macro = "warn" debug_assert_with_mut_call = "warn" doc_markdown = "warn" empty_enum = "warn" enum_glob_use = "warn" expl_impl_clone_on_copy = "warn" explicit_deref_methods = "warn" explicit_into_iter_loop = "warn" fallible_impl_from = "warn" filter_map_next = "warn" flat_map_option = "warn" float_cmp_const = "warn" fn_params_excessive_bools = "warn" from_iter_instead_of_collect = "warn" get_first = "allow" if_same_then_else = "allow" implicit_clone = "warn" imprecise_flops = "warn" inconsistent_struct_constructor = "warn" inefficient_to_string = "warn" infinite_loop = "warn" invalid_upcast_comparisons = "warn" large_digit_groups = "warn" large_stack_arrays = "warn" large_types_passed_by_value = "warn" let_and_return = "allow" linkedlist = "warn" lossy_float_literal = "warn" macro_use_imports = "warn" mem_forget = "warn" mutex_integer = "warn" needless_bool = "allow" needless_continue = "allow" needless_for_each = "warn" negative_feature_names = "warn" path_buf_push_overwrite = "warn" ptr_as_ptr = "warn" rc_mutex = "warn" redundant_feature_names = "warn" ref_option_ref = "warn" rest_pat_in_fully_bound_structs = "warn" result_large_err = "allow" same_functions_in_if_condition = "warn" self_named_module_files = "warn" semicolon_if_nothing_returned = "warn" str_to_string = "warn" string_add = "warn" string_add_assign = "warn" string_lit_as_bytes = "warn" string_to_string = "warn" todo = "warn" trait_duplication_in_bounds = "warn" uninlined_format_args = "warn" use_self = "warn" verbose_file_reads = "warn" wildcard_imports = "warn" zero_sized_map_values = "warn" [lints.rust] unnameable_types = "allow" unreachable_pub = "warn" unsafe_op_in_unsafe_fn = "warn" unused_lifetimes = "warn" unused_macro_rules = "warn" unused_qualifications = "warn" [lints.rust.rust_2018_idioms] level = "warn" priority = -1 toml-0.9.8/Cargo.toml.orig000064400000000000000000000075341046102023000135150ustar 00000000000000[package] name = "toml" version = "0.9.8" description = """ A native Rust encoder and decoder of TOML-formatted files and streams. Provides implementations of the standard Serialize/Deserialize traits for TOML data to facilitate deserializing and serializing Rust structures. """ categories = ["encoding", "parser-implementations", "parsing", "config"] keywords = ["encoding", "toml", "no_std"] repository.workspace = true license.workspace = true edition.workspace = true rust-version.workspace = true include.workspace = true [package.metadata.docs.rs] all-features = true rustdoc-args = ["--generate-link-to-definition"] [package.metadata.release] pre-release-replacements = [ {file="CHANGELOG.md", search="Unreleased", replace="{{version}}", min=1}, {file="CHANGELOG.md", search="\\.\\.\\.HEAD", replace="...{{tag_name}}", exactly=1}, {file="CHANGELOG.md", search="ReleaseDate", replace="{{date}}", min=1}, {file="CHANGELOG.md", search="", replace="\n## [Unreleased] - ReleaseDate\n", exactly=1}, {file="CHANGELOG.md", search="", replace="\n[Unreleased]: https://github.com/toml-rs/toml/compare/{{tag_name}}...HEAD", exactly=1}, ] [features] default = ["std", "serde", "parse", "display"] std = ["indexmap?/std", "serde_core?/std", "toml_parser?/std", "toml_writer?/std", "toml_datetime/std", "serde_spanned/std"] serde = ["dep:serde_core", "toml_datetime/serde", "serde_spanned/serde"] parse = ["dep:toml_parser", "dep:winnow"] display = ["dep:toml_writer"] fast_hash = ["preserve_order", "dep:foldhash"] debug = ["std", "toml_parser?/debug", "dep:anstream", "dep:anstyle"] # Provide a method disable_recursion_limit to parse arbitrarily deep structures # without any consideration for overflowing the stack. Additionally you will # need to be careful around other recursive operations on the parsed result # which may overflow the stack after deserialization has completed, including, # but not limited to, Display and Debug and Drop impls. unbounded = [] # Use indexmap rather than BTreeMap as the map type of toml::Value. # This allows data to be read into a Value and written back to a TOML string # while preserving the order of map keys in the input. preserve_order = ["dep:indexmap", "std"] [dependencies] serde_core = { version = "1.0.225", default-features = false, features = ["alloc"], optional = true } indexmap = { version = "2.11.4", default-features = false, optional = true } toml_parser = { version = "1.0.4", path = "../toml_parser", default-features = false, features = ["alloc"], optional = true } winnow = { version = "0.7.13", default-features = false, optional = true } anstream = { version = "0.6.20", optional = true } anstyle = { version = "1.0.11", optional = true } toml_datetime = { version = "0.7.3", path = "../toml_datetime", default-features = false, features = ["alloc"] } toml_writer = { version = "1.0.4", path = "../toml_writer", default-features = false, features = ["alloc"], optional = true } serde_spanned = { version = "1.0.3", path = "../serde_spanned", default-features = false, features = ["alloc"] } foldhash = { version = "0.2.0", default-features = false, optional = true } [dev-dependencies] serde = { version = "1.0.225", features = ["derive"] } serde_json = "1.0.145" toml-test-harness = { version = "1.3.3", features = ["snapshot"] } toml-test-data = "2.3.3" snapbox = "0.6.21" walkdir = "2.5.0" itertools = "0.14.0" serde-untagged = "0.1.9" [[test]] name = "decoder_compliance" harness = false [[test]] name = "encoder_compliance" harness = false [[test]] name = "encoder_pretty_compliance" harness = false [[example]] name = "decode" required-features = ["parse", "display", "serde"] [[example]] name = "enum_external" required-features = ["parse", "display", "serde"] [[example]] name = "toml2json" required-features = ["parse", "display", "serde"] [lints] workspace = true toml-0.9.8/LICENSE-APACHE000064400000000000000000000261361046102023000125510ustar 00000000000000 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. toml-0.9.8/LICENSE-MIT000064400000000000000000000020461046102023000122530ustar 00000000000000Copyright (c) Individual contributors 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. toml-0.9.8/README.md000064400000000000000000000016371046102023000121030ustar 00000000000000# toml [![Latest Version](https://img.shields.io/crates/v/toml.svg)](https://crates.io/crates/toml) [![Documentation](https://docs.rs/toml/badge.svg)](https://docs.rs/toml) A [serde]-compatible [TOML][toml] decoder and encoder for Rust. For format-preserving editing or finer control over output, see [toml_edit] [serde]: https://serde.rs/ [toml]: https://github.com/toml-lang/toml [toml_edit]: https://docs.rs/toml_edit ## License Licensed under either of * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or ) * MIT license ([LICENSE-MIT](LICENSE-MIT) or ) at your option. ### Contribution 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. toml-0.9.8/examples/decode.rs000064400000000000000000000023071046102023000142260ustar 00000000000000//! An example showing off the usage of `Deserialize` to automatically decode //! TOML into a Rust `struct` #![deny(warnings)] #![allow(dead_code)] use serde::Deserialize; /// This is what we're going to decode into. Each field is optional, meaning /// that it doesn't have to be present in TOML. #[derive(Debug, Deserialize)] struct Config { global_string: Option, global_integer: Option, server: Option, peers: Option>, } /// Sub-structs are decoded from tables, so this will decode from the `[server]` /// table. /// /// Again, each field is optional, meaning they don't have to be present. #[derive(Debug, Deserialize)] struct ServerConfig { ip: Option, port: Option, } #[derive(Debug, Deserialize)] struct PeerConfig { ip: Option, port: Option, } fn main() { let toml_str = r#" global_string = "test" global_integer = 5 [server] ip = "127.0.0.1" port = 80 [[peers]] ip = "127.0.0.1" port = 8080 [[peers]] ip = "127.0.0.1" "#; let decoded: Config = toml::from_str(toml_str).unwrap(); println!("{decoded:#?}"); } toml-0.9.8/examples/enum_external.rs000064400000000000000000000020171046102023000156470ustar 00000000000000//! An example showing off the usage of `Deserialize` to automatically decode //! TOML into a Rust `struct`, with enums. #![deny(warnings)] #![allow(dead_code)] use serde::Deserialize; /// This is what we're going to decode into. #[derive(Debug, Deserialize)] struct Config { plain: MyEnum, plain_table: MyEnum, tuple: MyEnum, #[serde(rename = "struct")] structv: MyEnum, newtype: MyEnum, my_enum: Vec, } #[derive(Debug, Deserialize)] enum MyEnum { Plain, Tuple(i64, bool), NewType(String), Struct { value: i64 }, } fn main() { let toml_str = r#" plain = "Plain" plain_table = { Plain = {} } tuple = { Tuple = { 0 = 123, 1 = true } } struct = { Struct = { value = 123 } } newtype = { NewType = "value" } my_enum = [ { Plain = {} }, { Tuple = { 0 = 123, 1 = true } }, { NewType = "value" }, { Struct = { value = 123 } } ]"#; let decoded: Config = toml::from_str(toml_str).unwrap(); println!("{decoded:#?}"); } toml-0.9.8/examples/toml2json.rs000064400000000000000000000024011046102023000147250ustar 00000000000000use std::env; use std::io; use std::io::prelude::*; use serde_json::Value as Json; use toml::Value as Toml; fn main() { let mut args = env::args(); let input = if args.len() > 1 { let name = args.nth(1).unwrap(); std::fs::read_to_string(name).unwrap() } else { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); input }; match input.parse() { Ok(toml) => { let json = convert(toml); println!("{}", serde_json::to_string_pretty(&json).unwrap()); } Err(error) => println!("failed to parse TOML: {error}"), } } fn convert(toml: Toml) -> Json { match toml { Toml::String(s) => Json::String(s), Toml::Integer(i) => Json::Number(i.into()), Toml::Float(f) => { let n = serde_json::Number::from_f64(f).expect("float infinite and nan not allowed"); Json::Number(n) } Toml::Boolean(b) => Json::Bool(b), Toml::Array(arr) => Json::Array(arr.into_iter().map(convert).collect()), Toml::Table(table) => { Json::Object(table.into_iter().map(|(k, v)| (k, convert(v))).collect()) } Toml::Datetime(dt) => Json::String(dt.to_string()), } } toml-0.9.8/src/de/deserializer/array.rs000064400000000000000000000044601046102023000161460ustar 00000000000000use serde_spanned::Spanned; use crate::de::DeArray; use crate::de::DeValue; use crate::de::Error; pub(crate) struct ArrayDeserializer<'i> { input: DeArray<'i>, span: core::ops::Range, } impl<'i> ArrayDeserializer<'i> { pub(crate) fn new(input: DeArray<'i>, span: core::ops::Range) -> Self { Self { input, span } } } impl<'de> serde_core::Deserializer<'de> for ArrayDeserializer<'de> { type Error = Error; fn deserialize_any(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { visitor.visit_seq(ArraySeqAccess::new(self.input)) } fn deserialize_struct( self, name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { if serde_spanned::de::is_spanned(name) { let span = self.span.clone(); return visitor.visit_map(super::SpannedDeserializer::new(self, span)); } self.deserialize_any(visitor) } serde_core::forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bytes byte_buf map option unit newtype_struct ignored_any unit_struct tuple_struct tuple enum identifier } } impl<'de> serde_core::de::IntoDeserializer<'de, Error> for ArrayDeserializer<'de> { type Deserializer = Self; fn into_deserializer(self) -> Self::Deserializer { self } } pub(crate) struct ArraySeqAccess<'i> { iter: alloc::vec::IntoIter>>, } impl<'i> ArraySeqAccess<'i> { pub(crate) fn new(input: DeArray<'i>) -> Self { Self { iter: input.into_iter(), } } } impl<'de> serde_core::de::SeqAccess<'de> for ArraySeqAccess<'de> { type Error = Error; fn next_element_seed(&mut self, seed: T) -> Result, Self::Error> where T: serde_core::de::DeserializeSeed<'de>, { match self.iter.next() { Some(v) => { let span = v.span(); let v = v.into_inner(); seed.deserialize(crate::de::ValueDeserializer::with_parts(v, span)) .map(Some) } None => Ok(None), } } } toml-0.9.8/src/de/deserializer/key.rs000064400000000000000000000160631046102023000156220ustar 00000000000000use serde_core::de::IntoDeserializer; use crate::de::DeString; use crate::de::Error; pub(crate) struct KeyDeserializer<'i> { span: Option>, key: DeString<'i>, } impl<'i> KeyDeserializer<'i> { pub(crate) fn new(key: DeString<'i>, span: Option>) -> Self { KeyDeserializer { span, key } } } impl<'de> IntoDeserializer<'de, Error> for KeyDeserializer<'de> { type Deserializer = Self; fn into_deserializer(self) -> Self::Deserializer { self } } impl<'de> serde_core::de::Deserializer<'de> for KeyDeserializer<'de> { type Error = Error; fn deserialize_any(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { self.key.into_deserializer().deserialize_any(visitor) } fn deserialize_bool(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: bool = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_bool(visitor) } fn deserialize_i8(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: i8 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_i8(visitor) } fn deserialize_i16(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: i16 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_i16(visitor) } fn deserialize_i32(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: i32 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_i32(visitor) } fn deserialize_i64(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: i64 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_i64(visitor) } fn deserialize_i128(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: i128 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_i128(visitor) } fn deserialize_u8(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: u8 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_u8(visitor) } fn deserialize_u16(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: u16 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_u16(visitor) } fn deserialize_u32(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: u32 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_u32(visitor) } fn deserialize_u64(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: u64 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_u64(visitor) } fn deserialize_u128(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: u128 = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_u128(visitor) } fn deserialize_char(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let key: char = self.key.parse().map_err(serde_core::de::Error::custom)?; key.into_deserializer().deserialize_char(visitor) } fn deserialize_enum( self, name: &str, variants: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { let _ = name; let _ = variants; visitor.visit_enum(self) } fn deserialize_struct( self, name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { if serde_spanned::de::is_spanned(name) { if let Some(span) = self.span.clone() { return visitor.visit_map(super::SpannedDeserializer::new(self.key, span)); } else { return Err(Error::custom("value is missing a span", None)); } } self.deserialize_any(visitor) } fn deserialize_newtype_struct( self, _name: &'static str, visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { visitor.visit_newtype_struct(self) } serde_core::forward_to_deserialize_any! { f32 f64 str string seq bytes byte_buf map option unit ignored_any unit_struct tuple_struct tuple identifier } } impl<'de> serde_core::de::EnumAccess<'de> for KeyDeserializer<'de> { type Error = Error; type Variant = UnitOnly; fn variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> where T: serde_core::de::DeserializeSeed<'de>, { seed.deserialize(self).map(unit_only) } } pub(crate) struct UnitOnly { marker: core::marker::PhantomData, } fn unit_only(t: T) -> (T, UnitOnly) { ( t, UnitOnly { marker: core::marker::PhantomData, }, ) } impl<'de, E> serde_core::de::VariantAccess<'de> for UnitOnly where E: serde_core::de::Error, { type Error = E; fn unit_variant(self) -> Result<(), Self::Error> { Ok(()) } fn newtype_variant_seed(self, _seed: T) -> Result where T: serde_core::de::DeserializeSeed<'de>, { Err(serde_core::de::Error::invalid_type( serde_core::de::Unexpected::UnitVariant, &"newtype variant", )) } fn tuple_variant(self, _len: usize, _visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { Err(serde_core::de::Error::invalid_type( serde_core::de::Unexpected::UnitVariant, &"tuple variant", )) } fn struct_variant( self, _fields: &'static [&'static str], _visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { Err(serde_core::de::Error::invalid_type( serde_core::de::Unexpected::UnitVariant, &"struct variant", )) } } toml-0.9.8/src/de/deserializer/mod.rs000064400000000000000000000113051046102023000156030ustar 00000000000000//! Deserializing TOML into Rust structures. //! //! This module contains all the Serde support for deserializing TOML documents //! into Rust structures. Note that some top-level functions here are also //! provided at the top of the crate. mod array; mod key; mod table; mod table_enum; mod value; pub use value::ValueDeserializer; use crate::de::DeTable; use crate::de::DeValue; use crate::de::Error; use array::ArrayDeserializer; use key::KeyDeserializer; use serde_spanned::de::SpannedDeserializer; use serde_spanned::Spanned; use table::TableDeserializer; use table_enum::TableEnumDeserializer; use toml_datetime::de::DatetimeDeserializer; /// Deserialization for TOML [documents][crate::Table]. /// /// To deserializes TOML values, instead of documents, see [`ValueDeserializer`]. pub struct Deserializer<'i> { span: core::ops::Range, root: DeTable<'i>, raw: Option<&'i str>, } impl<'i> Deserializer<'i> { /// Parse a TOML document pub fn parse(raw: &'i str) -> Result { let root = DeTable::parse(raw)?; let span = root.span(); let root = root.into_inner(); Ok(Self { span, root, raw: Some(raw), }) } /// Deprecated, replaced with [`Deserializer::parse`] #[deprecated(since = "0.9.0", note = "replaced with `Deserializer::parse`")] pub fn new(raw: &'i str) -> Result { Self::parse(raw) } fn into_table_de(self) -> ValueDeserializer<'i> { ValueDeserializer::with_parts(DeValue::Table(self.root), self.span) } } impl<'i> From>> for Deserializer<'i> { fn from(root: Spanned>) -> Self { let span = root.span(); let root = root.into_inner(); Self { span, root, raw: None, } } } impl<'de> serde_core::Deserializer<'de> for Deserializer<'de> { type Error = Error; fn deserialize_any(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let raw = self.raw; self.into_table_de() .deserialize_any(visitor) .map_err(|mut e: Self::Error| { e.set_input(raw); e }) } // `None` is interpreted as a missing field so be sure to implement `Some` // as a present field. fn deserialize_option(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let raw = self.raw; self.into_table_de() .deserialize_option(visitor) .map_err(|mut e: Self::Error| { e.set_input(raw); e }) } fn deserialize_newtype_struct( self, name: &'static str, visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { let raw = self.raw; self.into_table_de() .deserialize_newtype_struct(name, visitor) .map_err(|mut e: Self::Error| { e.set_input(raw); e }) } fn deserialize_struct( self, name: &'static str, fields: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { let raw = self.raw; self.into_table_de() .deserialize_struct(name, fields, visitor) .map_err(|mut e: Self::Error| { e.set_input(raw); e }) } // Called when the type to deserialize is an enum, as opposed to a field in the type. fn deserialize_enum( self, name: &'static str, variants: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { let raw = self.raw; self.into_table_de() .deserialize_enum(name, variants, visitor) .map_err(|mut e: Self::Error| { e.set_input(raw); e }) } serde_core::forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bytes byte_buf map unit ignored_any unit_struct tuple_struct tuple identifier } } impl<'de> serde_core::de::IntoDeserializer<'de, Error> for Deserializer<'de> { type Deserializer = Self; fn into_deserializer(self) -> Self::Deserializer { self } } impl<'de> serde_core::de::IntoDeserializer<'de, Error> for Spanned> { type Deserializer = Deserializer<'de>; fn into_deserializer(self) -> Self::Deserializer { Deserializer::from(self) } } toml-0.9.8/src/de/deserializer/table.rs000064400000000000000000000142061046102023000161160ustar 00000000000000use serde_core::de::IntoDeserializer; use serde_spanned::Spanned; use crate::de::DeString; use crate::de::DeTable; use crate::de::DeValue; use crate::de::Error; use crate::map::IntoIter; pub(crate) struct TableDeserializer<'i> { span: core::ops::Range, items: DeTable<'i>, } impl<'i> TableDeserializer<'i> { pub(crate) fn new(items: DeTable<'i>, span: core::ops::Range) -> Self { Self { span, items } } } impl<'de> serde_core::Deserializer<'de> for TableDeserializer<'de> { type Error = Error; fn deserialize_any(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { visitor.visit_map(TableMapAccess::new(self)) } // `None` is interpreted as a missing field so be sure to implement `Some` // as a present field. fn deserialize_option(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { visitor.visit_some(self) } fn deserialize_newtype_struct( self, _name: &'static str, visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { visitor.visit_newtype_struct(self) } fn deserialize_struct( self, name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { if serde_spanned::de::is_spanned(name) { let span = self.span.clone(); return visitor.visit_map(super::SpannedDeserializer::new(self, span)); } self.deserialize_any(visitor) } // Called when the type to deserialize is an enum, as opposed to a field in the type. fn deserialize_enum( self, _name: &'static str, _variants: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { if self.items.is_empty() { Err(Error::custom( "wanted exactly 1 element, found 0 elements", Some(self.span), )) } else if self.items.len() != 1 { Err(Error::custom( "wanted exactly 1 element, more than 1 element", Some(self.span), )) } else { visitor.visit_enum(TableMapAccess::new(self)) } } serde_core::forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bytes byte_buf map unit ignored_any unit_struct tuple_struct tuple identifier } } impl<'de> IntoDeserializer<'de, Error> for TableDeserializer<'de> { type Deserializer = Self; fn into_deserializer(self) -> Self::Deserializer { self } } pub(crate) struct TableMapAccess<'i> { iter: IntoIter>, Spanned>>, span: core::ops::Range, value: Option<(Spanned>, Spanned>)>, } impl<'i> TableMapAccess<'i> { pub(crate) fn new(input: TableDeserializer<'i>) -> Self { Self { iter: input.items.into_iter(), span: input.span, value: None, } } } impl<'de> serde_core::de::MapAccess<'de> for TableMapAccess<'de> { type Error = Error; fn next_key_seed(&mut self, seed: K) -> Result, Self::Error> where K: serde_core::de::DeserializeSeed<'de>, { match self.iter.next() { Some((k, v)) => { let key_span = k.span(); let ret = seed .deserialize(super::KeyDeserializer::new( k.clone().into_inner(), Some(key_span.clone()), )) .map(Some) .map_err(|mut e: Self::Error| { if e.span().is_none() { e.set_span(Some(key_span)); } e }); self.value = Some((k, v)); ret } None => Ok(None), } } fn next_value_seed(&mut self, seed: V) -> Result where V: serde_core::de::DeserializeSeed<'de>, { match self.value.take() { Some((k, v)) => { let span = v.span(); seed.deserialize(crate::de::ValueDeserializer::with_parts( v.into_inner(), span.clone(), )) .map_err(|mut e: Self::Error| { if e.span().is_none() { e.set_span(Some(span)); } e.add_key(k.into_inner().into_owned()); e }) } None => { panic!("no more values in next_value_seed, internal error in ValueDeserializer") } } } } impl<'de> serde_core::de::EnumAccess<'de> for TableMapAccess<'de> { type Error = Error; type Variant = super::TableEnumDeserializer<'de>; fn variant_seed(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> where V: serde_core::de::DeserializeSeed<'de>, { let (key, value) = match self.iter.next() { Some(pair) => pair, None => { return Err(Error::custom( "expected table with exactly 1 entry, found empty table", Some(self.span), )); } }; let key_span = key.span(); let val = seed .deserialize(super::KeyDeserializer::new( key.into_inner(), Some(key_span.clone()), )) .map_err(|mut e: Self::Error| { if e.span().is_none() { e.set_span(Some(key_span)); } e })?; let value_span = value.span(); let value = value.into_inner(); let variant = super::TableEnumDeserializer::new(value, value_span); Ok((val, variant)) } } toml-0.9.8/src/de/deserializer/table_enum.rs000064400000000000000000000102021046102023000171320ustar 00000000000000use crate::alloc_prelude::*; use crate::de::DeArray; use crate::de::DeValue; use crate::de::Error; /// Deserializes table values into enum variants. pub(crate) struct TableEnumDeserializer<'i> { value: DeValue<'i>, span: core::ops::Range, } impl<'i> TableEnumDeserializer<'i> { pub(crate) fn new(value: DeValue<'i>, span: core::ops::Range) -> Self { TableEnumDeserializer { value, span } } } impl<'de> serde_core::de::VariantAccess<'de> for TableEnumDeserializer<'de> { type Error = Error; fn unit_variant(self) -> Result<(), Self::Error> { match self.value { DeValue::Array(values) => { if values.is_empty() { Ok(()) } else { Err(Error::custom("expected empty array", Some(self.span))) } } DeValue::Table(values) => { if values.is_empty() { Ok(()) } else { Err(Error::custom("expected empty table", Some(self.span))) } } e => Err(Error::custom( format!("expected table, found {}", e.type_str()), Some(self.span), )), } } fn newtype_variant_seed(self, seed: T) -> Result where T: serde_core::de::DeserializeSeed<'de>, { seed.deserialize(super::ValueDeserializer::with_parts(self.value, self.span)) } fn tuple_variant(self, len: usize, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { match self.value { DeValue::Array(values) => { let values_span = self.span.clone(); let tuple_values = values; if tuple_values.len() == len { serde_core::de::Deserializer::deserialize_seq( super::ArrayDeserializer::new(tuple_values, values_span), visitor, ) } else { Err(Error::custom( format!("expected tuple with length {len}"), Some(values_span), )) } } DeValue::Table(values) => { let values_span = self.span.clone(); let tuple_values: Result, _> = values .into_iter() .enumerate() .map( |(index, (key, value))| match key.get_ref().parse::() { Ok(key_index) if key_index == index => Ok(value), Ok(_) | Err(_) => Err(Error::custom( format!("expected table key `{index}`, but was `{key}`"), Some(key.span()), )), }, ) .collect(); let tuple_values = tuple_values?; if tuple_values.len() == len { serde_core::de::Deserializer::deserialize_seq( super::ArrayDeserializer::new(tuple_values, values_span), visitor, ) } else { Err(Error::custom( format!("expected tuple with length {len}"), Some(values_span), )) } } e => Err(Error::custom( format!("expected table, found {}", e.type_str()), Some(self.span), )), } } fn struct_variant( self, fields: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { serde_core::de::Deserializer::deserialize_struct( super::ValueDeserializer::with_parts(self.value, self.span) .with_struct_key_validation(), "", // TODO: this should be the variant name fields, visitor, ) } } toml-0.9.8/src/de/deserializer/value.rs000064400000000000000000000214071046102023000161440ustar 00000000000000use serde_core::de::IntoDeserializer as _; use serde_spanned::Spanned; use super::ArrayDeserializer; use super::DatetimeDeserializer; use super::TableDeserializer; use crate::alloc_prelude::*; use crate::de::DeString; use crate::de::DeTable; use crate::de::DeValue; use crate::de::Error; /// Deserialization implementation for TOML [values][crate::Value]. /// /// # Example /// /// ``` /// # #[cfg(feature = "parse")] { /// # #[cfg(feature = "display")] { /// use serde::Deserialize; /// /// #[derive(Deserialize)] /// struct Config { /// title: String, /// owner: Owner, /// } /// /// #[derive(Deserialize)] /// struct Owner { /// name: String, /// } /// /// let value = r#"{ title = 'TOML Example', owner = { name = 'Lisa' } }"#; /// let deserializer = toml::de::ValueDeserializer::parse(value).unwrap(); /// let config = Config::deserialize(deserializer).unwrap(); /// assert_eq!(config.title, "TOML Example"); /// assert_eq!(config.owner.name, "Lisa"); /// # } /// # } /// ``` pub struct ValueDeserializer<'i> { span: core::ops::Range, input: DeValue<'i>, validate_struct_keys: bool, } impl<'i> ValueDeserializer<'i> { /// Parse a TOML value pub fn parse(raw: &'i str) -> Result { let input = DeValue::parse(raw)?; let span = input.span(); let input = input.into_inner(); Ok(Self::with_parts(input, span)) } /// Deprecated, replaced with [`ValueDeserializer::parse`] #[deprecated(since = "0.9.0", note = "replaced with `ValueDeserializer::parse`")] pub fn new(raw: &'i str) -> Result { Self::parse(raw) } pub(crate) fn with_parts(input: DeValue<'i>, span: core::ops::Range) -> Self { Self { input, span, validate_struct_keys: false, } } pub(crate) fn with_struct_key_validation(mut self) -> Self { self.validate_struct_keys = true; self } } impl<'i> From>> for ValueDeserializer<'i> { fn from(root: Spanned>) -> Self { let span = root.span(); let root = root.into_inner(); Self::with_parts(root, span) } } impl<'de> serde_core::Deserializer<'de> for ValueDeserializer<'de> { type Error = Error; fn deserialize_any(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let span = self.span.clone(); match self.input { DeValue::String(DeString::Owned(v)) => visitor.visit_string(v), DeValue::String(DeString::Borrowed(v)) => visitor.visit_str(v), DeValue::Integer(v) => { if let Some(v) = v.to_i64() { visitor.visit_i64(v) } else if let Some(v) = v.to_u64() { visitor.visit_u64(v) } else if let Some(v) = v.to_i128() { visitor.visit_i128(v) } else if let Some(v) = v.to_u128() { visitor.visit_u128(v) } else { Err(Error::custom("integer number overflowed", None)) } } DeValue::Float(v) => { if let Some(v) = v.to_f64() { visitor.visit_f64(v) } else { Err(Error::custom("floating-point number overflowed", None)) } } DeValue::Boolean(v) => visitor.visit_bool(v), DeValue::Datetime(v) => visitor.visit_map(DatetimeDeserializer::new(v)), DeValue::Array(v) => ArrayDeserializer::new(v, span.clone()).deserialize_any(visitor), DeValue::Table(v) => TableDeserializer::new(v, span.clone()).deserialize_any(visitor), } .map_err(|mut e: Self::Error| { if e.span().is_none() { e.set_span(Some(span)); } e }) } fn deserialize_u128(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { self.deserialize_any(visitor) } fn deserialize_i128(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { self.deserialize_any(visitor) } // `None` is interpreted as a missing field so be sure to implement `Some` // as a present field. fn deserialize_option(self, visitor: V) -> Result where V: serde_core::de::Visitor<'de>, { let span = self.span.clone(); visitor.visit_some(self).map_err(|mut e: Self::Error| { if e.span().is_none() { e.set_span(Some(span)); } e }) } fn deserialize_newtype_struct( self, _name: &'static str, visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { let span = self.span.clone(); visitor .visit_newtype_struct(self) .map_err(|mut e: Self::Error| { if e.span().is_none() { e.set_span(Some(span)); } e }) } fn deserialize_struct( self, name: &'static str, fields: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { if serde_spanned::de::is_spanned(name) { let span = self.span.clone(); return visitor.visit_map(super::SpannedDeserializer::new(self, span)); } if toml_datetime::de::is_datetime(name) { let span = self.span.clone(); if let DeValue::Datetime(d) = self.input { return visitor.visit_map(DatetimeDeserializer::new(d)).map_err( |mut e: Self::Error| { if e.span().is_none() { e.set_span(Some(span)); } e }, ); } } if self.validate_struct_keys { let span = self.span.clone(); match &self.input { DeValue::Table(values) => validate_struct_keys(values, fields), _ => Ok(()), } .map_err(|mut e: Self::Error| { if e.span().is_none() { e.set_span(Some(span)); } e })?; } self.deserialize_any(visitor) } // Called when the type to deserialize is an enum, as opposed to a field in the type. fn deserialize_enum( self, name: &'static str, variants: &'static [&'static str], visitor: V, ) -> Result where V: serde_core::de::Visitor<'de>, { let span = self.span.clone(); match self.input { DeValue::String(v) => visitor.visit_enum(v.into_deserializer()), DeValue::Table(v) => { TableDeserializer::new(v, span.clone()).deserialize_enum(name, variants, visitor) } _ => Err(Error::custom("wanted string or table", Some(span.clone()))), } .map_err(|mut e: Self::Error| { if e.span().is_none() { e.set_span(Some(span)); } e }) } serde_core::forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bytes byte_buf map unit ignored_any unit_struct tuple_struct tuple identifier } } impl<'de> serde_core::de::IntoDeserializer<'de, Error> for ValueDeserializer<'de> { type Deserializer = Self; fn into_deserializer(self) -> Self::Deserializer { self } } impl<'de> serde_core::de::IntoDeserializer<'de, Error> for Spanned> { type Deserializer = ValueDeserializer<'de>; fn into_deserializer(self) -> Self::Deserializer { ValueDeserializer::from(self) } } pub(crate) fn validate_struct_keys( table: &DeTable<'_>, fields: &'static [&'static str], ) -> Result<(), Error> { let extra_fields = table .keys() .filter_map(|key| { if !fields.contains(&key.get_ref().as_ref()) { Some(key.clone()) } else { None } }) .collect::>(); if extra_fields.is_empty() { Ok(()) } else { Err(Error::custom( format!( "unexpected keys in table: {}, available keys: {}", extra_fields .iter() .map(|k| k.get_ref().as_ref()) .collect::>() .join(", "), fields.join(", "), ), Some(extra_fields[0].span()), )) } } toml-0.9.8/src/de/error.rs000064400000000000000000000211271046102023000134760ustar 00000000000000use crate::alloc_prelude::*; /// Errors that can occur when deserializing a type. #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct Error { message: String, input: Option>, keys: Vec, span: Option>, } impl Error { #[cfg(feature = "parse")] pub(crate) fn new(input: alloc::sync::Arc, error: toml_parser::ParseError) -> Self { let mut message = String::new(); message.push_str(error.description()); if let Some(expected) = error.expected() { message.push_str(", expected "); if expected.is_empty() { message.push_str("nothing"); } else { for (i, expected) in expected.iter().enumerate() { if i != 0 { message.push_str(", "); } match expected { toml_parser::Expected::Literal(desc) => { message.push_str(&render_literal(desc)); } toml_parser::Expected::Description(desc) => message.push_str(desc), _ => message.push_str("etc"), } } } } let span = error.unexpected().map(|span| span.start()..span.end()); Self { message, input: Some(input), keys: Vec::new(), span, } } pub(crate) fn custom(msg: T, span: Option>) -> Self where T: core::fmt::Display, { Self { message: msg.to_string(), input: None, keys: Vec::new(), span, } } pub(crate) fn add_key(&mut self, key: String) { self.keys.insert(0, key); } /// What went wrong pub fn message(&self) -> &str { &self.message } /// The start/end index into the original document where the error occurred pub fn span(&self) -> Option> { self.span.clone() } pub(crate) fn set_span(&mut self, span: Option>) { self.span = span; } /// Provide the encoded TOML the error applies to pub fn set_input(&mut self, input: Option<&str>) { self.input = input.map(|s| s.into()); } } #[cfg(feature = "serde")] impl serde_core::de::Error for Error { fn custom(msg: T) -> Self where T: core::fmt::Display, { Self::custom(msg.to_string(), None) } } fn render_literal(literal: &str) -> String { match literal { "\n" => "newline".to_owned(), "`" => "'`'".to_owned(), s if s.chars().all(|c| c.is_ascii_control()) => { format!("`{}`", s.escape_debug()) } s => format!("`{s}`"), } } /// Displays a TOML parse error /// /// # Example /// /// TOML parse error at line 1, column 10 /// | /// 1 | 00:32:00.a999999 /// | ^ /// Unexpected `a` /// Expected `digit` /// While parsing a Time /// While parsing a Date-Time impl core::fmt::Display for Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let mut context = false; if let (Some(input), Some(span)) = (&self.input, self.span()) { context = true; let (line, column) = translate_position(input.as_bytes(), span.start); let line_num = line + 1; let col_num = column + 1; let gutter = line_num.to_string().len(); let content = input.split('\n').nth(line).expect("valid line number"); let highlight_len = span.end - span.start; // Allow highlight to go one past the line let highlight_len = highlight_len.min(content.len().saturating_sub(column)); writeln!(f, "TOML parse error at line {line_num}, column {col_num}")?; // | for _ in 0..=gutter { write!(f, " ")?; } writeln!(f, "|")?; // 1 | 00:32:00.a999999 write!(f, "{line_num} | ")?; writeln!(f, "{content}")?; // | ^ for _ in 0..=gutter { write!(f, " ")?; } write!(f, "|")?; for _ in 0..=column { write!(f, " ")?; } // The span will be empty at eof, so we need to make sure we always print at least // one `^` write!(f, "^")?; for _ in 1..highlight_len { write!(f, "^")?; } writeln!(f)?; } writeln!(f, "{}", self.message)?; if !context && !self.keys.is_empty() { writeln!(f, "in `{}`", self.keys.join("."))?; } Ok(()) } } #[cfg(feature = "std")] impl std::error::Error for Error {} #[cfg(not(feature = "std"))] #[cfg(feature = "serde")] impl serde_core::de::StdError for Error {} fn translate_position(input: &[u8], index: usize) -> (usize, usize) { if input.is_empty() { return (0, index); } let safe_index = index.min(input.len() - 1); let column_offset = index - safe_index; let index = safe_index; let nl = input[0..index] .iter() .rev() .enumerate() .find(|(_, b)| **b == b'\n') .map(|(nl, _)| index - nl - 1); let line_start = match nl { Some(nl) => nl + 1, None => 0, }; let line = input[0..line_start].iter().filter(|b| **b == b'\n').count(); let column = core::str::from_utf8(&input[line_start..=index]) .map(|s| s.chars().count() - 1) .unwrap_or_else(|_| index - line_start); let column = column + column_offset; (line, column) } #[cfg(feature = "parse")] pub(crate) struct TomlSink<'i, S> { source: toml_parser::Source<'i>, input: Option>, sink: S, } #[cfg(feature = "parse")] impl<'i, S: Default> TomlSink<'i, S> { pub(crate) fn new(source: toml_parser::Source<'i>) -> Self { Self { source, input: None, sink: Default::default(), } } pub(crate) fn into_inner(self) -> S { self.sink } } #[cfg(feature = "parse")] impl<'i> toml_parser::ErrorSink for TomlSink<'i, Option> { fn report_error(&mut self, error: toml_parser::ParseError) { if self.sink.is_none() { let input = self .input .get_or_insert_with(|| alloc::sync::Arc::from(self.source.input())); let error = Error::new(input.clone(), error); self.sink = Some(error); } } } #[cfg(feature = "parse")] impl<'i> toml_parser::ErrorSink for TomlSink<'i, Vec> { fn report_error(&mut self, error: toml_parser::ParseError) { let input = self .input .get_or_insert_with(|| alloc::sync::Arc::from(self.source.input())); let error = Error::new(input.clone(), error); self.sink.push(error); } } #[cfg(test)] mod test_translate_position { use super::*; #[test] fn empty() { let input = b""; let index = 0; let position = translate_position(&input[..], index); assert_eq!(position, (0, 0)); } #[test] fn start() { let input = b"Hello"; let index = 0; let position = translate_position(&input[..], index); assert_eq!(position, (0, 0)); } #[test] fn end() { let input = b"Hello"; let index = input.len() - 1; let position = translate_position(&input[..], index); assert_eq!(position, (0, input.len() - 1)); } #[test] fn after() { let input = b"Hello"; let index = input.len(); let position = translate_position(&input[..], index); assert_eq!(position, (0, input.len())); } #[test] fn first_line() { let input = b"Hello\nWorld\n"; let index = 2; let position = translate_position(&input[..], index); assert_eq!(position, (0, 2)); } #[test] fn end_of_line() { let input = b"Hello\nWorld\n"; let index = 5; let position = translate_position(&input[..], index); assert_eq!(position, (0, 5)); } #[test] fn start_of_second_line() { let input = b"Hello\nWorld\n"; let index = 6; let position = translate_position(&input[..], index); assert_eq!(position, (1, 0)); } #[test] fn second_line() { let input = b"Hello\nWorld\n"; let index = 8; let position = translate_position(&input[..], index); assert_eq!(position, (1, 2)); } } toml-0.9.8/src/de/mod.rs000064400000000000000000000044331046102023000131250ustar 00000000000000//! Deserializing TOML into Rust structures. //! //! This module contains all the Serde support for deserializing TOML documents //! into Rust structures. Note that some top-level functions here are also //! provided at the top of the crate. #[cfg(feature = "parse")] #[cfg(feature = "serde")] mod deserializer; mod error; #[cfg(feature = "parse")] mod parser; #[cfg(feature = "parse")] #[cfg(feature = "serde")] pub use deserializer::Deserializer; #[cfg(feature = "parse")] #[cfg(feature = "serde")] pub use deserializer::ValueDeserializer; #[cfg(feature = "parse")] pub use parser::DeArray; #[cfg(feature = "parse")] pub use parser::DeFloat; #[cfg(feature = "parse")] pub use parser::DeInteger; #[cfg(feature = "parse")] pub use parser::DeString; #[cfg(feature = "parse")] pub use parser::DeTable; #[cfg(feature = "parse")] pub use parser::DeValue; pub use error::Error; use crate::alloc_prelude::*; /// Deserializes a string into a type. /// /// This function will attempt to interpret `s` as a TOML document and /// deserialize `T` from the document. /// /// To deserializes TOML values, instead of documents, see [`ValueDeserializer`]. /// /// # Examples /// /// ``` /// use serde::Deserialize; /// /// #[derive(Deserialize)] /// struct Config { /// title: String, /// owner: Owner, /// } /// /// #[derive(Deserialize)] /// struct Owner { /// name: String, /// } /// /// let config: Config = toml::from_str(r#" /// title = 'TOML Example' /// /// [owner] /// name = 'Lisa' /// "#).unwrap(); /// /// assert_eq!(config.title, "TOML Example"); /// assert_eq!(config.owner.name, "Lisa"); /// ``` #[cfg(feature = "parse")] #[cfg(feature = "serde")] pub fn from_str<'de, T>(s: &'de str) -> Result where T: serde_core::de::Deserialize<'de>, { T::deserialize(Deserializer::parse(s)?) } /// Deserializes bytes into a type. /// /// This function will attempt to interpret `s` as a TOML document and /// deserialize `T` from the document. /// /// To deserializes TOML values, instead of documents, see [`ValueDeserializer`]. #[cfg(feature = "parse")] #[cfg(feature = "serde")] pub fn from_slice<'de, T>(s: &'de [u8]) -> Result where T: serde_core::de::Deserialize<'de>, { let s = core::str::from_utf8(s).map_err(|e| Error::custom(e.to_string(), None))?; from_str(s) } toml-0.9.8/src/de/parser/array.rs000064400000000000000000000077601046102023000147660ustar 00000000000000use serde_spanned::Spanned; use crate::de::parser::inline_table::on_inline_table; use crate::de::parser::value::on_scalar; use crate::de::{DeArray, DeValue}; use crate::de::parser::prelude::*; /// ```bnf /// ;; Array /// /// array = array-open array-values array-close /// array-values = ws-comment-newline val ws-comment-newline array-sep array-values /// array-values =/ ws-comment-newline val ws-comment-newline [ array-sep ] /// ``` pub(crate) fn on_array<'i>( open_event: &toml_parser::parser::Event, input: &mut Input<'_>, source: toml_parser::Source<'i>, errors: &mut dyn ErrorSink, ) -> Spanned> { #[cfg(feature = "debug")] let _scope = TraceScope::new("array::on_array"); let mut result = DeArray::new(); let mut close_span = open_event.span(); let mut state = State::default(); state.open(open_event); while let Some(event) = input.next_token() { close_span = event.span(); match event.kind() { EventKind::StdTableOpen | EventKind::ArrayTableOpen | EventKind::InlineTableClose | EventKind::SimpleKey | EventKind::KeySep | EventKind::KeyValSep | EventKind::StdTableClose | EventKind::ArrayTableClose => { #[cfg(feature = "debug")] trace( &format!("unexpected {event:?}"), anstyle::AnsiColor::Red.on_default(), ); break; } EventKind::Error => { #[cfg(feature = "debug")] trace( &format!("unexpected {event:?}"), anstyle::AnsiColor::Red.on_default(), ); continue; } EventKind::InlineTableOpen => { let value = on_inline_table(event, input, source, errors); state.capture_value(event, value); } EventKind::ArrayOpen => { let value = on_array(event, input, source, errors); state.capture_value(event, value); } EventKind::Scalar => { let value = on_scalar(event, source, errors); state.capture_value(event, value); } EventKind::ValueSep => { state.finish_value(event, &mut result); state.sep_value(event); } EventKind::Whitespace | EventKind::Comment | EventKind::Newline => { state.whitespace(event); } EventKind::ArrayClose => { state.finish_value(event, &mut result); state.close(open_event, event, &mut result); break; } } } let span = open_event.span().start()..close_span.end(); Spanned::new(span, DeValue::Array(result)) } #[derive(Default)] struct State<'i> { current_value: Option>>, trailing_start: Option, } impl<'i> State<'i> { fn open(&mut self, _open_event: &toml_parser::parser::Event) {} fn whitespace(&mut self, _event: &toml_parser::parser::Event) {} fn capture_value(&mut self, _event: &toml_parser::parser::Event, value: Spanned>) { self.trailing_start = None; self.current_value = Some(value); } fn finish_value(&mut self, _event: &toml_parser::parser::Event, result: &mut DeArray<'i>) { #[cfg(feature = "debug")] let _scope = TraceScope::new("array::finish_value"); if let Some(value) = self.current_value.take() { result.push(value); } } fn sep_value(&mut self, event: &toml_parser::parser::Event) { self.trailing_start = Some(event.span().end()); } fn close( &mut self, _open_event: &toml_parser::parser::Event, _close_event: &toml_parser::parser::Event, _result: &mut DeArray<'i>, ) { #[cfg(feature = "debug")] let _scope = TraceScope::new("array::close"); } } toml-0.9.8/src/de/parser/dearray.rs000064400000000000000000000064141046102023000152720ustar 00000000000000use serde_spanned::Spanned; use crate::alloc_prelude::*; use crate::de::DeValue; /// Type representing a TOML array, payload of the `DeValue::Array` variant #[derive(Clone)] pub struct DeArray<'i> { items: Vec>>, array_of_tables: bool, } impl<'i> DeArray<'i> { /// Constructs a new, empty `DeArray`. /// /// This will not allocate until elements are pushed onto it. pub const fn new() -> Self { Self { items: Vec::new(), array_of_tables: false, } } /// Appends an element to the back of a collection. /// /// # Panics /// /// Panics if the new capacity exceeds `isize::MAX` _bytes_. pub fn push(&mut self, value: Spanned>) { self.items.push(value); } } impl DeArray<'_> { pub(crate) fn is_array_of_tables(&self) -> bool { self.array_of_tables } pub(crate) fn set_array_of_tables(&mut self, yes: bool) { self.array_of_tables = yes; } } impl<'i> core::ops::Deref for DeArray<'i> { type Target = [Spanned>]; #[inline] fn deref(&self) -> &[Spanned>] { self.items.as_slice() } } impl<'i> core::ops::DerefMut for DeArray<'i> { #[inline] fn deref_mut(&mut self) -> &mut [Spanned>] { self.items.as_mut_slice() } } impl<'i> AsRef<[Spanned>]> for DeArray<'i> { fn as_ref(&self) -> &[Spanned>] { &self.items } } impl<'i> AsMut<[Spanned>]> for DeArray<'i> { fn as_mut(&mut self) -> &mut [Spanned>] { &mut self.items } } impl<'i> core::borrow::Borrow<[Spanned>]> for DeArray<'i> { fn borrow(&self) -> &[Spanned>] { &self.items[..] } } impl<'i> core::borrow::BorrowMut<[Spanned>]> for DeArray<'i> { fn borrow_mut(&mut self) -> &mut [Spanned>] { &mut self.items[..] } } impl<'i, I: core::slice::SliceIndex<[Spanned>]>> core::ops::Index for DeArray<'i> { type Output = I::Output; #[inline] fn index(&self, index: I) -> &Self::Output { self.items.index(index) } } impl<'a, 'i> IntoIterator for &'a DeArray<'i> { type Item = &'a Spanned>; type IntoIter = core::slice::Iter<'a, Spanned>>; fn into_iter(self) -> Self::IntoIter { self.iter() } } impl<'i> IntoIterator for DeArray<'i> { type Item = Spanned>; type IntoIter = alloc::vec::IntoIter>>; #[inline] fn into_iter(self) -> Self::IntoIter { self.items.into_iter() } } impl<'i> FromIterator>> for DeArray<'i> { #[inline] #[track_caller] fn from_iter>>>(iter: I) -> Self { Self { items: iter.into_iter().collect(), array_of_tables: false, } } } impl Default for DeArray<'static> { #[inline] fn default() -> Self { Self { items: Default::default(), array_of_tables: false, } } } impl core::fmt::Debug for DeArray<'_> { #[inline] fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.items.fmt(formatter) } } toml-0.9.8/src/de/parser/debug.rs000064400000000000000000000043341046102023000147300ustar 00000000000000pub(crate) struct TraceScope { text: String, style: anstyle::Style, guard: DebugDepthGuard, } impl TraceScope { pub(crate) fn new(text: impl core::fmt::Display) -> Self { let text = text.to_string(); let style = anstyle::Style::new(); trace(&format!("> {text}"), style); Self { text, style, guard: DEBUG_DEPTH.scoped(), } } } impl Drop for TraceScope { fn drop(&mut self) { let text = &self.text; let style = self.style; drop(self.guard.take()); trace(&format!("< {text}"), style); } } pub(crate) fn trace(text: &str, style: anstyle::Style) { #![allow(unexpected_cfgs)] // HACK: fixed in newer versions let depth = DEBUG_DEPTH.depth(); anstream::eprintln!("{:depth$}{style}{text}{style:#}", ""); } pub(crate) struct DebugDepth(core::sync::atomic::AtomicUsize); impl DebugDepth { pub(crate) fn scoped(&self) -> DebugDepthGuard { DebugDepthGuard::new() } pub(crate) fn enter_unchecked(&self) -> usize { self.0.fetch_add(1, core::sync::atomic::Ordering::SeqCst) } pub(crate) fn exit_unchecked(&self) { let _ = self.0.fetch_sub(1, core::sync::atomic::Ordering::SeqCst); } pub(crate) fn depth(&self) -> usize { self.0.load(core::sync::atomic::Ordering::SeqCst) } } static DEBUG_DEPTH: DebugDepth = DebugDepth(core::sync::atomic::AtomicUsize::new(0)); pub(crate) struct DebugDepthGuard { depth: usize, inc: bool, } impl DebugDepthGuard { pub(crate) fn new() -> Self { let depth = DEBUG_DEPTH.enter_unchecked(); Self { depth, inc: true } } fn take(&mut self) -> Self { let depth = self.depth; let inc = self.inc; self.inc = false; Self { depth, inc } } } impl Drop for DebugDepthGuard { fn drop(&mut self) { if self.inc { DEBUG_DEPTH.exit_unchecked(); } } } impl AsRef for DebugDepthGuard { #[inline(always)] fn as_ref(&self) -> &usize { &self.depth } } impl core::ops::Deref for DebugDepthGuard { type Target = usize; #[inline(always)] fn deref(&self) -> &Self::Target { &self.depth } } toml-0.9.8/src/de/parser/detable.rs000064400000000000000000000032511046102023000152370ustar 00000000000000use alloc::borrow::Cow; use serde_spanned::Spanned; use crate::alloc_prelude::*; use crate::de::DeString; use crate::de::DeValue; use crate::map::Map; /// Type representing a TOML table, payload of the `Value::Table` variant. /// /// By default it entries are stored in /// [lexicographic order](https://doc.rust-lang.org/std/primitive.str.html#impl-Ord-for-str) /// of the keys. Enable the `preserve_order` feature to store entries in the order they appear in /// the source file. pub type DeTable<'i> = Map>, Spanned>>; impl<'i> DeTable<'i> { /// Parse a TOML document pub fn parse(input: &'i str) -> Result, crate::de::Error> { let source = toml_parser::Source::new(input); let mut errors = crate::de::error::TomlSink::>::new(source); let value = crate::de::parser::parse_document(source, &mut errors); if let Some(err) = errors.into_inner() { Err(err) } else { Ok(value) } } /// Parse a TOML document, with best effort recovery on error pub fn parse_recoverable(input: &'i str) -> (Spanned, Vec) { let source = toml_parser::Source::new(input); let mut errors = crate::de::error::TomlSink::>::new(source); let value = crate::de::parser::parse_document(source, &mut errors); (value, errors.into_inner()) } /// Ensure no data is borrowed pub fn make_owned(&mut self) { self.mut_entries(|k, v| { let owned = core::mem::take(k.get_mut()); *k.get_mut() = Cow::Owned(owned.into_owned()); v.get_mut().make_owned(); }); } } toml-0.9.8/src/de/parser/devalue.rs000064400000000000000000000247731046102023000153000ustar 00000000000000//! Definition of a TOML [value][DeValue] for deserialization use alloc::borrow::Cow; use core::mem::discriminant; use core::ops; use serde_spanned::Spanned; use toml_datetime::Datetime; use crate::alloc_prelude::*; use crate::de::DeArray; use crate::de::DeTable; /// Type representing a TOML string, payload of the `DeValue::String` variant pub type DeString<'i> = Cow<'i, str>; /// Represents a TOML integer #[derive(Clone, Debug)] pub struct DeInteger<'i> { pub(crate) inner: DeString<'i>, pub(crate) radix: u32, } impl DeInteger<'_> { pub(crate) fn to_u64(&self) -> Option { u64::from_str_radix(self.inner.as_ref(), self.radix).ok() } pub(crate) fn to_i64(&self) -> Option { i64::from_str_radix(self.inner.as_ref(), self.radix).ok() } pub(crate) fn to_u128(&self) -> Option { u128::from_str_radix(self.inner.as_ref(), self.radix).ok() } pub(crate) fn to_i128(&self) -> Option { i128::from_str_radix(self.inner.as_ref(), self.radix).ok() } /// [`from_str_radix`][i64::from_str_radix]-compatible representation of an integer /// /// Requires [`DeInteger::radix`] to interpret /// /// See [`Display`][std::fmt::Display] for a representation that includes the radix pub fn as_str(&self) -> &str { self.inner.as_ref() } /// Numeric base of [`DeInteger::as_str`] pub fn radix(&self) -> u32 { self.radix } } impl Default for DeInteger<'_> { fn default() -> Self { Self { inner: DeString::Borrowed("0"), radix: 10, } } } impl core::fmt::Display for DeInteger<'_> { fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self.radix { 2 => "0b".fmt(formatter)?, 8 => "0o".fmt(formatter)?, 10 => {} 16 => "0x".fmt(formatter)?, _ => { unreachable!( "we should only ever have 2, 8, 10, and 16 radix, not {}", self.radix ) } } self.as_str().fmt(formatter)?; Ok(()) } } /// Represents a TOML integer #[derive(Clone, Debug)] pub struct DeFloat<'i> { pub(crate) inner: DeString<'i>, } impl DeFloat<'_> { pub(crate) fn to_f64(&self) -> Option { let f: f64 = self.inner.as_ref().parse().ok()?; if f.is_infinite() && !self.as_str().contains("inf") { None } else { Some(f) } } /// [`FromStr`][std::str::FromStr]-compatible representation of a float pub fn as_str(&self) -> &str { self.inner.as_ref() } } impl Default for DeFloat<'_> { fn default() -> Self { Self { inner: DeString::Borrowed("0.0"), } } } impl core::fmt::Display for DeFloat<'_> { fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.as_str().fmt(formatter)?; Ok(()) } } /// Representation of a TOML value. #[derive(Clone, Debug)] pub enum DeValue<'i> { /// Represents a TOML string String(DeString<'i>), /// Represents a TOML integer Integer(DeInteger<'i>), /// Represents a TOML float Float(DeFloat<'i>), /// Represents a TOML boolean Boolean(bool), /// Represents a TOML datetime Datetime(Datetime), /// Represents a TOML array Array(DeArray<'i>), /// Represents a TOML table Table(DeTable<'i>), } impl<'i> DeValue<'i> { /// Parse a TOML value pub fn parse(input: &'i str) -> Result, crate::de::Error> { let source = toml_parser::Source::new(input); let mut errors = crate::de::error::TomlSink::>::new(source); let value = crate::de::parser::parse_value(source, &mut errors); if let Some(err) = errors.into_inner() { Err(err) } else { Ok(value) } } /// Parse a TOML value, with best effort recovery on error pub fn parse_recoverable(input: &'i str) -> (Spanned, Vec) { let source = toml_parser::Source::new(input); let mut errors = crate::de::error::TomlSink::>::new(source); let value = crate::de::parser::parse_value(source, &mut errors); (value, errors.into_inner()) } /// Ensure no data is borrowed pub fn make_owned(&mut self) { match self { DeValue::String(v) => { let owned = core::mem::take(v); *v = Cow::Owned(owned.into_owned()); } DeValue::Integer(..) | DeValue::Float(..) | DeValue::Boolean(..) | DeValue::Datetime(..) => {} DeValue::Array(v) => { for e in v.iter_mut() { e.get_mut().make_owned(); } } DeValue::Table(v) => v.make_owned(), } } /// Index into a TOML array or map. A string index can be used to access a /// value in a map, and a usize index can be used to access an element of an /// array. /// /// Returns `None` if the type of `self` does not match the type of the /// index, for example if the index is a string and `self` is an array or a /// number. Also returns `None` if the given key does not exist in the map /// or the given index is not within the bounds of the array. pub fn get(&self, index: I) -> Option<&Spanned> { index.index(self) } /// Extracts the integer value if it is an integer. pub fn as_integer(&self) -> Option<&DeInteger<'i>> { match self { DeValue::Integer(i) => Some(i), _ => None, } } /// Tests whether this value is an integer. pub fn is_integer(&self) -> bool { self.as_integer().is_some() } /// Extracts the float value if it is a float. pub fn as_float(&self) -> Option<&DeFloat<'i>> { match self { DeValue::Float(f) => Some(f), _ => None, } } /// Tests whether this value is a float. pub fn is_float(&self) -> bool { self.as_float().is_some() } /// Extracts the boolean value if it is a boolean. pub fn as_bool(&self) -> Option { match *self { DeValue::Boolean(b) => Some(b), _ => None, } } /// Tests whether this value is a boolean. pub fn is_bool(&self) -> bool { self.as_bool().is_some() } /// Extracts the string of this value if it is a string. pub fn as_str(&self) -> Option<&str> { match *self { DeValue::String(ref s) => Some(&**s), _ => None, } } /// Tests if this value is a string. pub fn is_str(&self) -> bool { self.as_str().is_some() } /// Extracts the datetime value if it is a datetime. /// /// Note that a parsed TOML value will only contain ISO 8601 dates. An /// example date is: /// /// ```notrust /// 1979-05-27T07:32:00Z /// ``` pub fn as_datetime(&self) -> Option<&Datetime> { match *self { DeValue::Datetime(ref s) => Some(s), _ => None, } } /// Tests whether this value is a datetime. pub fn is_datetime(&self) -> bool { self.as_datetime().is_some() } /// Extracts the array value if it is an array. pub fn as_array(&self) -> Option<&DeArray<'i>> { match *self { DeValue::Array(ref s) => Some(s), _ => None, } } pub(crate) fn as_array_mut(&mut self) -> Option<&mut DeArray<'i>> { match self { DeValue::Array(s) => Some(s), _ => None, } } /// Tests whether this value is an array. pub fn is_array(&self) -> bool { self.as_array().is_some() } /// Extracts the table value if it is a table. pub fn as_table(&self) -> Option<&DeTable<'i>> { match *self { DeValue::Table(ref s) => Some(s), _ => None, } } pub(crate) fn as_table_mut(&mut self) -> Option<&mut DeTable<'i>> { match self { DeValue::Table(s) => Some(s), _ => None, } } /// Tests whether this value is a table. pub fn is_table(&self) -> bool { self.as_table().is_some() } /// Tests whether this and another value have the same type. pub fn same_type(&self, other: &DeValue<'_>) -> bool { discriminant(self) == discriminant(other) } /// Returns a human-readable representation of the type of this value. pub fn type_str(&self) -> &'static str { match *self { DeValue::String(..) => "string", DeValue::Integer(..) => "integer", DeValue::Float(..) => "float", DeValue::Boolean(..) => "boolean", DeValue::Datetime(..) => "datetime", DeValue::Array(..) => "array", DeValue::Table(..) => "table", } } } impl ops::Index for DeValue<'_> where I: Index, { type Output = Spanned; fn index(&self, index: I) -> &Spanned { self.get(index).expect("index not found") } } /// Types that can be used to index a `toml::Value` /// /// Currently this is implemented for `usize` to index arrays and `str` to index /// tables. /// /// This trait is sealed and not intended for implementation outside of the /// `toml` crate. pub trait Index: Sealed { #[doc(hidden)] fn index<'r, 'i>(&self, val: &'r DeValue<'i>) -> Option<&'r Spanned>>; } /// An implementation detail that should not be implemented, this will change in /// the future and break code otherwise. #[doc(hidden)] pub trait Sealed {} impl Sealed for usize {} impl Sealed for str {} impl Sealed for String {} impl Sealed for &T {} impl Index for usize { fn index<'r, 'i>(&self, val: &'r DeValue<'i>) -> Option<&'r Spanned>> { match *val { DeValue::Array(ref a) => a.get(*self), _ => None, } } } impl Index for str { fn index<'r, 'i>(&self, val: &'r DeValue<'i>) -> Option<&'r Spanned>> { match *val { DeValue::Table(ref a) => a.get(self), _ => None, } } } impl Index for String { fn index<'r, 'i>(&self, val: &'r DeValue<'i>) -> Option<&'r Spanned>> { self[..].index(val) } } impl Index for &T where T: Index + ?Sized, { fn index<'r, 'i>(&self, val: &'r DeValue<'i>) -> Option<&'r Spanned>> { (**self).index(val) } } toml-0.9.8/src/de/parser/document.rs000064400000000000000000000417531046102023000154660ustar 00000000000000use serde_spanned::Spanned; use crate::alloc_prelude::*; use crate::de::parser::key::on_key; use crate::de::parser::prelude::*; use crate::de::parser::value::value; use crate::de::DeString; use crate::de::DeValue; use crate::de::{DeArray, DeTable}; use crate::map::Entry; /// ```bnf /// ;; TOML /// /// toml = expression *( newline expression ) /// /// expression = ( ( ws comment ) / /// ( ws keyval ws [ comment ] ) / /// ( ws table ws [ comment ] ) / /// ws ) /// ``` pub(crate) fn document<'i>( input: &mut Input<'_>, source: toml_parser::Source<'i>, errors: &mut dyn ErrorSink, ) -> Spanned> { #[cfg(feature = "debug")] let _scope = TraceScope::new("document::document"); let mut state = State::default(); while let Some(event) = input.next_token() { match event.kind() { EventKind::InlineTableOpen | EventKind::InlineTableClose | EventKind::ArrayOpen | EventKind::ArrayClose | EventKind::Scalar | EventKind::ValueSep | EventKind::Error | EventKind::KeySep | EventKind::KeyValSep | EventKind::StdTableClose | EventKind::ArrayTableClose => { #[cfg(feature = "debug")] trace( &format!("unexpected {event:?}"), anstyle::AnsiColor::Red.on_default(), ); continue; } EventKind::StdTableOpen | EventKind::ArrayTableOpen => { state.finish_table(errors); let header = on_table(event, input, source, errors); state.start_table(header, errors); } EventKind::SimpleKey => { let (path, key) = on_key(event, input, source, errors); let Some(key) = key else { break; }; let Some(next_event) = input.next_token() else { break; }; let keyval_event = if next_event.kind() == EventKind::Whitespace { let Some(next_event) = input.next_token() else { break; }; next_event } else { next_event }; if keyval_event.kind() != EventKind::KeyValSep { break; } if input .first() .map(|e| e.kind() == EventKind::Whitespace) .unwrap_or(false) { let _ = input.next_token(); } let value = value(input, source, errors); state.capture_key_value(path, key, value, errors); } EventKind::Whitespace | EventKind::Comment | EventKind::Newline => { state.capture_trailing(event); } } } state.finish_table(errors); let span = Default::default(); Spanned::new(span, state.root) } /// ```bnf /// ;; Standard Table /// /// std-table = std-table-open key *( table-key-sep key) std-table-close /// /// ;; Array Table /// /// array-table = array-table-open key *( table-key-sep key) array-table-close /// ``` fn on_table<'i>( open_event: &toml_parser::parser::Event, input: &mut Input<'_>, source: toml_parser::Source<'i>, errors: &mut dyn ErrorSink, ) -> TableHeader<'i> { #[cfg(feature = "debug")] let _scope = TraceScope::new("document::on_table"); let is_array = open_event.kind() == EventKind::ArrayTableOpen; let mut current_path = None; let mut current_key = None; let mut current_span = open_event.span(); let mut current_prefix = None; let mut current_suffix = None; while let Some(event) = input.next_token() { match event.kind() { EventKind::InlineTableOpen | EventKind::InlineTableClose | EventKind::ArrayOpen | EventKind::ArrayClose | EventKind::Scalar | EventKind::ValueSep | EventKind::Error | EventKind::KeySep | EventKind::KeyValSep | EventKind::StdTableOpen | EventKind::ArrayTableOpen | EventKind::Comment | EventKind::Newline => { #[cfg(feature = "debug")] trace( &format!("unexpected {event:?}"), anstyle::AnsiColor::Red.on_default(), ); continue; } EventKind::ArrayTableClose | EventKind::StdTableClose => { current_span = current_span.append(event.span()); break; } EventKind::SimpleKey => { current_prefix.get_or_insert_with(|| event.span().before()); let (path, key) = on_key(event, input, source, errors); current_path = Some(path); current_key = key; current_suffix.get_or_insert_with(|| event.span().after()); } EventKind::Whitespace => { if current_key.is_some() { current_suffix = Some(event.span()); } else { current_prefix = Some(event.span()); } } } } TableHeader { path: current_path.unwrap_or_default(), key: current_key, span: current_span, is_array, } } struct TableHeader<'i> { path: Vec>>, key: Option>>, span: toml_parser::Span, is_array: bool, } #[derive(Default)] struct State<'i> { root: DeTable<'i>, current_table: DeTable<'i>, current_header: Option>, current_position: usize, } impl<'i> State<'i> { fn capture_trailing(&mut self, _event: &toml_parser::parser::Event) {} fn capture_key_value( &mut self, path: Vec>>, key: Spanned>, value: Spanned>, errors: &mut dyn ErrorSink, ) { #[cfg(feature = "debug")] let _scope = TraceScope::new("document::capture_key_value"); #[cfg(feature = "debug")] trace( &format!( "path={:?}", path.iter().map(|k| k.get_ref()).collect::>() ), anstyle::AnsiColor::Blue.on_default(), ); #[cfg(feature = "debug")] trace( &format!("key={key}",), anstyle::AnsiColor::Blue.on_default(), ); #[cfg(feature = "debug")] trace( &format!("value={value:?}",), anstyle::AnsiColor::Blue.on_default(), ); let dotted = true; let Some(parent_table) = descend_path(&mut self.current_table, &path, dotted, errors) else { return; }; // "Likewise, using dotted keys to redefine tables already defined in [table] form is not allowed" let mixed_table_types = parent_table.is_dotted() == path.is_empty(); if mixed_table_types { let key_span = get_key_span(&key); errors.report_error(ParseError::new("duplicate key").with_unexpected(key_span)); return; } let key_span = get_key_span(&key); match parent_table.entry(key) { Entry::Vacant(o) => { o.insert(value); } Entry::Occupied(existing) => { // "Since tables cannot be defined more than once, redefining such tables using a [table] header is not allowed" let old_span = get_key_span(existing.key()); errors.report_error( ParseError::new("duplicate key") .with_unexpected(key_span) .with_context(old_span), ); } } } fn finish_table(&mut self, errors: &mut dyn ErrorSink) { #[cfg(feature = "debug")] let _scope = TraceScope::new("document::finish_table"); let prev_table = core::mem::take(&mut self.current_table); if let Some(header) = self.current_header.take() { let Some(key) = &header.key else { return; }; let header_span = header.span.start()..header.span.end(); let prev_table = Spanned::new(header_span.clone(), DeValue::Table(prev_table)); let parent_key = &header.path; let dotted = false; let Some(parent_table) = descend_path(&mut self.root, parent_key, dotted, errors) else { return; }; #[cfg(feature = "debug")] trace( &format!("key={key}",), anstyle::AnsiColor::Blue.on_default(), ); if header.is_array { let entry = parent_table.entry(key.clone()).or_insert_with(|| { let mut array = DeArray::new(); array.set_array_of_tables(true); Spanned::new(header_span, DeValue::Array(array)) }); let Some(array) = entry .as_mut() .as_array_mut() .filter(|a| a.is_array_of_tables()) else { let key_span = get_key_span(key); let old_span = entry.span(); let old_span = toml_parser::Span::new_unchecked(old_span.start, old_span.end); errors.report_error( ParseError::new("duplicate key") .with_unexpected(key_span) .with_context(old_span), ); return; }; array.push(prev_table); } else { let existing = parent_table.insert(key.clone(), prev_table); debug_assert!(existing.is_none()); } } else { self.root = prev_table; } } fn start_table(&mut self, header: TableHeader<'i>, errors: &mut dyn ErrorSink) { if !header.is_array { // 1. Look up the table on start to ensure the duplicate_key error points to the right line // 2. Ensure any child tables from an implicit table are preserved let root = &mut self.root; if let (Some(parent_table), Some(key)) = (descend_path(root, &header.path, false, errors), &header.key) { if let Some((old_key, old_value)) = parent_table.remove_entry(key) { match old_value.into_inner() { DeValue::Table(t) if t.is_implicit() && !t.is_dotted() => { self.current_table = t; } // Since tables cannot be defined more than once, redefining such tables using a [table] header is not allowed. Likewise, using dotted keys to redefine tables already defined in [table] form is not allowed. old_value => { let old_span = get_key_span(&old_key); let key_span = get_key_span(key); errors.report_error( ParseError::new("duplicate key") .with_unexpected(key_span) .with_context(old_span), ); if let DeValue::Table(t) = old_value { self.current_table = t; } } } } } } self.current_position += 1; self.current_table.set_implicit(false); self.current_table.set_dotted(false); self.current_header = Some(header); } } fn descend_path<'t, 'i>( mut table: &'t mut DeTable<'i>, path: &[Spanned>], dotted: bool, errors: &mut dyn ErrorSink, ) -> Option<&'t mut DeTable<'i>> { #[cfg(feature = "debug")] let _scope = TraceScope::new("document::descend_path"); #[cfg(feature = "debug")] trace( &format!( "path={:?}", path.iter().map(|k| k.get_ref()).collect::>() ), anstyle::AnsiColor::Blue.on_default(), ); for key in path.iter() { table = match table.entry(key.clone()) { Entry::Vacant(entry) => { let mut new_table = DeTable::new(); new_table.set_implicit(true); new_table.set_dotted(dotted); let value = DeValue::Table(new_table); let value = Spanned::new(key.span(), value); let value = entry.insert(value); value.as_mut().as_table_mut().unwrap() } Entry::Occupied(entry) => { let spanned = entry.into_mut(); let old_span = spanned.span(); match spanned.as_mut() { DeValue::Array(ref mut array) => { if !array.is_array_of_tables() { let old_span = toml_parser::Span::new_unchecked(old_span.start, old_span.end); let key_span = get_key_span(key); errors.report_error( ParseError::new( "cannot extend value of type array with a dotted key", ) .with_unexpected(key_span) .with_context(old_span), ); return None; } debug_assert!(!array.is_empty()); let index = array.len() - 1; let last_child = array.get_mut(index).unwrap(); match last_child.as_mut() { DeValue::Table(table) => table, existing => { let old_span = toml_parser::Span::new_unchecked(old_span.start, old_span.end); let key_span = get_key_span(key); errors.report_error( ParseError::new(format!( "cannot extend value of type {} with a dotted key", existing.type_str() )) .with_unexpected(key_span) .with_context(old_span), ); return None; } } } DeValue::Table(ref mut sweet_child_of_mine) => { if sweet_child_of_mine.is_inline() { let key_span = get_key_span(key); errors.report_error( ParseError::new( "cannot extend value of type inline table with a dotted key", ) .with_unexpected(key_span), ); return None; } // Since tables cannot be defined more than once, redefining such tables using a // [table] header is not allowed. Likewise, using dotted keys to redefine tables // already defined in [table] form is not allowed. if dotted && !sweet_child_of_mine.is_implicit() { let key_span = get_key_span(key); errors.report_error( ParseError::new("duplicate key").with_unexpected(key_span), ); return None; } sweet_child_of_mine } existing => { let old_span = toml_parser::Span::new_unchecked(old_span.start, old_span.end); let key_span = get_key_span(key); errors.report_error( ParseError::new(format!( "cannot extend value of type {} with a dotted key", existing.type_str() )) .with_unexpected(key_span) .with_context(old_span), ); return None; } } } }; } Some(table) } fn get_key_span(key: &Spanned>) -> toml_parser::Span { let key_span = key.span(); toml_parser::Span::new_unchecked(key_span.start, key_span.end) } toml-0.9.8/src/de/parser/inline_table.rs000064400000000000000000000203711046102023000162660ustar 00000000000000use serde_spanned::Spanned; use crate::alloc_prelude::*; use crate::de::parser::array::on_array; use crate::de::parser::key::on_key; use crate::de::parser::prelude::*; use crate::de::parser::value::on_scalar; use crate::de::DeString; use crate::de::DeTable; use crate::de::DeValue; use crate::map::Entry; /// ```bnf /// ;; Inline Table /// /// inline-table = inline-table-open inline-table-keyvals inline-table-close /// ``` pub(crate) fn on_inline_table<'i>( open_event: &toml_parser::parser::Event, input: &mut Input<'_>, source: toml_parser::Source<'i>, errors: &mut dyn ErrorSink, ) -> Spanned> { #[cfg(feature = "debug")] let _scope = TraceScope::new("inline_table::on_inline_table"); let mut result = DeTable::new(); result.set_inline(true); let mut close_span = open_event.span(); let mut state = State::default(); while let Some(event) = input.next_token() { close_span = event.span(); match event.kind() { EventKind::StdTableOpen | EventKind::ArrayTableOpen | EventKind::StdTableClose | EventKind::ArrayClose | EventKind::ArrayTableClose | EventKind::KeySep => { #[cfg(feature = "debug")] trace( &format!("unexpected {event:?}"), anstyle::AnsiColor::Red.on_default(), ); break; } EventKind::Error => { #[cfg(feature = "debug")] trace( &format!("unexpected {event:?}"), anstyle::AnsiColor::Red.on_default(), ); continue; } EventKind::SimpleKey => { let (path, key) = on_key(event, input, source, errors); state.capture_key(event, path, key); } EventKind::KeyValSep => { state.finish_key(event); } EventKind::InlineTableOpen => { let value = on_inline_table(event, input, source, errors); state.capture_value(event, value); } EventKind::ArrayOpen => { let value = on_array(event, input, source, errors); state.capture_value(event, value); } EventKind::Scalar => { let value = on_scalar(event, source, errors); state.capture_value(event, value); } EventKind::ValueSep => { state.finish_value(event, &mut result, errors); } EventKind::Whitespace | EventKind::Comment | EventKind::Newline => { state.whitespace(event); } EventKind::InlineTableClose => { state.finish_value(event, &mut result, errors); state.close(open_event, event, &mut result); break; } } } let span = open_event.span().start()..close_span.end(); Spanned::new(span, DeValue::Table(result)) } #[derive(Default)] struct State<'i> { current_key: Option<(Vec>>, Spanned>)>, seen_keyval_sep: bool, current_value: Option>>, } impl<'i> State<'i> { fn whitespace(&mut self, _event: &toml_parser::parser::Event) {} fn capture_key( &mut self, _event: &toml_parser::parser::Event, path: Vec>>, key: Option>>, ) { if let Some(key) = key { self.current_key = Some((path, key)); } } fn finish_key(&mut self, _event: &toml_parser::parser::Event) { self.seen_keyval_sep = true; } fn capture_value(&mut self, _event: &toml_parser::parser::Event, value: Spanned>) { self.current_value = Some(value); } fn finish_value( &mut self, _event: &toml_parser::parser::Event, result: &mut DeTable<'i>, errors: &mut dyn ErrorSink, ) { #[cfg(feature = "debug")] let _scope = TraceScope::new("inline_table::finish_value"); self.seen_keyval_sep = false; if let (Some((path, key)), Some(value)) = (self.current_key.take(), self.current_value.take()) { let Some(table) = descend_path(result, &path, true, errors) else { return; }; // "Likewise, using dotted keys to redefine tables already defined in [table] form is not allowed" let mixed_table_types = table.is_dotted() == path.is_empty(); if mixed_table_types { let key_span = get_key_span(&key); errors.report_error(ParseError::new("duplicate key").with_unexpected(key_span)); } else { let key_span = get_key_span(&key); match table.entry(key) { Entry::Vacant(o) => { o.insert(value); } Entry::Occupied(o) => { let old_span = get_key_span(o.key()); errors.report_error( ParseError::new("duplicate key") .with_unexpected(key_span) .with_context(old_span), ); } } } } } fn close( &mut self, _open_event: &toml_parser::parser::Event, _close_event: &toml_parser::parser::Event, _result: &mut DeTable<'i>, ) { #[cfg(feature = "debug")] let _scope = TraceScope::new("inline_table::close"); } } fn descend_path<'a, 'i>( mut table: &'a mut DeTable<'i>, path: &'a [Spanned>], dotted: bool, errors: &mut dyn ErrorSink, ) -> Option<&'a mut DeTable<'i>> { #[cfg(feature = "debug")] let _scope = TraceScope::new("inline_table::descend_path"); #[cfg(feature = "debug")] trace( &format!( "key={:?}", path.iter().map(|k| k.get_ref()).collect::>() ), anstyle::AnsiColor::Blue.on_default(), ); for key in path.iter() { table = match table.entry(key.clone()) { Entry::Vacant(entry) => { let mut new_table = DeTable::new(); new_table.set_implicit(true); new_table.set_dotted(dotted); new_table.set_inline(true); let value = DeValue::Table(new_table); let value = Spanned::new(key.span(), value); let value = entry.insert(value); value.as_mut().as_table_mut().unwrap() } Entry::Occupied(entry) => { let spanned = entry.into_mut(); match spanned.as_mut() { DeValue::Table(ref mut sweet_child_of_mine) => { // Since tables cannot be defined more than once, redefining such tables using a // [table] header is not allowed. Likewise, using dotted keys to redefine tables // already defined in [table] form is not allowed. if dotted && !sweet_child_of_mine.is_implicit() { let key_span = get_key_span(key); errors.report_error( ParseError::new("duplicate key").with_unexpected(key_span), ); return None; } sweet_child_of_mine } item => { let key_span = get_key_span(key); errors.report_error( ParseError::new(format!( "cannot extend value of type {} with a dotted key", item.type_str() )) .with_unexpected(key_span), ); return None; } } } }; } Some(table) } fn get_key_span(key: &Spanned>) -> toml_parser::Span { let key_span = key.span(); toml_parser::Span::new_unchecked(key_span.start, key_span.end) } toml-0.9.8/src/de/parser/key.rs000064400000000000000000000072401046102023000144310ustar 00000000000000use serde_spanned::Spanned; use crate::alloc_prelude::*; use crate::de::parser::prelude::*; use crate::de::DeString; /// ```bnf /// key = simple-key / dotted-key /// dotted-key = simple-key 1*( dot-sep simple-key ) /// ``` pub(crate) fn on_key<'i>( key_event: &toml_parser::parser::Event, input: &mut Input<'_>, source: toml_parser::Source<'i>, errors: &mut dyn ErrorSink, ) -> (Vec>>, Option>>) { #[cfg(feature = "debug")] let _scope = TraceScope::new("key::on_key"); let mut result_path = Vec::new(); let mut result_key = None; let mut state = State::new(key_event); if more_key(input) { while let Some(event) = input.next_token() { match event.kind() { EventKind::StdTableOpen | EventKind::ArrayTableOpen | EventKind::InlineTableOpen | EventKind::InlineTableClose | EventKind::ArrayOpen | EventKind::ArrayClose | EventKind::Scalar | EventKind::ValueSep | EventKind::Comment | EventKind::Newline | EventKind::KeyValSep | EventKind::StdTableClose | EventKind::ArrayTableClose | EventKind::Error => { #[cfg(feature = "debug")] trace( &format!("unexpected {event:?}"), anstyle::AnsiColor::Red.on_default(), ); continue; } EventKind::SimpleKey => { state.current_key = Some(*event); if !more_key(input) { break; } } EventKind::Whitespace => { state.whitespace(event); } EventKind::KeySep => { state.close_key(&mut result_path, &mut result_key, source, errors); } } } } state.close_key(&mut result_path, &mut result_key, source, errors); #[cfg(not(feature = "unbounded"))] if super::LIMIT <= result_path.len() as u32 { errors.report_error(ParseError::new("recursion limit")); return (Vec::new(), None); } (result_path, result_key) } fn more_key(input: &Input<'_>) -> bool { let first = input.get(0).map(|e| e.kind()); let second = input.get(1).map(|e| e.kind()); if first == Some(EventKind::KeySep) { true } else if first == Some(EventKind::Whitespace) && second == Some(EventKind::KeySep) { true } else { false } } struct State { current_key: Option, } impl State { fn new(key_event: &toml_parser::parser::Event) -> Self { Self { current_key: Some(*key_event), } } fn whitespace(&mut self, _event: &toml_parser::parser::Event) {} fn close_key<'i>( &mut self, result_path: &mut Vec>>, result_key: &mut Option>>, source: toml_parser::Source<'i>, errors: &mut dyn ErrorSink, ) { let Some(key) = self.current_key.take() else { return; }; let key_span = key.span(); let key_span = key_span.start()..key_span.end(); let raw = source.get(key).unwrap(); let mut decoded = alloc::borrow::Cow::Borrowed(""); raw.decode_key(&mut decoded, errors); let key = Spanned::new(key_span, decoded); if let Some(last_key) = result_key.replace(key) { result_path.push(last_key); } } } toml-0.9.8/src/de/parser/mod.rs000064400000000000000000000050601046102023000144160ustar 00000000000000#![allow(clippy::type_complexity)] use serde_spanned::Spanned; #[cfg(not(feature = "unbounded"))] use toml_parser::parser::RecursionGuard; use toml_parser::parser::ValidateWhitespace; pub use dearray::DeArray; pub use detable::DeTable; pub use devalue::DeFloat; pub use devalue::DeInteger; pub use devalue::DeString; pub use devalue::DeValue; use crate::alloc_prelude::*; pub(crate) mod array; pub(crate) mod dearray; #[cfg(feature = "debug")] pub(crate) mod debug; pub(crate) mod detable; pub(crate) mod devalue; pub(crate) mod document; pub(crate) mod inline_table; pub(crate) mod key; pub(crate) mod value; pub(crate) fn parse_document<'i>( source: toml_parser::Source<'i>, errors: &mut dyn prelude::ErrorSink, ) -> Spanned> { let tokens = source.lex().into_vec(); let mut events = Vec::with_capacity(tokens.len()); let mut receiver = ValidateWhitespace::new(&mut events, source); #[cfg(not(feature = "unbounded"))] let mut receiver = RecursionGuard::new(&mut receiver, LIMIT); #[cfg(not(feature = "unbounded"))] let receiver = &mut receiver; #[cfg(feature = "unbounded")] let receiver = &mut receiver; toml_parser::parser::parse_document(&tokens, receiver, errors); let mut input = prelude::Input::new(&events); let doc = document::document(&mut input, source, errors); doc } pub(crate) fn parse_value<'i>( source: toml_parser::Source<'i>, errors: &mut dyn prelude::ErrorSink, ) -> Spanned> { let tokens = source.lex().into_vec(); let mut events = Vec::with_capacity(tokens.len()); let mut receiver = ValidateWhitespace::new(&mut events, source); #[cfg(not(feature = "unbounded"))] let mut receiver = RecursionGuard::new(&mut receiver, LIMIT); #[cfg(not(feature = "unbounded"))] let receiver = &mut receiver; #[cfg(feature = "unbounded")] let receiver = &mut receiver; toml_parser::parser::parse_value(&tokens, receiver, errors); let mut input = prelude::Input::new(&events); let value = value::value(&mut input, source, errors); value } #[cfg(not(feature = "unbounded"))] const LIMIT: u32 = 80; pub(crate) mod prelude { pub(crate) use toml_parser::parser::EventKind; pub(crate) use toml_parser::ErrorSink; pub(crate) use toml_parser::ParseError; pub(crate) use winnow::stream::Stream as _; pub(crate) type Input<'i> = winnow::stream::TokenSlice<'i, toml_parser::parser::Event>; #[cfg(feature = "debug")] pub(crate) use super::debug::trace; #[cfg(feature = "debug")] pub(crate) use super::debug::TraceScope; } toml-0.9.8/src/de/parser/value.rs000064400000000000000000000073421046102023000147600ustar 00000000000000use serde_spanned::Spanned; use crate::alloc_prelude::*; use crate::de::parser::array::on_array; use crate::de::parser::inline_table::on_inline_table; use crate::de::parser::prelude::*; use crate::de::DeFloat; use crate::de::DeInteger; use crate::de::DeValue; /// ```bnf /// val = string / boolean / array / inline-table / date-time / float / integer /// ``` pub(crate) fn value<'i>( input: &mut Input<'_>, source: toml_parser::Source<'i>, errors: &mut dyn ErrorSink, ) -> Spanned> { #[cfg(feature = "debug")] let _scope = TraceScope::new("value"); if let Some(event) = input.next_token() { match event.kind() { EventKind::StdTableOpen | EventKind::ArrayTableOpen | EventKind::InlineTableClose | EventKind::ArrayClose | EventKind::ValueSep | EventKind::Comment | EventKind::Newline | EventKind::Error | EventKind::SimpleKey | EventKind::KeySep | EventKind::KeyValSep | EventKind::StdTableClose | EventKind::ArrayTableClose => { #[cfg(feature = "debug")] trace( &format!("unexpected {event:?}"), anstyle::AnsiColor::Red.on_default(), ); } EventKind::Whitespace => { #[cfg(feature = "debug")] trace( &format!("unexpected {event:?}"), anstyle::AnsiColor::Red.on_default(), ); } EventKind::InlineTableOpen => { return on_inline_table(event, input, source, errors); } EventKind::ArrayOpen => { return on_array(event, input, source, errors); } EventKind::Scalar => { return on_scalar(event, source, errors); } } } Spanned::new(0..0, DeValue::Integer(Default::default())) } pub(crate) fn on_scalar<'i>( event: &toml_parser::parser::Event, source: toml_parser::Source<'i>, errors: &mut dyn ErrorSink, ) -> Spanned> { #[cfg(feature = "debug")] let _scope = TraceScope::new("on_scalar"); let value_span = event.span(); let value_span = value_span.start()..value_span.end(); let raw = source.get(event).unwrap(); let mut decoded = alloc::borrow::Cow::Borrowed(""); let kind = raw.decode_scalar(&mut decoded, errors); match kind { toml_parser::decoder::ScalarKind::String => { Spanned::new(value_span, DeValue::String(decoded)) } toml_parser::decoder::ScalarKind::Boolean(value) => { Spanned::new(value_span, DeValue::Boolean(value)) } toml_parser::decoder::ScalarKind::DateTime => { let value = match decoded.parse::() { Ok(value) => value, Err(err) => { errors.report_error( ParseError::new(err.to_string()).with_unexpected(event.span()), ); toml_datetime::Datetime { date: None, time: None, offset: None, } } }; Spanned::new(value_span, DeValue::Datetime(value)) } toml_parser::decoder::ScalarKind::Float => { Spanned::new(value_span, DeValue::Float(DeFloat { inner: decoded })) } toml_parser::decoder::ScalarKind::Integer(radix) => Spanned::new( value_span, DeValue::Integer(DeInteger { inner: decoded, radix: radix.value(), }), ), } } toml-0.9.8/src/lib.rs000064400000000000000000000130241046102023000125200ustar 00000000000000//! A [serde]-compatible [TOML]-parsing library //! //! TOML itself is a simple, ergonomic, and readable configuration format: //! //! ```toml //! [package] //! name = "toml" //! //! [dependencies] //! serde = "1.0" //! ``` //! //! The TOML format tends to be relatively common throughout the Rust community //! for configuration, notably being used by [Cargo], Rust's package manager. //! //! ## TOML values //! //! A TOML document is represented with the [`Table`] type which maps `String` to the [`Value`] enum: //! #![cfg_attr(not(feature = "default"), doc = " ```ignore")] #![cfg_attr(feature = "default", doc = " ```")] //! # use toml::value::{Datetime, Array, Table}; //! pub enum Value { //! String(String), //! Integer(i64), //! Float(f64), //! Boolean(bool), //! Datetime(Datetime), //! Array(Array), //! Table(Table), //! } //! ``` //! //! ## Parsing TOML //! //! The easiest way to parse a TOML document is via the [`Table`] type: //! #![cfg_attr(not(feature = "default"), doc = " ```ignore")] #![cfg_attr(feature = "default", doc = " ```")] //! use toml::Table; //! //! let value = "foo = 'bar'".parse::().unwrap(); //! //! assert_eq!(value["foo"].as_str(), Some("bar")); //! ``` //! //! The [`Table`] type implements a number of convenience methods and //! traits; the example above uses [`FromStr`] to parse a [`str`] into a //! [`Table`]. //! //! ## Deserialization and Serialization //! //! This crate supports [`serde`] 1.0 with a number of //! implementations of the `Deserialize`, `Serialize`, `Deserializer`, and //! `Serializer` traits. Namely, you'll find: //! //! * `Deserialize for Table` //! * `Serialize for Table` //! * `Deserialize for Value` //! * `Serialize for Value` //! * `Deserialize for Datetime` //! * `Serialize for Datetime` //! * `Deserializer for de::Deserializer` //! * `Serializer for ser::Serializer` //! * `Deserializer for Table` //! * `Deserializer for Value` //! //! This means that you can use Serde to deserialize/serialize the //! [`Table`] type as well as [`Value`] and [`Datetime`] type in this crate. You can also //! use the [`Deserializer`], [`Serializer`], or [`Table`] type itself to act as //! a deserializer/serializer for arbitrary types. //! //! An example of deserializing with TOML is: //! #![cfg_attr(not(feature = "default"), doc = " ```ignore")] #![cfg_attr(feature = "default", doc = " ```")] //! use serde::Deserialize; //! //! #[derive(Deserialize)] //! struct Config { //! ip: String, //! port: Option, //! keys: Keys, //! } //! //! #[derive(Deserialize)] //! struct Keys { //! github: String, //! travis: Option, //! } //! //! let config: Config = toml::from_str(r#" //! ip = '127.0.0.1' //! //! [keys] //! github = 'xxxxxxxxxxxxxxxxx' //! travis = 'yyyyyyyyyyyyyyyyy' //! "#).unwrap(); //! //! assert_eq!(config.ip, "127.0.0.1"); //! assert_eq!(config.port, None); //! assert_eq!(config.keys.github, "xxxxxxxxxxxxxxxxx"); //! assert_eq!(config.keys.travis.as_ref().unwrap(), "yyyyyyyyyyyyyyyyy"); //! ``` //! //! You can serialize types in a similar fashion: //! #![cfg_attr(not(feature = "default"), doc = " ```ignore")] #![cfg_attr(feature = "default", doc = " ```")] //! use serde::Serialize; //! //! #[derive(Serialize)] //! struct Config { //! ip: String, //! port: Option, //! keys: Keys, //! } //! //! #[derive(Serialize)] //! struct Keys { //! github: String, //! travis: Option, //! } //! //! let config = Config { //! ip: "127.0.0.1".to_string(), //! port: None, //! keys: Keys { //! github: "xxxxxxxxxxxxxxxxx".to_string(), //! travis: Some("yyyyyyyyyyyyyyyyy".to_string()), //! }, //! }; //! //! let toml = toml::to_string(&config).unwrap(); //! ``` //! //! [TOML]: https://github.com/toml-lang/toml //! [Cargo]: https://crates.io/ //! [`serde`]: https://serde.rs/ //! [serde]: https://serde.rs/ #![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(all(not(feature = "std"), not(test)), no_std)] #![warn(clippy::std_instead_of_core)] #![warn(clippy::std_instead_of_alloc)] // Makes rustc abort compilation if there are any unsafe blocks in the crate. // Presence of this annotation is picked up by tools such as cargo-geiger // and lets them ensure that there is indeed no unsafe code as opposed to // something they couldn't detect (e.g. unsafe added via macro expansion, etc). #![forbid(unsafe_code)] #![warn(missing_docs)] #![warn(clippy::print_stderr)] #![warn(clippy::print_stdout)] #[allow(unused_extern_crates)] extern crate alloc; pub(crate) mod alloc_prelude { pub(crate) use alloc::borrow::ToOwned as _; pub(crate) use alloc::format; pub(crate) use alloc::string::String; pub(crate) use alloc::string::ToString as _; pub(crate) use alloc::vec::Vec; } pub mod map; #[cfg(feature = "serde")] pub mod value; pub mod de; #[cfg(feature = "serde")] pub mod ser; #[doc(hidden)] #[cfg(feature = "serde")] pub mod macros; #[cfg(feature = "serde")] mod table; #[doc(inline)] #[cfg(feature = "parse")] #[cfg(feature = "serde")] pub use crate::de::{from_slice, from_str, Deserializer}; #[doc(inline)] #[cfg(feature = "display")] #[cfg(feature = "serde")] pub use crate::ser::{to_string, to_string_pretty, Serializer}; #[doc(inline)] #[cfg(feature = "serde")] pub use crate::value::Value; pub use serde_spanned::Spanned; #[cfg(feature = "serde")] pub use table::Table; // Shortcuts for the module doc-comment #[allow(unused_imports)] use core::str::FromStr; #[allow(unused_imports)] use toml_datetime::Datetime; #[doc = include_str!("../README.md")] #[cfg(doctest)] pub struct ReadmeDoctests; toml-0.9.8/src/macros.rs000064400000000000000000000503221046102023000132400ustar 00000000000000pub use serde_core::de::{Deserialize, IntoDeserializer}; use crate::alloc_prelude::*; use crate::value::{Array, Table, Value}; /// Construct a [`Table`] from TOML syntax. /// /// ```rust /// let cargo_toml = toml::toml! { /// [package] /// name = "toml" /// /// [dependencies] /// serde = "1.0" /// /// [dev-dependencies] /// serde_derive = "1.0" /// serde_json = "1.0" /// }; /// /// println!("{:#?}", cargo_toml); /// ``` #[macro_export] macro_rules! toml { ($($toml:tt)+) => {{ let table = $crate::value::Table::new(); let mut root = $crate::Value::Table(table); $crate::toml_internal!(@toplevel root [] $($toml)+); match root { $crate::Value::Table(table) => table, _ => unreachable!(), } }}; } // TT-muncher to parse TOML syntax into a toml::Value. // // @toplevel -- Parse tokens outside of an inline table or inline array. In // this state, `[table headers]` and `[[array headers]]` are // allowed and `key = value` pairs are not separated by commas. // // @topleveldatetime -- Helper to parse a Datetime from string and insert it // into a table, continuing in the @toplevel state. // // @path -- Turn a path segment into a string. Segments that look like idents // are stringified, while quoted segments like `"cfg(windows)"` // are not. // // @value -- Parse the value part of a `key = value` pair, which may be a // primitive or inline table or inline array. // // @table -- Parse the contents of an inline table, returning them as a // toml::Value::Table. // // @tabledatetime -- Helper to parse a Datetime from string and insert it // into a table, continuing in the @table state. // // @array -- Parse the contents of an inline array, returning them as a // toml::Value::Array. // // @arraydatetime -- Helper to parse a Datetime from string and push it into // an array, continuing in the @array state. // // @trailingcomma -- Helper to append a comma to a sequence of tokens if the // sequence is non-empty and does not already end in a trailing // comma. // #[macro_export] #[doc(hidden)] macro_rules! toml_internal { // Base case, no elements remaining. (@toplevel $root:ident [$($path:tt)*]) => {}; // Parse negative number `key = -value`. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = - $v:tt $($rest:tt)*) => { $crate::toml_internal!(@toplevel $root [$($path)*] $($($k)-+).+ = (-$v) $($rest)*); }; // Parse positive number `key = +value`. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = + $v:tt $($rest:tt)*) => { $crate::toml_internal!(@toplevel $root [$($path)*] $($($k)-+).+ = ($v) $($rest)*); }; // Parse offset datetime `key = 1979-05-27T00:32:00.999999-07:00`. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec . $frac - $tzh : $tzm) $($rest)*); }; // Space instead of T. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec . $frac - $tzh : $tzm) $($rest)*); }; // Parse offset datetime `key = 1979-05-27T00:32:00-07:00`. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec - $tzh : $tzm) $($rest)*); }; // Space instead of T. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec - $tzh : $tzm) $($rest)*); }; // Parse local datetime `key = 1979-05-27T00:32:00.999999`. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec . $frac) $($rest)*); }; // Space instead of T. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec . $frac) $($rest)*); }; // Parse offset datetime `key = 1979-05-27T07:32:00Z` and local datetime `key = 1979-05-27T07:32:00`. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec) $($rest)*); }; // Space instead of T. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec) $($rest)*); }; // Parse local date `key = 1979-05-27`. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($yr - $mo - $day) $($rest)*); }; // Parse local time `key = 00:32:00.999999`. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $hr:tt : $min:tt : $sec:tt . $frac:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($hr : $min : $sec . $frac) $($rest)*); }; // Parse local time `key = 07:32:00`. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $hr:tt : $min:tt : $sec:tt $($rest:tt)*) => { $crate::toml_internal!(@topleveldatetime $root [$($path)*] $($($k)-+).+ = ($hr : $min : $sec) $($rest)*); }; // Parse any other `key = value` including string, inline array, inline // table, number, and boolean. (@toplevel $root:ident [$($path:tt)*] $($($k:tt)-+).+ = $v:tt $($rest:tt)*) => {{ $crate::macros::insert_toml( &mut $root, &[$($path)* $(&concat!($("-", $crate::toml_internal!(@path $k),)+)[1..], )+], $crate::toml_internal!(@value $v)); $crate::toml_internal!(@toplevel $root [$($path)*] $($rest)*); }}; // Parse array header `[[bin]]`. (@toplevel $root:ident $oldpath:tt [[$($($path:tt)-+).+]] $($rest:tt)*) => { $crate::macros::push_toml( &mut $root, &[$(&concat!($("-", $crate::toml_internal!(@path $path),)+)[1..],)+]); $crate::toml_internal!(@toplevel $root [$(&concat!($("-", $crate::toml_internal!(@path $path),)+)[1..],)+] $($rest)*); }; // Parse table header `[patch.crates-io]`. (@toplevel $root:ident $oldpath:tt [$($($path:tt)-+).+] $($rest:tt)*) => { $crate::macros::insert_toml( &mut $root, &[$(&concat!($("-", $crate::toml_internal!(@path $path),)+)[1..],)+], $crate::Value::Table($crate::value::Table::new())); $crate::toml_internal!(@toplevel $root [$(&concat!($("-", $crate::toml_internal!(@path $path),)+)[1..],)+] $($rest)*); }; // Parse datetime from string and insert into table. (@topleveldatetime $root:ident [$($path:tt)*] $($($k:tt)-+).+ = ($($datetime:tt)+) $($rest:tt)*) => { $crate::macros::insert_toml( &mut $root, &[$($path)* $(&concat!($("-", $crate::toml_internal!(@path $k),)+)[1..], )+], $crate::Value::Datetime(concat!($(stringify!($datetime)),+).parse().unwrap())); $crate::toml_internal!(@toplevel $root [$($path)*] $($rest)*); }; // Turn a path segment into a string. (@path $ident:ident) => { stringify!($ident) }; // For a path segment that is not an ident, expect that it is already a // quoted string, like in `[target."cfg(windows)".dependencies]`. (@path $quoted:tt) => { $quoted }; // Construct a Value from an inline table. (@value { $($inline:tt)* }) => {{ let mut table = $crate::Value::Table($crate::value::Table::new()); $crate::toml_internal!(@trailingcomma (@table table) $($inline)*); table }}; // Construct a Value from an inline array. (@value [ $($inline:tt)* ]) => {{ let mut array = $crate::value::Array::new(); $crate::toml_internal!(@trailingcomma (@array array) $($inline)*); $crate::Value::Array(array) }}; (@value (-nan)) => { $crate::Value::Float(::core::f64::NAN.copysign(-1.0)) }; (@value (nan)) => { $crate::Value::Float(::core::f64::NAN.copysign(1.0)) }; (@value nan) => { $crate::Value::Float(::core::f64::NAN.copysign(1.0)) }; (@value (-inf)) => { $crate::Value::Float(::core::f64::NEG_INFINITY) }; (@value (inf)) => { $crate::Value::Float(::core::f64::INFINITY) }; (@value inf) => { $crate::Value::Float(::core::f64::INFINITY) }; // Construct a Value from any other type, probably string or boolean or number. (@value $v:tt) => {{ // TODO: Implement this with something like serde_json::to_value instead. let de = $crate::macros::IntoDeserializer::<$crate::de::Error>::into_deserializer($v); <$crate::Value as $crate::macros::Deserialize>::deserialize(de).unwrap() }}; // Base case of inline table. (@table $root:ident) => {}; // Parse negative number `key = -value`. (@table $root:ident $($($k:tt)-+).+ = - $v:tt , $($rest:tt)*) => { $crate::toml_internal!(@table $root $($($k)-+).+ = (-$v) , $($rest)*); }; // Parse positive number `key = +value`. (@table $root:ident $($($k:tt)-+).+ = + $v:tt , $($rest:tt)*) => { $crate::toml_internal!(@table $root $($($k)-+).+ = ($v) , $($rest)*); }; // Parse offset datetime `key = 1979-05-27T00:32:00.999999-07:00`. (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec . $frac - $tzh : $tzm) $($rest)*); }; // Space instead of T. (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec . $frac - $tzh : $tzm) $($rest)*); }; // Parse offset datetime `key = 1979-05-27T00:32:00-07:00`. (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec - $tzh : $tzm) $($rest)*); }; // Space instead of T. (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec - $tzh : $tzm) $($rest)*); }; // Parse local datetime `key = 1979-05-27T00:32:00.999999`. (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec . $frac) $($rest)*); }; // Space instead of T. (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec . $frac) $($rest)*); }; // Parse offset datetime `key = 1979-05-27T07:32:00Z` and local datetime `key = 1979-05-27T07:32:00`. (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $dhr : $min : $sec) $($rest)*); }; // Space instead of T. (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day T $hr : $min : $sec) $($rest)*); }; // Parse local date `key = 1979-05-27`. (@table $root:ident $($($k:tt)-+).+ = $yr:tt - $mo:tt - $day:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($yr - $mo - $day) $($rest)*); }; // Parse local time `key = 00:32:00.999999`. (@table $root:ident $($($k:tt)-+).+ = $hr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($hr : $min : $sec . $frac) $($rest)*); }; // Parse local time `key = 07:32:00`. (@table $root:ident $($($k:tt)-+).+ = $hr:tt : $min:tt : $sec:tt , $($rest:tt)*) => { $crate::toml_internal!(@tabledatetime $root $($($k)-+).+ = ($hr : $min : $sec) $($rest)*); }; // Parse any other type, probably string or boolean or number. (@table $root:ident $($($k:tt)-+).+ = $v:tt , $($rest:tt)*) => { $crate::macros::insert_toml( &mut $root, &[$(&concat!($("-", $crate::toml_internal!(@path $k),)+)[1..], )+], $crate::toml_internal!(@value $v)); $crate::toml_internal!(@table $root $($rest)*); }; // Parse a Datetime from string and continue in @table state. (@tabledatetime $root:ident $($($k:tt)-+).+ = ($($datetime:tt)*) $($rest:tt)*) => { $crate::macros::insert_toml( &mut $root, &[$(&concat!($("-", $crate::toml_internal!(@path $k),)+)[1..], )+], $crate::Value::Datetime(concat!($(stringify!($datetime)),+).parse().unwrap())); $crate::toml_internal!(@table $root $($rest)*); }; // Base case of inline array. (@array $root:ident) => {}; // Parse negative number `-value`. (@array $root:ident - $v:tt , $($rest:tt)*) => { $crate::toml_internal!(@array $root (-$v) , $($rest)*); }; // Parse positive number `+value`. (@array $root:ident + $v:tt , $($rest:tt)*) => { $crate::toml_internal!(@array $root ($v) , $($rest)*); }; // Parse offset datetime `1979-05-27T00:32:00.999999-07:00`. (@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec . $frac - $tzh : $tzm) $($rest)*); }; // Space instead of T. (@array $root:ident $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($yr - $mo - $day T $hr : $min : $sec . $frac - $tzh : $tzm) $($rest)*); }; // Parse offset datetime `1979-05-27T00:32:00-07:00`. (@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec - $tzh : $tzm) $($rest)*); }; // Space instead of T. (@array $root:ident $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt - $tzh:tt : $tzm:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($yr - $mo - $day T $hr : $min : $sec - $tzh : $tzm) $($rest)*); }; // Parse local datetime `1979-05-27T00:32:00.999999`. (@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec . $frac) $($rest)*); }; // Space instead of T. (@array $root:ident $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($yr - $mo - $day T $hr : $min : $sec . $frac) $($rest)*); }; // Parse offset datetime `1979-05-27T07:32:00Z` and local datetime `1979-05-27T07:32:00`. (@array $root:ident $yr:tt - $mo:tt - $dhr:tt : $min:tt : $sec:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($yr - $mo - $dhr : $min : $sec) $($rest)*); }; // Space instead of T. (@array $root:ident $yr:tt - $mo:tt - $day:tt $hr:tt : $min:tt : $sec:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($yr - $mo - $day T $hr : $min : $sec) $($rest)*); }; // Parse local date `1979-05-27`. (@array $root:ident $yr:tt - $mo:tt - $day:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($yr - $mo - $day) $($rest)*); }; // Parse local time `00:32:00.999999`. (@array $root:ident $hr:tt : $min:tt : $sec:tt . $frac:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($hr : $min : $sec . $frac) $($rest)*); }; // Parse local time `07:32:00`. (@array $root:ident $hr:tt : $min:tt : $sec:tt , $($rest:tt)*) => { $crate::toml_internal!(@arraydatetime $root ($hr : $min : $sec) $($rest)*); }; // Parse any other type, probably string or boolean or number. (@array $root:ident $v:tt , $($rest:tt)*) => { $root.push($crate::toml_internal!(@value $v)); $crate::toml_internal!(@array $root $($rest)*); }; // Parse a Datetime from string and continue in @array state. (@arraydatetime $root:ident ($($datetime:tt)*) $($rest:tt)*) => { $root.push($crate::Value::Datetime(concat!($(stringify!($datetime)),+).parse().unwrap())); $crate::toml_internal!(@array $root $($rest)*); }; // No trailing comma required if the tokens are empty. (@trailingcomma ($($args:tt)*)) => { $crate::toml_internal!($($args)*); }; // Tokens end with a trailing comma, do not append another one. (@trailingcomma ($($args:tt)*) ,) => { $crate::toml_internal!($($args)* ,); }; // Tokens end with something other than comma, append a trailing comma. (@trailingcomma ($($args:tt)*) $last:tt) => { $crate::toml_internal!($($args)* $last ,); }; // Not yet at the last token. (@trailingcomma ($($args:tt)*) $first:tt $($rest:tt)+) => { $crate::toml_internal!(@trailingcomma ($($args)* $first) $($rest)+); }; } // Called when parsing a `key = value` pair. // Inserts an entry into the table at the given path. pub fn insert_toml(root: &mut Value, path: &[&str], value: Value) { *traverse(root, path) = value; } // Called when parsing an `[[array header]]`. // Pushes an empty table onto the array at the given path. pub fn push_toml(root: &mut Value, path: &[&str]) { let target = traverse(root, path); if !target.is_array() { *target = Value::Array(Array::new()); } target .as_array_mut() .unwrap() .push(Value::Table(Table::new())); } fn traverse<'a>(root: &'a mut Value, path: &[&str]) -> &'a mut Value { let mut cur = root; for &key in path { // Lexical lifetimes :D let cur1 = cur; // From the TOML spec: // // > Each double-bracketed sub-table will belong to the most recently // > defined table element above it. let cur2 = if cur1.is_array() { cur1.as_array_mut().unwrap().last_mut().unwrap() } else { cur1 }; // We are about to index into this value, so it better be a table. if !cur2.is_table() { *cur2 = Value::Table(Table::new()); } if !cur2.as_table().unwrap().contains_key(key) { // Insert an empty table for the next loop iteration to point to. let empty = Value::Table(Table::new()); cur2.as_table_mut().unwrap().insert(key.to_owned(), empty); } // Step into the current table. cur = cur2.as_table_mut().unwrap().get_mut(key).unwrap(); } cur } toml-0.9.8/src/map.rs000064400000000000000000000450031046102023000125310ustar 00000000000000// Copyright 2017 Serde Developers // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! A map of `String` to [Value][crate::Value]. //! //! By default the map is backed by a [`BTreeMap`]. Enable the `preserve_order` //! feature of toml-rs to use [`IndexMap`] instead. //! //! [`BTreeMap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html //! [`IndexMap`]: https://docs.rs/indexmap #[cfg(not(feature = "preserve_order"))] use alloc::collections::{btree_map, BTreeMap}; use core::borrow::Borrow; use core::fmt::{self, Debug}; use core::hash::Hash; use core::iter::FromIterator; use core::ops; #[cfg(feature = "preserve_order")] use indexmap::{self, IndexMap}; /// Represents a TOML key/value type. pub struct Map { map: MapImpl, dotted: bool, implicit: bool, inline: bool, } #[cfg(not(feature = "preserve_order"))] type MapImpl = BTreeMap; #[cfg(all(feature = "preserve_order", not(feature = "fast_hash")))] type RandomState = std::collections::hash_map::RandomState; #[cfg(all(feature = "preserve_order", feature = "fast_hash"))] type RandomState = foldhash::fast::RandomState; #[cfg(feature = "preserve_order")] type MapImpl = IndexMap; impl Map where K: Ord + Hash, { /// Makes a new empty Map. #[inline] pub fn new() -> Self { Self { #[cfg(feature = "preserve_order")] map: MapImpl::with_hasher(RandomState::default()), #[cfg(not(feature = "preserve_order"))] map: MapImpl::new(), dotted: false, implicit: false, inline: false, } } #[cfg(not(feature = "preserve_order"))] /// Makes a new empty Map with the given initial capacity. #[inline] pub fn with_capacity(capacity: usize) -> Self { // does not support with_capacity let _ = capacity; Self::new() } #[cfg(feature = "preserve_order")] /// Makes a new empty Map with the given initial capacity. #[inline] pub fn with_capacity(capacity: usize) -> Self { Self { map: IndexMap::with_capacity_and_hasher(capacity, RandomState::default()), dotted: false, implicit: false, inline: false, } } /// Clears the map, removing all values. #[inline] pub fn clear(&mut self) { self.map.clear(); } /// Returns a reference to the value corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but the ordering /// on the borrowed form *must* match the ordering on the key type. #[inline] pub fn get(&self, key: &Q) -> Option<&V> where K: Borrow, Q: Ord + Eq + Hash + ?Sized, { self.map.get(key) } /// Returns true if the map contains a value for the specified key. /// /// The key may be any borrowed form of the map's key type, but the ordering /// on the borrowed form *must* match the ordering on the key type. #[inline] pub fn contains_key(&self, key: &Q) -> bool where K: Borrow, Q: Ord + Eq + Hash + ?Sized, { self.map.contains_key(key) } /// Returns a mutable reference to the value corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but the ordering /// on the borrowed form *must* match the ordering on the key type. #[inline] pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> where K: Borrow, Q: Ord + Eq + Hash + ?Sized, { self.map.get_mut(key) } /// Returns the key-value pair matching the given key. /// /// The key may be any borrowed form of the map's key type, but the ordering /// on the borrowed form *must* match the ordering on the key type. #[inline] pub fn get_key_value(&self, key: &Q) -> Option<(&K, &V)> where K: Borrow, Q: ?Sized + Ord + Eq + Hash, { self.map.get_key_value(key) } /// Inserts a key-value pair into the map. /// /// If the map did not have this key present, `None` is returned. /// /// If the map did have this key present, the value is updated, and the old /// value is returned. The key is not updated, though; this matters for /// types that can be `==` without being identical. #[inline] pub fn insert(&mut self, k: K, v: V) -> Option { self.map.insert(k, v) } /// Removes a key from the map, returning the value at the key if the key /// was previously in the map. /// /// The key may be any borrowed form of the map's key type, but the ordering /// on the borrowed form *must* match the ordering on the key type. #[inline] pub fn remove(&mut self, key: &Q) -> Option where K: Borrow, Q: Ord + Eq + Hash + ?Sized, { #[cfg(not(feature = "preserve_order"))] { self.map.remove(key) } #[cfg(feature = "preserve_order")] { self.map.shift_remove(key) } } /// Removes a key from the map, returning the stored key and value if the key was previously in the map. #[inline] pub fn remove_entry(&mut self, key: &Q) -> Option<(K, V)> where K: Borrow, Q: Ord + Eq + Hash + ?Sized, { #[cfg(not(feature = "preserve_order"))] { self.map.remove_entry(key) } #[cfg(feature = "preserve_order")] { self.map.shift_remove_entry(key) } } /// Retains only the elements specified by the `keep` predicate. /// /// In other words, remove all pairs `(k, v)` for which `keep(&k, &mut v)` /// returns `false`. /// /// The elements are visited in iteration order. #[inline] pub fn retain(&mut self, mut keep: F) where K: AsRef, F: FnMut(&str, &mut V) -> bool, { self.map.retain(|key, value| keep(key.as_ref(), value)); } /// Gets the given key's corresponding entry in the map for in-place /// manipulation. pub fn entry(&mut self, key: S) -> Entry<'_, K, V> where S: Into, { #[cfg(not(feature = "preserve_order"))] use alloc::collections::btree_map::Entry as EntryImpl; #[cfg(feature = "preserve_order")] use indexmap::map::Entry as EntryImpl; match self.map.entry(key.into()) { EntryImpl::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant }), EntryImpl::Occupied(occupied) => Entry::Occupied(OccupiedEntry { occupied }), } } /// Returns the number of elements in the map. #[inline] pub fn len(&self) -> usize { self.map.len() } /// Returns true if the map contains no elements. #[inline] pub fn is_empty(&self) -> bool { self.map.is_empty() } /// Gets an iterator over the entries of the map. #[inline] pub fn iter(&self) -> Iter<'_, K, V> { Iter { iter: self.map.iter(), } } /// Gets a mutable iterator over the entries of the map. #[inline] pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { IterMut { iter: self.map.iter_mut(), } } /// Gets an iterator over the keys of the map. #[inline] pub fn keys(&self) -> Keys<'_, K, V> { Keys { iter: self.map.keys(), } } /// Gets an iterator over the values of the map. #[inline] pub fn values(&self) -> Values<'_, K, V> { Values { iter: self.map.values(), } } /// Scan through each key-value pair in the map and keep those where the /// closure `keep` returns `true`. /// /// The elements are visited in order, and remaining elements keep their /// order. /// /// Computes in **O(n)** time (average). #[allow(unused_mut)] pub(crate) fn mut_entries(&mut self, mut op: F) where F: FnMut(&mut K, &mut V), { #[cfg(feature = "preserve_order")] { use indexmap::map::MutableKeys as _; for (key, value) in self.map.iter_mut2() { op(key, value); } } #[cfg(not(feature = "preserve_order"))] { self.map = core::mem::take(&mut self.map) .into_iter() .map(move |(mut k, mut v)| { op(&mut k, &mut v); (k, v) }) .collect(); } } } impl Map where K: Ord, { pub(crate) fn is_dotted(&self) -> bool { self.dotted } pub(crate) fn is_implicit(&self) -> bool { self.implicit } pub(crate) fn is_inline(&self) -> bool { self.inline } pub(crate) fn set_implicit(&mut self, yes: bool) { self.implicit = yes; } pub(crate) fn set_dotted(&mut self, yes: bool) { self.dotted = yes; } pub(crate) fn set_inline(&mut self, yes: bool) { self.inline = yes; } } impl Default for Map where K: Ord + Hash, { #[inline] fn default() -> Self { Self::new() } } impl Clone for Map { #[inline] fn clone(&self) -> Self { Self { map: self.map.clone(), dotted: self.dotted, implicit: self.implicit, inline: self.inline, } } } impl PartialEq for Map { #[inline] fn eq(&self, other: &Self) -> bool { self.map.eq(&other.map) } } /// Access an element of this map. Panics if the given key is not present in the /// map. impl ops::Index<&Q> for Map where K: Borrow + Ord, Q: Ord + Eq + Hash + ?Sized, { type Output = V; fn index(&self, index: &Q) -> &V { self.map.index(index) } } /// Mutably access an element of this map. Panics if the given key is not /// present in the map. impl ops::IndexMut<&Q> for Map where K: Borrow + Ord, Q: Ord + Eq + Hash + ?Sized, { fn index_mut(&mut self, index: &Q) -> &mut V { self.map.get_mut(index).expect("no entry found for key") } } impl Debug for Map { #[inline] fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { self.map.fmt(formatter) } } impl FromIterator<(K, V)> for Map { fn from_iter(iter: T) -> Self where T: IntoIterator, { Self { map: FromIterator::from_iter(iter), dotted: false, implicit: false, inline: false, } } } impl Extend<(K, V)> for Map { fn extend(&mut self, iter: T) where T: IntoIterator, { self.map.extend(iter); } } macro_rules! delegate_iterator { (($name:ident $($generics:tt)*) => $item:ty) => { impl $($generics)* Iterator for $name $($generics)* { type Item = $item; #[inline] fn next(&mut self) -> Option { self.iter.next() } #[inline] fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl $($generics)* DoubleEndedIterator for $name $($generics)* { #[inline] fn next_back(&mut self) -> Option { self.iter.next_back() } } impl $($generics)* ExactSizeIterator for $name $($generics)* { #[inline] fn len(&self) -> usize { self.iter.len() } } } } ////////////////////////////////////////////////////////////////////////////// /// A view into a single entry in a map, which may either be vacant or occupied. /// This enum is constructed from the [`entry`] method on [`Map`]. /// /// [`entry`]: struct.Map.html#method.entry /// [`Map`]: struct.Map.html pub enum Entry<'a, K, V> { /// A vacant Entry. Vacant(VacantEntry<'a, K, V>), /// An occupied Entry. Occupied(OccupiedEntry<'a, K, V>), } /// A vacant Entry. It is part of the [`Entry`] enum. /// /// [`Entry`]: enum.Entry.html pub struct VacantEntry<'a, K, V> { vacant: VacantEntryImpl<'a, K, V>, } /// An occupied Entry. It is part of the [`Entry`] enum. /// /// [`Entry`]: enum.Entry.html pub struct OccupiedEntry<'a, K, V> { occupied: OccupiedEntryImpl<'a, K, V>, } #[cfg(not(feature = "preserve_order"))] type VacantEntryImpl<'a, K, V> = btree_map::VacantEntry<'a, K, V>; #[cfg(feature = "preserve_order")] type VacantEntryImpl<'a, K, V> = indexmap::map::VacantEntry<'a, K, V>; #[cfg(not(feature = "preserve_order"))] type OccupiedEntryImpl<'a, K, V> = btree_map::OccupiedEntry<'a, K, V>; #[cfg(feature = "preserve_order")] type OccupiedEntryImpl<'a, K, V> = indexmap::map::OccupiedEntry<'a, K, V>; impl<'a, K: Ord, V> Entry<'a, K, V> { /// Returns a reference to this entry's key. pub fn key(&self) -> &K { match *self { Entry::Vacant(ref e) => e.key(), Entry::Occupied(ref e) => e.key(), } } /// Ensures a value is in the entry by inserting the default if empty, and /// returns a mutable reference to the value in the entry. pub fn or_insert(self, default: V) -> &'a mut V { match self { Entry::Vacant(entry) => entry.insert(default), Entry::Occupied(entry) => entry.into_mut(), } } /// Ensures a value is in the entry by inserting the result of the default /// function if empty, and returns a mutable reference to the value in the /// entry. pub fn or_insert_with(self, default: F) -> &'a mut V where F: FnOnce() -> V, { match self { Entry::Vacant(entry) => entry.insert(default()), Entry::Occupied(entry) => entry.into_mut(), } } } impl<'a, K: Ord, V> VacantEntry<'a, K, V> { /// Gets a reference to the key that would be used when inserting a value /// through the `VacantEntry`. #[inline] pub fn key(&self) -> &K { self.vacant.key() } /// Sets the value of the entry with the `VacantEntry`'s key, and returns a /// mutable reference to it. #[inline] pub fn insert(self, value: V) -> &'a mut V { self.vacant.insert(value) } } impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> { /// Gets a reference to the key in the entry. #[inline] pub fn key(&self) -> &K { self.occupied.key() } /// Gets a reference to the value in the entry. #[inline] pub fn get(&self) -> &V { self.occupied.get() } /// Gets a mutable reference to the value in the entry. #[inline] pub fn get_mut(&mut self) -> &mut V { self.occupied.get_mut() } /// Converts the entry into a mutable reference to its value. #[inline] pub fn into_mut(self) -> &'a mut V { self.occupied.into_mut() } /// Sets the value of the entry with the `OccupiedEntry`'s key, and returns /// the entry's old value. #[inline] pub fn insert(&mut self, value: V) -> V { self.occupied.insert(value) } /// Takes the value of the entry out of the map, and returns it. #[inline] pub fn remove(self) -> V { #[cfg(not(feature = "preserve_order"))] { self.occupied.remove() } #[cfg(feature = "preserve_order")] { self.occupied.shift_remove() } } } ////////////////////////////////////////////////////////////////////////////// impl<'a, K, V> IntoIterator for &'a Map { type Item = (&'a K, &'a V); type IntoIter = Iter<'a, K, V>; #[inline] fn into_iter(self) -> Self::IntoIter { Iter { iter: self.map.iter(), } } } /// An iterator over a `toml::Map`'s entries. pub struct Iter<'a, K, V> { iter: IterImpl<'a, K, V>, } #[cfg(not(feature = "preserve_order"))] type IterImpl<'a, K, V> = btree_map::Iter<'a, K, V>; #[cfg(feature = "preserve_order")] type IterImpl<'a, K, V> = indexmap::map::Iter<'a, K, V>; delegate_iterator!((Iter<'a, K, V>) => (&'a K, &'a V)); ////////////////////////////////////////////////////////////////////////////// impl<'a, K, V> IntoIterator for &'a mut Map { type Item = (&'a K, &'a mut V); type IntoIter = IterMut<'a, K, V>; #[inline] fn into_iter(self) -> Self::IntoIter { IterMut { iter: self.map.iter_mut(), } } } /// A mutable iterator over a `toml::Map`'s entries. pub struct IterMut<'a, K, V> { iter: IterMutImpl<'a, K, V>, } #[cfg(not(feature = "preserve_order"))] type IterMutImpl<'a, K, V> = btree_map::IterMut<'a, K, V>; #[cfg(feature = "preserve_order")] type IterMutImpl<'a, K, V> = indexmap::map::IterMut<'a, K, V>; delegate_iterator!((IterMut<'a, K, V>) => (&'a K, &'a mut V)); ////////////////////////////////////////////////////////////////////////////// impl IntoIterator for Map { type Item = (K, V); type IntoIter = IntoIter; #[inline] fn into_iter(self) -> Self::IntoIter { IntoIter { iter: self.map.into_iter(), } } } /// An owning iterator over a `toml::Map`'s entries. pub struct IntoIter { iter: IntoIterImpl, } #[cfg(not(feature = "preserve_order"))] type IntoIterImpl = btree_map::IntoIter; #[cfg(feature = "preserve_order")] type IntoIterImpl = indexmap::map::IntoIter; delegate_iterator!((IntoIter) => (K, V)); ////////////////////////////////////////////////////////////////////////////// /// An iterator over a `toml::Map`'s keys. pub struct Keys<'a, K, V> { iter: KeysImpl<'a, K, V>, } #[cfg(not(feature = "preserve_order"))] type KeysImpl<'a, K, V> = btree_map::Keys<'a, K, V>; #[cfg(feature = "preserve_order")] type KeysImpl<'a, K, V> = indexmap::map::Keys<'a, K, V>; delegate_iterator!((Keys<'a, K, V>) => &'a K); ////////////////////////////////////////////////////////////////////////////// /// An iterator over a `toml::Map`'s values. pub struct Values<'a, K, V> { iter: ValuesImpl<'a, K, V>, } #[cfg(not(feature = "preserve_order"))] type ValuesImpl<'a, K, V> = btree_map::Values<'a, K, V>; #[cfg(feature = "preserve_order")] type ValuesImpl<'a, K, V> = indexmap::map::Values<'a, K, V>; delegate_iterator!((Values<'a, K, V>) => &'a V); toml-0.9.8/src/ser/document/array.rs000064400000000000000000000035671046102023000155120ustar 00000000000000use core::fmt::Write as _; use toml_writer::TomlWrite as _; use super::style::Style; use super::value::ValueSerializer; use super::Buffer; use super::Error; use super::Table; #[doc(hidden)] pub struct SerializeDocumentTupleVariant<'d> { buf: &'d mut Buffer, table: Table, seen_value: bool, style: Style, } impl<'d> SerializeDocumentTupleVariant<'d> { pub(crate) fn tuple( buf: &'d mut Buffer, mut table: Table, variant: &'static str, _len: usize, style: Style, ) -> Result { let dst = table.body_mut(); dst.key(variant)?; dst.space()?; dst.keyval_sep()?; dst.space()?; dst.open_array()?; Ok(Self { buf, table, seen_value: false, style, }) } } impl<'d> serde_core::ser::SerializeTupleVariant for SerializeDocumentTupleVariant<'d> { type Ok = &'d mut Buffer; type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<(), Error> where T: serde_core::ser::Serialize + ?Sized, { let dst = self.table.body_mut(); if self.style.multiline_array { dst.newline()?; write!(dst, " ")?; } else { if self.seen_value { dst.val_sep()?; dst.space()?; } } self.seen_value = true; value.serialize(ValueSerializer::with_style(dst, self.style))?; if self.style.multiline_array { dst.val_sep()?; } Ok(()) } fn end(mut self) -> Result { let dst = self.table.body_mut(); if self.style.multiline_array && self.seen_value { dst.newline()?; } dst.close_array()?; dst.newline()?; self.buf.push(self.table); Ok(self.buf) } } toml-0.9.8/src/ser/document/array_of_tables.rs000064400000000000000000000174271046102023000175300ustar 00000000000000use super::style::Style; use super::Buffer; use super::Error; use super::Serializer; use super::Table; use crate::alloc_prelude::*; pub(crate) struct ArrayOfTablesSerializer<'d> { buf: &'d mut Buffer, parent: Table, key: String, style: Style, } impl<'d> ArrayOfTablesSerializer<'d> { /// Creates a new serializer which will emit TOML into the buffer provided. /// /// The serializer can then be used to serialize a type after which the data /// will be present in `dst`. pub(crate) fn new(buf: &'d mut Buffer, parent: Table, key: String, style: Style) -> Self { Self { buf, parent, key, style, } } } impl<'d> serde_core::ser::Serializer for ArrayOfTablesSerializer<'d> { type Ok = &'d mut Buffer; type Error = Error; type SerializeSeq = SerializeArrayOfTablesSerializer<'d>; type SerializeTuple = SerializeArrayOfTablesSerializer<'d>; type SerializeTupleStruct = SerializeArrayOfTablesSerializer<'d>; type SerializeTupleVariant = serde_core::ser::Impossible; type SerializeMap = serde_core::ser::Impossible; type SerializeStruct = serde_core::ser::Impossible; type SerializeStructVariant = serde_core::ser::Impossible; fn serialize_bool(self, _v: bool) -> Result { Err(Error::unsupported_type(Some("bool"))) } fn serialize_i8(self, _v: i8) -> Result { Err(Error::unsupported_type(Some("i8"))) } fn serialize_i16(self, _v: i16) -> Result { Err(Error::unsupported_type(Some("i16"))) } fn serialize_i32(self, _v: i32) -> Result { Err(Error::unsupported_type(Some("i32"))) } fn serialize_i64(self, _v: i64) -> Result { Err(Error::unsupported_type(Some("i64"))) } fn serialize_u8(self, _v: u8) -> Result { Err(Error::unsupported_type(Some("u8"))) } fn serialize_u16(self, _v: u16) -> Result { Err(Error::unsupported_type(Some("u16"))) } fn serialize_u32(self, _v: u32) -> Result { Err(Error::unsupported_type(Some("u32"))) } fn serialize_u64(self, _v: u64) -> Result { Err(Error::unsupported_type(Some("u64"))) } fn serialize_f32(self, _v: f32) -> Result { Err(Error::unsupported_type(Some("f32"))) } fn serialize_f64(self, _v: f64) -> Result { Err(Error::unsupported_type(Some("f64"))) } fn serialize_char(self, _v: char) -> Result { Err(Error::unsupported_type(Some("char"))) } fn serialize_str(self, _v: &str) -> Result { Err(Error::unsupported_type(Some("str"))) } fn serialize_bytes(self, _v: &[u8]) -> Result { Err(Error::unsupported_type(Some("bytes"))) } fn serialize_none(self) -> Result { Err(Error::unsupported_none()) } fn serialize_some(self, v: &T) -> Result where T: serde_core::ser::Serialize + ?Sized, { v.serialize(self) } fn serialize_unit(self) -> Result { Err(Error::unsupported_type(Some("unit"))) } fn serialize_unit_struct(self, name: &'static str) -> Result { Err(Error::unsupported_type(Some(name))) } fn serialize_unit_variant( self, name: &'static str, _variant_index: u32, _variant: &'static str, ) -> Result { Err(Error::unsupported_type(Some(name))) } fn serialize_newtype_struct( self, _name: &'static str, v: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { v.serialize(self) } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, _value: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { Err(Error::unsupported_type(Some(variant))) } fn serialize_seq(self, _len: Option) -> Result { Ok(SerializeArrayOfTablesSerializer::seq( self.buf, self.parent, self.key, self.style, )) } fn serialize_tuple(self, len: usize) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_struct( self, _name: &'static str, len: usize, ) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, _len: usize, ) -> Result { Err(Error::unsupported_type(Some(variant))) } fn serialize_map(self, _len: Option) -> Result { Err(Error::unsupported_type(Some("map"))) } fn serialize_struct( self, name: &'static str, _len: usize, ) -> Result { Err(Error::unsupported_type(Some(name))) } fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, _len: usize, ) -> Result { Err(Error::unsupported_type(Some(variant))) } } #[doc(hidden)] pub(crate) struct SerializeArrayOfTablesSerializer<'d> { buf: &'d mut Buffer, parent: Table, key: String, style: Style, } impl<'d> SerializeArrayOfTablesSerializer<'d> { pub(crate) fn seq(buf: &'d mut Buffer, parent: Table, key: String, style: Style) -> Self { Self { buf, parent, key, style, } } fn end(self) -> Result<&'d mut Buffer, Error> { Ok(self.buf) } } impl<'d> serde_core::ser::SerializeSeq for SerializeArrayOfTablesSerializer<'d> { type Ok = &'d mut Buffer; type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<(), Error> where T: serde_core::ser::Serialize + ?Sized, { let child = self.buf.element_table(&mut self.parent, self.key.clone()); let value_serializer = Serializer::with_table(self.buf, child, self.style); value.serialize(value_serializer)?; Ok(()) } fn end(self) -> Result { self.end() } } impl<'d> serde_core::ser::SerializeTuple for SerializeArrayOfTablesSerializer<'d> { type Ok = &'d mut Buffer; type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<(), Error> where T: serde_core::ser::Serialize + ?Sized, { serde_core::ser::SerializeSeq::serialize_element(self, value) } fn end(self) -> Result { serde_core::ser::SerializeSeq::end(self) } } impl<'d> serde_core::ser::SerializeTupleStruct for SerializeArrayOfTablesSerializer<'d> { type Ok = &'d mut Buffer; type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<(), Error> where T: serde_core::ser::Serialize + ?Sized, { serde_core::ser::SerializeSeq::serialize_element(self, value) } fn end(self) -> Result { serde_core::ser::SerializeSeq::end(self) } } toml-0.9.8/src/ser/document/buffer.rs000064400000000000000000000062401046102023000156340ustar 00000000000000use toml_writer::TomlWrite as _; use crate::alloc_prelude::*; /// TOML Document serialization buffer #[derive(Debug, Default)] pub struct Buffer { tables: Vec>, } impl Buffer { /// Initialize a new serialization buffer pub fn new() -> Self { Default::default() } /// Reset the buffer for serializing another document pub fn clear(&mut self) { self.tables.clear(); } pub(crate) fn root_table(&mut self) -> Table { self.new_table(None) } pub(crate) fn child_table(&mut self, parent: &mut Table, key: String) -> Table { parent.has_children = true; let mut key_path = parent.key.clone(); key_path.get_or_insert_with(Vec::new).push(key); self.new_table(key_path) } pub(crate) fn element_table(&mut self, parent: &mut Table, key: String) -> Table { let mut table = self.child_table(parent, key); table.array = true; table } pub(crate) fn new_table(&mut self, key: Option>) -> Table { let pos = self.tables.len(); let table = Table { key, body: String::new(), has_children: false, pos, array: false, }; self.tables.push(None); table } pub(crate) fn push(&mut self, table: Table) { let pos = table.pos; self.tables[pos] = Some(table); } } impl core::fmt::Display for Buffer { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let mut tables = self .tables .iter() .filter_map(|t| t.as_ref()) .filter(|t| required_table(t)); if let Some(table) = tables.next() { table.fmt(f)?; } for table in tables { f.newline()?; table.fmt(f)?; } Ok(()) } } fn required_table(table: &Table) -> bool { if table.key.is_none() { !table.body.is_empty() } else { table.array || !table.body.is_empty() || !table.has_children } } #[derive(Clone, Debug)] pub(crate) struct Table { key: Option>, body: String, has_children: bool, array: bool, pos: usize, } impl Table { pub(crate) fn body_mut(&mut self) -> &mut String { &mut self.body } pub(crate) fn has_children(&mut self, yes: bool) { self.has_children = yes; } } impl core::fmt::Display for Table { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { if let Some(key) = &self.key { if self.array { f.open_array_of_tables_header()?; } else { f.open_table_header()?; } let mut key = key.iter(); if let Some(key) = key.next() { write!(f, "{key}")?; } for key in key { f.key_sep()?; write!(f, "{key}")?; } if self.array { f.close_array_of_tables_header()?; } else { f.close_table_header()?; } f.newline()?; } self.body.fmt(f)?; Ok(()) } } toml-0.9.8/src/ser/document/map.rs000064400000000000000000000120711046102023000151370ustar 00000000000000use core::fmt::Write as _; use toml_writer::TomlWrite as _; use super::array_of_tables::ArrayOfTablesSerializer; use super::style::Style; use super::value::KeySerializer; use super::value::ValueSerializer; use super::Buffer; use super::Error; use super::SerializationStrategy; use super::Serializer; use super::Table; use crate::alloc_prelude::*; #[doc(hidden)] pub struct SerializeDocumentTable<'d> { buf: &'d mut Buffer, table: Table, key: Option, style: Style, } impl<'d> SerializeDocumentTable<'d> { pub(crate) fn map(buf: &'d mut Buffer, table: Table, style: Style) -> Result { Ok(Self { buf, table, key: None, style, }) } fn end(self) -> Result<&'d mut Buffer, Error> { self.buf.push(self.table); Ok(self.buf) } } impl<'d> serde_core::ser::SerializeMap for SerializeDocumentTable<'d> { type Ok = &'d mut Buffer; type Error = Error; fn serialize_key(&mut self, input: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { let mut encoded_key = String::new(); input.serialize(KeySerializer { dst: &mut encoded_key, })?; self.key = Some(encoded_key); Ok(()) } fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { let encoded_key = self .key .take() .expect("always called after `serialize_key`"); match SerializationStrategy::from(value) { SerializationStrategy::Value => { let dst = self.table.body_mut(); write!(dst, "{encoded_key}")?; dst.space()?; dst.keyval_sep()?; dst.space()?; let value_serializer = ValueSerializer::with_style(dst, self.style); let dst = value.serialize(value_serializer)?; dst.newline()?; } SerializationStrategy::ArrayOfTables => { self.table.has_children(true); let value_serializer = ArrayOfTablesSerializer::new( self.buf, self.table.clone(), encoded_key, self.style, ); value.serialize(value_serializer)?; } SerializationStrategy::Table | SerializationStrategy::Unknown => { let child = self.buf.child_table(&mut self.table, encoded_key); let value_serializer = Serializer::with_table(self.buf, child, self.style); value.serialize(value_serializer)?; } SerializationStrategy::Skip => { // silently drop these key-value pairs } } Ok(()) } fn end(self) -> Result { self.end() } } impl<'d> serde_core::ser::SerializeStruct for SerializeDocumentTable<'d> { type Ok = &'d mut Buffer; type Error = Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { match SerializationStrategy::from(value) { SerializationStrategy::Value => { let dst = self.table.body_mut(); dst.key(key)?; dst.space()?; dst.keyval_sep()?; dst.space()?; let value_serializer = ValueSerializer::with_style(dst, self.style); let dst = value.serialize(value_serializer)?; dst.newline()?; } SerializationStrategy::ArrayOfTables => { self.table.has_children(true); let value_serializer = ArrayOfTablesSerializer::new( self.buf, self.table.clone(), key.to_owned(), self.style, ); value.serialize(value_serializer)?; } SerializationStrategy::Table | SerializationStrategy::Unknown => { let child = self.buf.child_table(&mut self.table, key.to_owned()); let value_serializer = Serializer::with_table(self.buf, child, self.style); value.serialize(value_serializer)?; } SerializationStrategy::Skip => { // silently drop these key-value pairs } } Ok(()) } fn end(self) -> Result { self.end() } } impl<'d> serde_core::ser::SerializeStructVariant for SerializeDocumentTable<'d> { type Ok = &'d mut Buffer; type Error = Error; #[inline] fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { serde_core::ser::SerializeStruct::serialize_field(self, key, value) } #[inline] fn end(self) -> Result { self.end() } } toml-0.9.8/src/ser/document/mod.rs000064400000000000000000000204611046102023000151430ustar 00000000000000//! Serializing Rust structures into TOML. //! //! This module contains all the Serde support for serializing Rust structures //! into TOML documents (as strings). Note that some top-level functions here //! are also provided at the top of the crate. mod array; mod array_of_tables; mod buffer; mod map; mod strategy; use toml_writer::TomlWrite as _; use super::style; use super::value; use super::Error; use crate::alloc_prelude::*; use buffer::Table; use strategy::SerializationStrategy; pub use buffer::Buffer; /// Serialization for TOML documents. /// /// This structure implements serialization support for TOML to serialize an /// arbitrary type to TOML. Note that the TOML format does not support all /// datatypes in Rust, such as enums, tuples, and tuple structs. These types /// will generate an error when serialized. /// /// Currently a serializer always writes its output to an in-memory `String`, /// which is passed in when creating the serializer itself. /// /// To serialize TOML values, instead of documents, see /// [`ValueSerializer`][super::value::ValueSerializer]. pub struct Serializer<'d> { buf: &'d mut Buffer, style: style::Style, table: Table, } impl<'d> Serializer<'d> { /// Creates a new serializer which will emit TOML into the buffer provided. /// /// The serializer can then be used to serialize a type after which the data /// will be present in `buf`. pub fn new(buf: &'d mut Buffer) -> Self { let table = buf.root_table(); Self { buf, style: Default::default(), table, } } /// Apply a default "pretty" policy to the document /// /// For greater customization, instead serialize to a /// [`toml_edit::DocumentMut`](https://docs.rs/toml_edit/latest/toml_edit/struct.DocumentMut.html). pub fn pretty(buf: &'d mut Buffer) -> Self { let mut ser = Serializer::new(buf); ser.style.multiline_array = true; ser } pub(crate) fn with_table(buf: &'d mut Buffer, table: Table, style: style::Style) -> Self { Self { buf, style, table } } fn end(self) -> Result<&'d mut Buffer, Error> { self.buf.push(self.table); Ok(self.buf) } } impl<'d> serde_core::ser::Serializer for Serializer<'d> { type Ok = &'d mut Buffer; type Error = Error; type SerializeSeq = serde_core::ser::Impossible; type SerializeTuple = serde_core::ser::Impossible; type SerializeTupleStruct = serde_core::ser::Impossible; type SerializeTupleVariant = array::SerializeDocumentTupleVariant<'d>; type SerializeMap = map::SerializeDocumentTable<'d>; type SerializeStruct = map::SerializeDocumentTable<'d>; type SerializeStructVariant = map::SerializeDocumentTable<'d>; fn serialize_bool(self, _v: bool) -> Result { Err(Error::unsupported_type(Some("bool"))) } fn serialize_i8(self, _v: i8) -> Result { Err(Error::unsupported_type(Some("i8"))) } fn serialize_i16(self, _v: i16) -> Result { Err(Error::unsupported_type(Some("i16"))) } fn serialize_i32(self, _v: i32) -> Result { Err(Error::unsupported_type(Some("i32"))) } fn serialize_i64(self, _v: i64) -> Result { Err(Error::unsupported_type(Some("i64"))) } fn serialize_u8(self, _v: u8) -> Result { Err(Error::unsupported_type(Some("u8"))) } fn serialize_u16(self, _v: u16) -> Result { Err(Error::unsupported_type(Some("u16"))) } fn serialize_u32(self, _v: u32) -> Result { Err(Error::unsupported_type(Some("u32"))) } fn serialize_u64(self, _v: u64) -> Result { Err(Error::unsupported_type(Some("u64"))) } fn serialize_f32(self, _v: f32) -> Result { Err(Error::unsupported_type(Some("f32"))) } fn serialize_f64(self, _v: f64) -> Result { Err(Error::unsupported_type(Some("f64"))) } fn serialize_char(self, _v: char) -> Result { Err(Error::unsupported_type(Some("char"))) } fn serialize_str(self, _v: &str) -> Result { Err(Error::unsupported_type(Some("str"))) } fn serialize_bytes(self, _v: &[u8]) -> Result { Err(Error::unsupported_type(Some("bytes"))) } fn serialize_none(self) -> Result { Err(Error::unsupported_none()) } fn serialize_some(self, v: &T) -> Result where T: serde_core::ser::Serialize + ?Sized, { v.serialize(self) } fn serialize_unit(self) -> Result { Err(Error::unsupported_type(Some("unit"))) } fn serialize_unit_struct(self, name: &'static str) -> Result { Err(Error::unsupported_type(Some(name))) } fn serialize_unit_variant( self, name: &'static str, _variant_index: u32, _variant: &'static str, ) -> Result { Err(Error::unsupported_type(Some(name))) } fn serialize_newtype_struct( self, _name: &'static str, v: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { v.serialize(self) } fn serialize_newtype_variant( mut self, _name: &'static str, _variant_index: u32, variant: &'static str, value: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { match SerializationStrategy::from(value) { SerializationStrategy::Value | SerializationStrategy::ArrayOfTables => { let dst = self.table.body_mut(); dst.key(variant)?; dst.space()?; dst.keyval_sep()?; dst.space()?; let value_serializer = value::ValueSerializer::with_style(dst, self.style); let dst = value.serialize(value_serializer)?; dst.newline()?; } SerializationStrategy::Table | SerializationStrategy::Unknown => { let child = self.buf.child_table(&mut self.table, variant.to_owned()); let value_serializer = Serializer::with_table(self.buf, child, self.style); value.serialize(value_serializer)?; } SerializationStrategy::Skip => { // silently drop these key-value pairs } } self.end() } fn serialize_seq(self, _len: Option) -> Result { Err(Error::unsupported_type(Some("array"))) } fn serialize_tuple(self, len: usize) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_struct( self, _name: &'static str, len: usize, ) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize, ) -> Result { array::SerializeDocumentTupleVariant::tuple(self.buf, self.table, variant, len, self.style) } fn serialize_map(self, _len: Option) -> Result { map::SerializeDocumentTable::map(self.buf, self.table, self.style) } fn serialize_struct( self, _name: &'static str, len: usize, ) -> Result { self.serialize_map(Some(len)) } fn serialize_struct_variant( mut self, _name: &'static str, _variant_index: u32, variant: &'static str, _len: usize, ) -> Result { let child = self.buf.child_table(&mut self.table, variant.to_owned()); self.buf.push(self.table); map::SerializeDocumentTable::map(self.buf, child, self.style) } } toml-0.9.8/src/ser/document/strategy.rs000064400000000000000000000221101046102023000162170ustar 00000000000000#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub(crate) enum SerializationStrategy { Value, Table, ArrayOfTables, Skip, Unknown, } impl From<&T> for SerializationStrategy where T: serde_core::ser::Serialize + ?Sized, { fn from(value: &T) -> Self { value.serialize(WalkValue).unwrap_err() } } impl serde_core::ser::Error for SerializationStrategy { fn custom(_msg: T) -> Self where T: core::fmt::Display, { Self::Unknown } } impl core::fmt::Display for SerializationStrategy { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { "error".fmt(f) } } #[cfg(feature = "std")] impl std::error::Error for SerializationStrategy {} #[cfg(not(feature = "std"))] impl serde_core::de::StdError for SerializationStrategy {} struct WalkValue; impl serde_core::ser::Serializer for WalkValue { type Ok = core::convert::Infallible; type Error = SerializationStrategy; type SerializeSeq = ArrayWalkValue; type SerializeTuple = ArrayWalkValue; type SerializeTupleStruct = ArrayWalkValue; type SerializeTupleVariant = serde_core::ser::Impossible; type SerializeMap = serde_core::ser::Impossible; type SerializeStruct = StructWalkValue; type SerializeStructVariant = serde_core::ser::Impossible; fn serialize_bool(self, _v: bool) -> Result { Err(SerializationStrategy::Value) } fn serialize_i8(self, _v: i8) -> Result { Err(SerializationStrategy::Value) } fn serialize_i16(self, _v: i16) -> Result { Err(SerializationStrategy::Value) } fn serialize_i32(self, _v: i32) -> Result { Err(SerializationStrategy::Value) } fn serialize_i64(self, _v: i64) -> Result { Err(SerializationStrategy::Value) } fn serialize_i128(self, _v: i128) -> Result { Err(SerializationStrategy::Value) } fn serialize_u8(self, _v: u8) -> Result { Err(SerializationStrategy::Value) } fn serialize_u16(self, _v: u16) -> Result { Err(SerializationStrategy::Value) } fn serialize_u32(self, _v: u32) -> Result { Err(SerializationStrategy::Value) } fn serialize_u64(self, _v: u64) -> Result { Err(SerializationStrategy::Value) } fn serialize_u128(self, _v: u128) -> Result { Err(SerializationStrategy::Value) } fn serialize_f32(self, _v: f32) -> Result { Err(SerializationStrategy::Value) } fn serialize_f64(self, _v: f64) -> Result { Err(SerializationStrategy::Value) } fn serialize_char(self, _v: char) -> Result { Err(SerializationStrategy::Value) } fn serialize_str(self, _v: &str) -> Result { Err(SerializationStrategy::Value) } fn serialize_bytes(self, _v: &[u8]) -> Result { Err(SerializationStrategy::Value) } fn serialize_none(self) -> Result { Err(SerializationStrategy::Skip) } fn serialize_some(self, v: &T) -> Result where T: serde_core::ser::Serialize + ?Sized, { v.serialize(self) } fn serialize_unit(self) -> Result { Err(SerializationStrategy::Value) } fn serialize_unit_struct(self, _name: &'static str) -> Result { Err(SerializationStrategy::Value) } fn serialize_unit_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, ) -> Result { Err(SerializationStrategy::Value) } fn serialize_newtype_struct( self, _name: &'static str, v: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { v.serialize(self) } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _value: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { Err(SerializationStrategy::Table) } fn serialize_seq(self, _len: Option) -> Result { Ok(ArrayWalkValue::new()) } fn serialize_tuple(self, len: usize) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_struct( self, _name: &'static str, len: usize, ) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(SerializationStrategy::Table) } fn serialize_map(self, _len: Option) -> Result { Err(SerializationStrategy::Table) } fn serialize_struct( self, name: &'static str, _len: usize, ) -> Result { if toml_datetime::ser::is_datetime(name) { Ok(StructWalkValue) } else { Err(SerializationStrategy::Table) } } fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(SerializationStrategy::Table) } } #[doc(hidden)] pub(crate) struct ArrayWalkValue { is_empty: bool, } impl ArrayWalkValue { fn new() -> Self { Self { is_empty: true } } fn serialize_element(&mut self, value: &T) -> Result<(), SerializationStrategy> where T: serde_core::ser::Serialize + ?Sized, { self.is_empty = false; match SerializationStrategy::from(value) { SerializationStrategy::Value | SerializationStrategy::ArrayOfTables | SerializationStrategy::Unknown | SerializationStrategy::Skip => Err(SerializationStrategy::Value), SerializationStrategy::Table => Ok(()), } } fn end(self) -> Result { if self.is_empty { Err(SerializationStrategy::Value) } else { Err(SerializationStrategy::ArrayOfTables) } } } impl serde_core::ser::SerializeSeq for ArrayWalkValue { type Ok = core::convert::Infallible; type Error = SerializationStrategy; fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { self.serialize_element(value) } fn end(self) -> Result { self.end() } } impl serde_core::ser::SerializeTuple for ArrayWalkValue { type Ok = core::convert::Infallible; type Error = SerializationStrategy; fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { self.serialize_element(value) } fn end(self) -> Result { self.end() } } impl serde_core::ser::SerializeTupleStruct for ArrayWalkValue { type Ok = core::convert::Infallible; type Error = SerializationStrategy; fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { self.serialize_element(value) } fn end(self) -> Result { self.end() } } pub(crate) struct StructWalkValue; impl serde_core::ser::SerializeMap for StructWalkValue { type Ok = core::convert::Infallible; type Error = SerializationStrategy; fn serialize_key(&mut self, _input: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { Ok(()) } fn serialize_value(&mut self, _value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { Ok(()) } fn end(self) -> Result { // is date time Err(SerializationStrategy::Value) } } impl serde_core::ser::SerializeStruct for StructWalkValue { type Ok = core::convert::Infallible; type Error = SerializationStrategy; fn serialize_field(&mut self, _key: &'static str, _value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { Ok(()) } fn end(self) -> Result { // is date time Err(SerializationStrategy::Value) } } toml-0.9.8/src/ser/error.rs000064400000000000000000000061131046102023000136750ustar 00000000000000use crate::alloc_prelude::*; /// Errors that can occur when serializing a type. #[derive(Clone, PartialEq, Eq)] pub struct Error { pub(crate) inner: ErrorInner, } impl Error { pub(crate) fn new(inner: impl core::fmt::Display) -> Self { Self { inner: ErrorInner::Custom(inner.to_string()), } } pub(crate) fn unsupported_type(t: Option<&'static str>) -> Self { Self { inner: ErrorInner::UnsupportedType(t), } } pub(crate) fn out_of_range(t: Option<&'static str>) -> Self { Self { inner: ErrorInner::OutOfRange(t), } } pub(crate) fn unsupported_none() -> Self { Self { inner: ErrorInner::UnsupportedNone, } } pub(crate) fn key_not_string() -> Self { Self { inner: ErrorInner::KeyNotString, } } #[cfg(feature = "display")] pub(crate) fn date_invalid() -> Self { Self { inner: ErrorInner::DateInvalid, } } } impl From for Error { fn from(_: core::fmt::Error) -> Self { Self::new("an error occurred when writing a value") } } impl serde_core::ser::Error for Error { fn custom(msg: T) -> Self where T: core::fmt::Display, { Self::new(msg) } } impl core::fmt::Display for Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.inner.fmt(f) } } impl core::fmt::Debug for Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.inner.fmt(f) } } #[cfg(feature = "std")] impl std::error::Error for Error {} #[cfg(not(feature = "std"))] impl serde_core::de::StdError for Error {} /// Errors that can occur when deserializing a type. #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[non_exhaustive] pub(crate) enum ErrorInner { /// Type could not be serialized to TOML UnsupportedType(Option<&'static str>), /// Value was out of range for the given type OutOfRange(Option<&'static str>), /// `None` could not be serialized to TOML UnsupportedNone, /// Key was not convertible to `String` for serializing to TOML KeyNotString, /// A serialized date was invalid DateInvalid, /// Other serialization error Custom(String), } impl core::fmt::Display for ErrorInner { fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { Self::UnsupportedType(Some(t)) => write!(formatter, "unsupported {t} type"), Self::UnsupportedType(None) => write!(formatter, "unsupported rust type"), Self::OutOfRange(Some(t)) => write!(formatter, "out-of-range value for {t} type"), Self::OutOfRange(None) => write!(formatter, "out-of-range value"), Self::UnsupportedNone => "unsupported None value".fmt(formatter), Self::KeyNotString => "map key was not a string".fmt(formatter), Self::DateInvalid => "a serialized date was invalid".fmt(formatter), Self::Custom(s) => s.fmt(formatter), } } } toml-0.9.8/src/ser/mod.rs000064400000000000000000000051401046102023000133220ustar 00000000000000//! Serializing Rust structures into TOML. //! //! This module contains all the Serde support for serializing Rust structures //! into TOML documents (as strings). Note that some top-level functions here //! are also provided at the top of the crate. #[cfg(feature = "display")] mod document; mod error; #[cfg(feature = "display")] mod style; #[cfg(feature = "display")] mod value; use crate::alloc_prelude::*; #[cfg(feature = "display")] pub use document::Buffer; #[cfg(feature = "display")] pub use document::Serializer; pub use error::Error; pub(crate) use error::ErrorInner; #[cfg(feature = "display")] pub use value::ValueSerializer; /// Serialize the given data structure as a String of TOML. /// /// Serialization can fail if `T`'s implementation of `Serialize` decides to /// fail, if `T` contains a map with non-string keys, or if `T` attempts to /// serialize an unsupported datatype such as an enum, tuple, or tuple struct. /// /// To serialize TOML values, instead of documents, see [`ValueSerializer`]. /// /// # Examples /// /// ``` /// use serde::Serialize; /// /// #[derive(Serialize)] /// struct Config { /// database: Database, /// } /// /// #[derive(Serialize)] /// struct Database { /// ip: String, /// port: Vec, /// connection_max: u32, /// enabled: bool, /// } /// /// let config = Config { /// database: Database { /// ip: "192.168.1.1".to_string(), /// port: vec![8001, 8002, 8003], /// connection_max: 5000, /// enabled: false, /// }, /// }; /// /// let toml = toml::to_string(&config).unwrap(); /// println!("{}", toml) /// ``` #[cfg(feature = "display")] pub fn to_string(value: &T) -> Result where T: serde_core::ser::Serialize + ?Sized, { let mut output = Buffer::new(); let serializer = Serializer::new(&mut output); value.serialize(serializer)?; Ok(output.to_string()) } /// Serialize the given data structure as a "pretty" String of TOML. /// /// This is identical to `to_string` except the output string has a more /// "pretty" output. See `Serializer::pretty` for more details. /// /// To serialize TOML values, instead of documents, see [`ValueSerializer`]. /// /// For greater customization, instead serialize to a /// [`toml_edit::DocumentMut`](https://docs.rs/toml_edit/latest/toml_edit/struct.DocumentMut.html). #[cfg(feature = "display")] pub fn to_string_pretty(value: &T) -> Result where T: serde_core::ser::Serialize + ?Sized, { let mut output = Buffer::new(); let serializer = Serializer::pretty(&mut output); value.serialize(serializer)?; Ok(output.to_string()) } toml-0.9.8/src/ser/style.rs000064400000000000000000000001421046102023000137000ustar 00000000000000#[derive(Copy, Clone, Default)] pub(crate) struct Style { pub(crate) multiline_array: bool, } toml-0.9.8/src/ser/value/array.rs000064400000000000000000000071531046102023000150030ustar 00000000000000use core::fmt::Write as _; use toml_writer::TomlWrite as _; use super::Error; use super::Style; use crate::alloc_prelude::*; #[doc(hidden)] pub struct SerializeValueArray<'d> { dst: &'d mut String, seen_value: bool, style: Style, len: Option, } impl<'d> SerializeValueArray<'d> { pub(crate) fn seq( dst: &'d mut String, style: Style, len: Option, ) -> Result { dst.open_array()?; Ok(Self { dst, seen_value: false, style, len, }) } fn end(self) -> Result<&'d mut String, Error> { if self.multiline_array() && self.seen_value { self.dst.newline()?; } self.dst.close_array()?; Ok(self.dst) } fn multiline_array(&self) -> bool { self.style.multiline_array && 2 <= self.len.unwrap_or(usize::MAX) } } impl<'d> serde_core::ser::SerializeSeq for SerializeValueArray<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<(), Error> where T: serde_core::ser::Serialize + ?Sized, { if self.multiline_array() { self.dst.newline()?; write!(self.dst, " ")?; } else { if self.seen_value { self.dst.val_sep()?; self.dst.space()?; } } self.seen_value = true; value.serialize(super::ValueSerializer::with_style(self.dst, self.style))?; if self.multiline_array() { self.dst.val_sep()?; } Ok(()) } fn end(self) -> Result { self.end() } } impl<'d> serde_core::ser::SerializeTuple for SerializeValueArray<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<(), Error> where T: serde_core::ser::Serialize + ?Sized, { serde_core::ser::SerializeSeq::serialize_element(self, value) } fn end(self) -> Result { serde_core::ser::SerializeSeq::end(self) } } impl<'d> serde_core::ser::SerializeTupleStruct for SerializeValueArray<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<(), Error> where T: serde_core::ser::Serialize + ?Sized, { serde_core::ser::SerializeSeq::serialize_element(self, value) } fn end(self) -> Result { serde_core::ser::SerializeSeq::end(self) } } pub struct SerializeTupleVariant<'d> { inner: SerializeValueArray<'d>, } impl<'d> SerializeTupleVariant<'d> { pub(crate) fn tuple( dst: &'d mut String, variant: &'static str, len: usize, style: Style, ) -> Result { dst.open_inline_table()?; dst.space()?; dst.key(variant)?; dst.space()?; dst.keyval_sep()?; dst.space()?; Ok(Self { inner: SerializeValueArray::seq(dst, style, Some(len))?, }) } } impl<'d> serde_core::ser::SerializeTupleVariant for SerializeTupleVariant<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<(), Error> where T: serde_core::ser::Serialize + ?Sized, { serde_core::ser::SerializeSeq::serialize_element(&mut self.inner, value) } fn end(self) -> Result { let dst = self.inner.end()?; dst.space()?; dst.close_inline_table()?; Ok(dst) } } toml-0.9.8/src/ser/value/key.rs000064400000000000000000000126151046102023000144540ustar 00000000000000use toml_writer::TomlWrite as _; use super::Error; use crate::alloc_prelude::*; pub(crate) struct KeySerializer<'d> { pub(crate) dst: &'d mut String, } impl serde_core::ser::Serializer for KeySerializer<'_> { type Ok = (); type Error = Error; type SerializeSeq = serde_core::ser::Impossible; type SerializeTuple = serde_core::ser::Impossible; type SerializeTupleStruct = serde_core::ser::Impossible; type SerializeTupleVariant = serde_core::ser::Impossible; type SerializeMap = serde_core::ser::Impossible; type SerializeStruct = serde_core::ser::Impossible; type SerializeStructVariant = serde_core::ser::Impossible; fn serialize_bool(self, v: bool) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_i8(self, v: i8) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_i16(self, v: i16) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_i32(self, v: i32) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_i64(self, v: i64) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_i128(self, v: i128) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_u8(self, v: u8) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_u16(self, v: u16) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_u32(self, v: u32) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_u64(self, v: u64) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_u128(self, v: u128) -> Result { self.dst.key(v.to_string())?; Ok(()) } fn serialize_f32(self, _v: f32) -> Result { Err(Error::key_not_string()) } fn serialize_f64(self, _v: f64) -> Result { Err(Error::key_not_string()) } fn serialize_char(self, v: char) -> Result { let mut b = [0; 4]; let result = v.encode_utf8(&mut b); self.dst.key(&*result)?; Ok(()) } fn serialize_str(self, value: &str) -> Result { self.dst.key(value)?; Ok(()) } fn serialize_bytes(self, _value: &[u8]) -> Result { Err(Error::key_not_string()) } fn serialize_none(self) -> Result { Err(Error::key_not_string()) } fn serialize_some(self, _value: &T) -> Result where T: serde_core::ser::Serialize + ?Sized, { Err(Error::key_not_string()) } fn serialize_unit(self) -> Result { Err(Error::key_not_string()) } fn serialize_unit_struct(self, _name: &'static str) -> Result { Err(Error::key_not_string()) } fn serialize_unit_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, ) -> Result { self.dst.key(variant)?; Ok(()) } fn serialize_newtype_struct( self, _name: &'static str, value: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { value.serialize(self) } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _value: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { Err(Error::key_not_string()) } fn serialize_seq(self, _len: Option) -> Result { Err(Error::key_not_string()) } fn serialize_tuple(self, _len: usize) -> Result { Err(Error::key_not_string()) } fn serialize_tuple_struct( self, _name: &'static str, _len: usize, ) -> Result { Err(Error::key_not_string()) } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(Error::key_not_string()) } fn serialize_map(self, _len: Option) -> Result { Err(Error::key_not_string()) } fn serialize_struct( self, _name: &'static str, _len: usize, ) -> Result { Err(Error::key_not_string()) } fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(Error::key_not_string()) } } toml-0.9.8/src/ser/value/map.rs000064400000000000000000000361051046102023000144410ustar 00000000000000use core::fmt::Write as _; use toml_writer::TomlWrite as _; use super::array::SerializeTupleVariant; use super::array::SerializeValueArray; use super::key::KeySerializer; use super::Error; use super::Style; use super::ValueSerializer; use crate::alloc_prelude::*; #[doc(hidden)] #[allow(clippy::large_enum_variant)] pub enum SerializeMap<'d> { Datetime(SerializeDatetime<'d>), Table(SerializeTable<'d>), } impl<'d> SerializeMap<'d> { pub(crate) fn map(dst: &'d mut String, style: Style) -> Result { Ok(Self::Table(SerializeTable::map(dst, style)?)) } pub(crate) fn struct_( name: &'static str, dst: &'d mut String, style: Style, ) -> Result { if toml_datetime::ser::is_datetime(name) { Ok(Self::Datetime(SerializeDatetime::new(dst))) } else { Ok(Self::map(dst, style)?) } } } impl<'d> serde_core::ser::SerializeMap for SerializeMap<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_key(&mut self, input: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { match self { Self::Datetime(s) => s.serialize_key(input), Self::Table(s) => s.serialize_key(input), } } fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { match self { Self::Datetime(s) => s.serialize_value(value), Self::Table(s) => s.serialize_value(value), } } fn end(self) -> Result { match self { Self::Datetime(s) => s.end(), Self::Table(s) => s.end(), } } } impl<'d> serde_core::ser::SerializeStruct for SerializeMap<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { match self { Self::Datetime(s) => s.serialize_field(key, value), Self::Table(s) => s.serialize_field(key, value), } } fn end(self) -> Result { match self { Self::Datetime(s) => s.end(), Self::Table(s) => s.end(), } } } #[doc(hidden)] pub struct SerializeDatetime<'d> { dst: &'d mut String, inner: toml_datetime::ser::DatetimeSerializer, } impl<'d> SerializeDatetime<'d> { pub(crate) fn new(dst: &'d mut String) -> Self { Self { dst, inner: toml_datetime::ser::DatetimeSerializer::new(), } } } impl<'d> serde_core::ser::SerializeMap for SerializeDatetime<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_key(&mut self, _input: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { unreachable!("datetimes should only be serialized as structs, not maps") } fn serialize_value(&mut self, _value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { unreachable!("datetimes should only be serialized as structs, not maps") } fn end(self) -> Result { unreachable!("datetimes should only be serialized as structs, not maps") } } impl<'d> serde_core::ser::SerializeStruct for SerializeDatetime<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { self.inner.serialize_field(key, value).map_err(dt_err)?; Ok(()) } fn end(self) -> Result { let value = self.inner.end().map_err(dt_err)?; write!(self.dst, "{value}")?; Ok(self.dst) } } fn dt_err(err: toml_datetime::ser::SerializerError) -> Error { match err { toml_datetime::ser::SerializerError::InvalidFormat(err) => Error::new(err), _ => Error::date_invalid(), } } #[doc(hidden)] pub struct SerializeTable<'d> { dst: &'d mut String, seen_value: bool, key: Option, style: Style, } impl<'d> SerializeTable<'d> { pub(crate) fn map(dst: &'d mut String, style: Style) -> Result { dst.open_inline_table()?; Ok(Self { dst, seen_value: false, key: None, style, }) } pub(crate) fn end(self) -> Result<&'d mut String, Error> { if self.seen_value { self.dst.space()?; } self.dst.close_inline_table()?; Ok(self.dst) } } impl<'d> serde_core::ser::SerializeMap for SerializeTable<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_key(&mut self, input: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { let mut encoded_key = String::new(); input.serialize(KeySerializer { dst: &mut encoded_key, })?; self.key = Some(encoded_key); Ok(()) } fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { let encoded_key = self .key .take() .expect("always called after `serialize_key`"); let mut encoded_value = String::new(); let mut is_none = false; let value_serializer = MapValueSerializer::new(&mut encoded_value, &mut is_none, self.style); let res = value.serialize(value_serializer); match res { Ok(_) => { use core::fmt::Write as _; if self.seen_value { self.dst.val_sep()?; } self.seen_value = true; self.dst.space()?; write!(self.dst, "{encoded_key}")?; self.dst.space()?; self.dst.keyval_sep()?; self.dst.space()?; write!(self.dst, "{encoded_value}")?; } Err(e) => { if !(e == Error::unsupported_none() && is_none) { return Err(e); } } } Ok(()) } fn end(self) -> Result { self.end() } } impl<'d> serde_core::ser::SerializeStruct for SerializeTable<'d> { type Ok = &'d mut String; type Error = Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { let mut encoded_value = String::new(); let mut is_none = false; let value_serializer = MapValueSerializer::new(&mut encoded_value, &mut is_none, self.style); let res = value.serialize(value_serializer); match res { Ok(_) => { use core::fmt::Write as _; if self.seen_value { self.dst.val_sep()?; } self.seen_value = true; self.dst.space()?; self.dst.key(key)?; self.dst.space()?; self.dst.keyval_sep()?; self.dst.space()?; write!(self.dst, "{encoded_value}")?; } Err(e) => { if !(e == Error::unsupported_none() && is_none) { return Err(e); } } } Ok(()) } fn end(self) -> Result { self.end() } } pub(crate) struct MapValueSerializer<'d> { dst: &'d mut String, is_none: &'d mut bool, style: Style, } impl<'d> MapValueSerializer<'d> { pub(crate) fn new(dst: &'d mut String, is_none: &'d mut bool, style: Style) -> Self { Self { dst, is_none, style, } } } impl<'d> serde_core::ser::Serializer for MapValueSerializer<'d> { type Ok = &'d mut String; type Error = Error; type SerializeSeq = SerializeValueArray<'d>; type SerializeTuple = SerializeValueArray<'d>; type SerializeTupleStruct = SerializeValueArray<'d>; type SerializeTupleVariant = SerializeTupleVariant<'d>; type SerializeMap = SerializeMap<'d>; type SerializeStruct = SerializeMap<'d>; type SerializeStructVariant = SerializeStructVariant<'d>; fn serialize_bool(self, v: bool) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_bool(v) } fn serialize_i8(self, v: i8) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_i8(v) } fn serialize_i16(self, v: i16) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_i16(v) } fn serialize_i32(self, v: i32) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_i32(v) } fn serialize_i64(self, v: i64) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_i64(v) } fn serialize_u8(self, v: u8) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_u8(v) } fn serialize_u16(self, v: u16) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_u16(v) } fn serialize_u32(self, v: u32) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_u32(v) } fn serialize_u64(self, v: u64) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_u64(v) } fn serialize_f32(self, v: f32) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_f32(v) } fn serialize_f64(self, v: f64) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_f64(v) } fn serialize_char(self, v: char) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_char(v) } fn serialize_str(self, v: &str) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_str(v) } fn serialize_bytes(self, value: &[u8]) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_bytes(value) } fn serialize_none(self) -> Result { *self.is_none = true; Err(Error::unsupported_none()) } fn serialize_some(self, value: &T) -> Result where T: serde_core::ser::Serialize + ?Sized, { ValueSerializer::with_style(self.dst, self.style).serialize_some(value) } fn serialize_unit(self) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_unit() } fn serialize_unit_struct(self, name: &'static str) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_unit_struct(name) } fn serialize_unit_variant( self, name: &'static str, variant_index: u32, variant: &'static str, ) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_unit_variant( name, variant_index, variant, ) } fn serialize_newtype_struct( self, _name: &'static str, value: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { value.serialize(self) } fn serialize_newtype_variant( self, name: &'static str, variant_index: u32, variant: &'static str, value: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { ValueSerializer::with_style(self.dst, self.style).serialize_newtype_variant( name, variant_index, variant, value, ) } fn serialize_seq(self, len: Option) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_seq(len) } fn serialize_tuple(self, len: usize) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_tuple(len) } fn serialize_tuple_struct( self, name: &'static str, len: usize, ) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_tuple_struct(name, len) } fn serialize_tuple_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_tuple_variant( name, variant_index, variant, len, ) } fn serialize_map(self, len: Option) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_map(len) } fn serialize_struct( self, name: &'static str, len: usize, ) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_struct(name, len) } fn serialize_struct_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result { ValueSerializer::with_style(self.dst, self.style).serialize_struct_variant( name, variant_index, variant, len, ) } } pub struct SerializeStructVariant<'d> { inner: SerializeTable<'d>, } impl<'d> SerializeStructVariant<'d> { pub(crate) fn struct_( dst: &'d mut String, variant: &'static str, _len: usize, style: Style, ) -> Result { dst.open_inline_table()?; dst.space()?; dst.key(variant)?; dst.space()?; dst.keyval_sep()?; dst.space()?; Ok(Self { inner: SerializeTable::map(dst, style)?, }) } } impl<'d> serde_core::ser::SerializeStructVariant for SerializeStructVariant<'d> { type Ok = &'d mut String; type Error = Error; #[inline] fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: serde_core::ser::Serialize + ?Sized, { serde_core::ser::SerializeStruct::serialize_field(&mut self.inner, key, value) } #[inline] fn end(self) -> Result { let dst = serde_core::ser::SerializeStruct::end(self.inner)?; dst.space()?; dst.close_inline_table()?; Ok(dst) } } toml-0.9.8/src/ser/value/mod.rs000064400000000000000000000205061046102023000144410ustar 00000000000000mod array; mod key; mod map; use toml_writer::TomlWrite as _; use super::style::Style; use super::Error; use crate::alloc_prelude::*; #[allow(clippy::wildcard_imports)] pub(crate) use array::*; #[allow(clippy::wildcard_imports)] pub(crate) use key::*; #[allow(clippy::wildcard_imports)] pub(crate) use map::*; /// Serialization for TOML [values][crate::Value]. /// /// This structure implements serialization support for TOML to serialize an /// arbitrary type to TOML. Note that the TOML format does not support all /// datatypes in Rust, such as enums, tuples, and tuple structs. These types /// will generate an error when serialized. /// /// Currently a serializer always writes its output to an in-memory `String`, /// which is passed in when creating the serializer itself. /// /// # Examples /// /// ``` /// use serde::Serialize; /// /// #[derive(Serialize)] /// struct Config { /// database: Database, /// } /// /// #[derive(Serialize)] /// struct Database { /// ip: String, /// port: Vec, /// connection_max: u32, /// enabled: bool, /// } /// /// let config = Config { /// database: Database { /// ip: "192.168.1.1".to_string(), /// port: vec![8001, 8002, 8003], /// connection_max: 5000, /// enabled: false, /// }, /// }; /// /// let mut value = String::new(); /// serde::Serialize::serialize( /// &config, /// toml::ser::ValueSerializer::new(&mut value) /// ).unwrap(); /// println!("{}", value) /// ``` pub struct ValueSerializer<'d> { dst: &'d mut String, style: Style, } impl<'d> ValueSerializer<'d> { /// Creates a new serializer which will emit TOML into the buffer provided. /// /// The serializer can then be used to serialize a type after which the data /// will be present in `dst`. pub fn new(dst: &'d mut String) -> Self { Self { dst, style: Default::default(), } } pub(crate) fn with_style(dst: &'d mut String, style: Style) -> Self { Self { dst, style } } } impl<'d> serde_core::ser::Serializer for ValueSerializer<'d> { type Ok = &'d mut String; type Error = Error; type SerializeSeq = SerializeValueArray<'d>; type SerializeTuple = SerializeValueArray<'d>; type SerializeTupleStruct = SerializeValueArray<'d>; type SerializeTupleVariant = SerializeTupleVariant<'d>; type SerializeMap = SerializeMap<'d>; type SerializeStruct = SerializeMap<'d>; type SerializeStructVariant = SerializeStructVariant<'d>; fn serialize_bool(self, v: bool) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_i8(self, v: i8) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_i16(self, v: i16) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_i32(self, v: i32) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_i64(self, v: i64) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_u8(self, v: u8) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_u16(self, v: u16) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_u32(self, v: u32) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_u64(self, v: u64) -> Result { let v: i64 = v .try_into() .map_err(|_err| Error::out_of_range(Some("u64")))?; self.serialize_i64(v) } fn serialize_f32(self, mut v: f32) -> Result { // Discard sign of NaN when serialized using Serde. // // In all likelihood the sign of NaNs is not meaningful in the user's // program. Ending up with `-nan` in the TOML document would usually be // surprising and undesirable, when the sign of the NaN was not // intentionally controlled by the caller, or may even be // nondeterministic if it comes from arithmetic operations or a cast. if v.is_nan() { v = v.copysign(1.0); } self.dst.value(v)?; Ok(self.dst) } fn serialize_f64(self, mut v: f64) -> Result { // Discard sign of NaN when serialized using Serde. // // In all likelihood the sign of NaNs is not meaningful in the user's // program. Ending up with `-nan` in the TOML document would usually be // surprising and undesirable, when the sign of the NaN was not // intentionally controlled by the caller, or may even be // nondeterministic if it comes from arithmetic operations or a cast. if v.is_nan() { v = v.copysign(1.0); } self.dst.value(v)?; Ok(self.dst) } fn serialize_char(self, v: char) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_str(self, v: &str) -> Result { self.dst.value(v)?; Ok(self.dst) } fn serialize_bytes(self, value: &[u8]) -> Result { use serde_core::ser::Serialize; value.serialize(self) } fn serialize_none(self) -> Result { Err(Error::unsupported_none()) } fn serialize_some(self, value: &T) -> Result where T: serde_core::ser::Serialize + ?Sized, { value.serialize(self) } fn serialize_unit(self) -> Result { Err(Error::unsupported_type(Some("unit"))) } fn serialize_unit_struct(self, name: &'static str) -> Result { Err(Error::unsupported_type(Some(name))) } fn serialize_unit_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, ) -> Result { self.serialize_str(variant) } fn serialize_newtype_struct( self, _name: &'static str, value: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { value.serialize(self) } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, value: &T, ) -> Result where T: serde_core::ser::Serialize + ?Sized, { self.dst.open_inline_table()?; self.dst.space()?; self.dst.key(variant)?; self.dst.space()?; self.dst.keyval_sep()?; self.dst.space()?; value.serialize(ValueSerializer::with_style(self.dst, self.style))?; self.dst.space()?; self.dst.close_inline_table()?; Ok(self.dst) } fn serialize_seq(self, len: Option) -> Result { SerializeValueArray::seq(self.dst, self.style, len) } fn serialize_tuple(self, len: usize) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_struct( self, _name: &'static str, len: usize, ) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize, ) -> Result { SerializeTupleVariant::tuple(self.dst, variant, len, self.style) } fn serialize_map(self, _len: Option) -> Result { SerializeMap::map(self.dst, self.style) } fn serialize_struct( self, name: &'static str, _len: usize, ) -> Result { SerializeMap::struct_(name, self.dst, self.style) } fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize, ) -> Result { SerializeStructVariant::struct_(self.dst, variant, len, self.style) } } toml-0.9.8/src/table.rs000064400000000000000000000302451046102023000130450ustar 00000000000000use serde_core::de; use serde_core::ser; use crate::alloc_prelude::*; use crate::map::Map; use crate::Value; /// Type representing a TOML table, payload of the `Value::Table` variant. /// /// By default it entries are stored in /// [lexicographic order](https://doc.rust-lang.org/std/primitive.str.html#impl-Ord-for-str) /// of the keys. Enable the `preserve_order` feature to store entries in the order they appear in /// the source file. pub type Table = Map; impl Table { /// Convert a `T` into `toml::Table`. /// /// This conversion can fail if `T`'s implementation of `Serialize` decides to /// fail, or if `T` contains a map with non-string keys. pub fn try_from(value: T) -> Result where T: ser::Serialize, { value.serialize(TableSerializer) } /// Interpret a `toml::Table` as an instance of type `T`. /// /// This conversion can fail if the structure of the `Table` does not match the structure /// expected by `T`, for example if `T` is a bool which can't be mapped to a `Table`. It can /// also fail if the structure is correct but `T`'s implementation of `Deserialize` decides /// that something is wrong with the data, for example required struct fields are missing from /// the TOML map or some number is too big to fit in the expected primitive type. pub fn try_into<'de, T>(self) -> Result where T: de::Deserialize<'de>, { de::Deserialize::deserialize(self) } } #[cfg(feature = "display")] impl core::fmt::Display for Table { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { crate::ser::to_string(self) .expect("Unable to represent value as string") .fmt(f) } } #[cfg(feature = "parse")] impl core::str::FromStr for Table { type Err = crate::de::Error; fn from_str(s: &str) -> Result { crate::from_str(s) } } impl ser::Serialize for Table { #[inline] fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { use serde_core::ser::SerializeMap; let mut map = serializer.serialize_map(Some(self.len()))?; for (k, v) in self { map.serialize_key(k)?; map.serialize_value(v)?; } map.end() } } impl<'de> de::Deserialize<'de> for Table { #[inline] fn deserialize(deserializer: D) -> Result where D: de::Deserializer<'de>, { struct Visitor; impl<'de> de::Visitor<'de> for Visitor { type Value = Map; fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { formatter.write_str("a map") } #[inline] fn visit_unit(self) -> Result where E: de::Error, { Ok(Map::new()) } #[inline] fn visit_map(self, mut visitor: V) -> Result where V: de::MapAccess<'de>, { let mut values = Map::new(); while let Some((key, value)) = visitor.next_entry()? { values.insert(key, value); } Ok(values) } } deserializer.deserialize_map(Visitor) } } impl<'de> de::Deserializer<'de> for Table { type Error = crate::de::Error; fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { Value::Table(self).deserialize_any(visitor) } #[inline] fn deserialize_enum( self, name: &'static str, variants: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { Value::Table(self).deserialize_enum(name, variants, visitor) } // `None` is interpreted as a missing field so be sure to implement `Some` // as a present field. fn deserialize_option(self, visitor: V) -> Result where V: de::Visitor<'de>, { Value::Table(self).deserialize_option(visitor) } fn deserialize_newtype_struct( self, name: &'static str, visitor: V, ) -> Result where V: de::Visitor<'de>, { Value::Table(self).deserialize_newtype_struct(name, visitor) } serde_core::forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq bytes byte_buf map unit_struct tuple_struct struct tuple ignored_any identifier } } impl de::IntoDeserializer<'_, crate::de::Error> for Table { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } pub(crate) struct TableSerializer; impl ser::Serializer for TableSerializer { type Ok = Table; type Error = crate::ser::Error; type SerializeSeq = ser::Impossible; type SerializeTuple = ser::Impossible; type SerializeTupleStruct = ser::Impossible; type SerializeTupleVariant = ser::Impossible; type SerializeMap = SerializeMap; type SerializeStruct = SerializeMap; type SerializeStructVariant = ser::Impossible; fn serialize_bool(self, _value: bool) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_i8(self, _value: i8) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_i16(self, _value: i16) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_i32(self, _value: i32) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_i64(self, _value: i64) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_u8(self, _value: u8) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_u16(self, _value: u16) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_u32(self, _value: u32) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_u64(self, _value: u64) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_f32(self, _value: f32) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_f64(self, _value: f64) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_char(self, _value: char) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_str(self, _value: &str) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_bytes(self, _value: &[u8]) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_unit(self) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_unit_struct(self, _name: &'static str) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_unit_variant( self, name: &'static str, _variant_index: u32, _variant: &'static str, ) -> Result { Err(crate::ser::Error::unsupported_type(Some(name))) } fn serialize_newtype_struct( self, _name: &'static str, value: &T, ) -> Result where T: ser::Serialize + ?Sized, { value.serialize(self) } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, value: &T, ) -> Result where T: ser::Serialize + ?Sized, { let value = value.serialize(crate::value::ValueSerializer)?; let mut table = Table::new(); table.insert(variant.to_owned(), value); Ok(table) } fn serialize_none(self) -> Result { Err(crate::ser::Error::unsupported_none()) } fn serialize_some(self, value: &T) -> Result where T: ser::Serialize + ?Sized, { value.serialize(self) } fn serialize_seq(self, _len: Option) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_tuple(self, _len: usize) -> Result { Err(crate::ser::Error::unsupported_type(None)) } fn serialize_tuple_struct( self, name: &'static str, _len: usize, ) -> Result { Err(crate::ser::Error::unsupported_type(Some(name))) } fn serialize_tuple_variant( self, name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(crate::ser::Error::unsupported_type(Some(name))) } fn serialize_map(self, _len: Option) -> Result { Ok(SerializeMap::new()) } fn serialize_struct( self, _name: &'static str, len: usize, ) -> Result { self.serialize_map(Some(len)) } fn serialize_struct_variant( self, name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(crate::ser::Error::unsupported_type(Some(name))) } } pub(crate) struct SerializeMap { map: Table, next_key: Option, } impl SerializeMap { pub(crate) fn new() -> Self { Self { map: Table::new(), next_key: None, } } pub(crate) fn with_capacity(capacity: usize) -> Self { Self { map: Table::with_capacity(capacity), next_key: None, } } } impl ser::SerializeMap for SerializeMap { type Ok = Table; type Error = crate::ser::Error; fn serialize_key(&mut self, key: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { match Value::try_from(key)? { Value::String(s) => self.next_key = Some(s), _ => return Err(crate::ser::Error::key_not_string()), }; Ok(()) } fn serialize_value(&mut self, value: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { let key = self.next_key.take(); let key = key.expect("serialize_value called before serialize_key"); match Value::try_from(value) { Ok(value) => { self.map.insert(key, value); } Err(crate::ser::Error { inner: crate::ser::ErrorInner::UnsupportedNone, }) => {} Err(e) => return Err(e), } Ok(()) } fn end(self) -> Result { Ok(self.map) } } impl ser::SerializeStruct for SerializeMap { type Ok = Table; type Error = crate::ser::Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { ser::SerializeMap::serialize_key(self, key)?; ser::SerializeMap::serialize_value(self, value) } fn end(self) -> Result { ser::SerializeMap::end(self) } } toml-0.9.8/src/value.rs000064400000000000000000001051021046102023000130650ustar 00000000000000//! Definition of a TOML [value][Value] use alloc::collections::BTreeMap; use alloc::vec; use core::fmt; use core::hash::Hash; use core::mem::discriminant; use core::ops; #[cfg(feature = "std")] use std::collections::HashMap; use serde_core::de; use serde_core::de::IntoDeserializer; use serde_core::ser; use crate::alloc_prelude::*; pub use toml_datetime::{Date, Datetime, DatetimeParseError, Offset, Time}; /// Type representing a TOML array, payload of the `Value::Array` variant pub type Array = Vec; #[doc(no_inline)] pub use crate::Table; /// Representation of a TOML value. #[derive(PartialEq, Clone, Debug)] pub enum Value { /// Represents a TOML string String(String), /// Represents a TOML integer Integer(i64), /// Represents a TOML float Float(f64), /// Represents a TOML boolean Boolean(bool), /// Represents a TOML datetime Datetime(Datetime), /// Represents a TOML array Array(Array), /// Represents a TOML table Table(Table), } impl Value { /// Convert a `T` into `toml::Value` which is an enum that can represent /// any valid TOML data. /// /// This conversion can fail if `T`'s implementation of `Serialize` decides to /// fail, or if `T` contains a map with non-string keys. pub fn try_from(value: T) -> Result where T: ser::Serialize, { value.serialize(ValueSerializer) } /// Interpret a `toml::Value` as an instance of type `T`. /// /// This conversion can fail if the structure of the `Value` does not match the /// structure expected by `T`, for example if `T` is a struct type but the /// `Value` contains something other than a TOML table. It can also fail if the /// structure is correct but `T`'s implementation of `Deserialize` decides that /// something is wrong with the data, for example required struct fields are /// missing from the TOML map or some number is too big to fit in the expected /// primitive type. pub fn try_into<'de, T>(self) -> Result where T: de::Deserialize<'de>, { de::Deserialize::deserialize(self) } /// Index into a TOML array or map. A string index can be used to access a /// value in a map, and a usize index can be used to access an element of an /// array. /// /// Returns `None` if the type of `self` does not match the type of the /// index, for example if the index is a string and `self` is an array or a /// number. Also returns `None` if the given key does not exist in the map /// or the given index is not within the bounds of the array. pub fn get(&self, index: I) -> Option<&Self> { index.index(self) } /// Mutably index into a TOML array or map. A string index can be used to /// access a value in a map, and a usize index can be used to access an /// element of an array. /// /// Returns `None` if the type of `self` does not match the type of the /// index, for example if the index is a string and `self` is an array or a /// number. Also returns `None` if the given key does not exist in the map /// or the given index is not within the bounds of the array. pub fn get_mut(&mut self, index: I) -> Option<&mut Self> { index.index_mut(self) } /// Extracts the integer value if it is an integer. pub fn as_integer(&self) -> Option { match *self { Self::Integer(i) => Some(i), _ => None, } } /// Tests whether this value is an integer. pub fn is_integer(&self) -> bool { self.as_integer().is_some() } /// Extracts the float value if it is a float. pub fn as_float(&self) -> Option { match *self { Self::Float(f) => Some(f), _ => None, } } /// Tests whether this value is a float. pub fn is_float(&self) -> bool { self.as_float().is_some() } /// Extracts the boolean value if it is a boolean. pub fn as_bool(&self) -> Option { match *self { Self::Boolean(b) => Some(b), _ => None, } } /// Tests whether this value is a boolean. pub fn is_bool(&self) -> bool { self.as_bool().is_some() } /// Extracts the string of this value if it is a string. pub fn as_str(&self) -> Option<&str> { match *self { Self::String(ref s) => Some(&**s), _ => None, } } /// Tests if this value is a string. pub fn is_str(&self) -> bool { self.as_str().is_some() } /// Extracts the datetime value if it is a datetime. /// /// Note that a parsed TOML value will only contain ISO 8601 dates. An /// example date is: /// /// ```notrust /// 1979-05-27T07:32:00Z /// ``` pub fn as_datetime(&self) -> Option<&Datetime> { match *self { Self::Datetime(ref s) => Some(s), _ => None, } } /// Tests whether this value is a datetime. pub fn is_datetime(&self) -> bool { self.as_datetime().is_some() } /// Extracts the array value if it is an array. pub fn as_array(&self) -> Option<&Vec> { match *self { Self::Array(ref s) => Some(s), _ => None, } } /// Extracts the array value if it is an array. pub fn as_array_mut(&mut self) -> Option<&mut Vec> { match *self { Self::Array(ref mut s) => Some(s), _ => None, } } /// Tests whether this value is an array. pub fn is_array(&self) -> bool { self.as_array().is_some() } /// Extracts the table value if it is a table. pub fn as_table(&self) -> Option<&Table> { match *self { Self::Table(ref s) => Some(s), _ => None, } } /// Extracts the table value if it is a table. pub fn as_table_mut(&mut self) -> Option<&mut Table> { match *self { Self::Table(ref mut s) => Some(s), _ => None, } } /// Tests whether this value is a table. pub fn is_table(&self) -> bool { self.as_table().is_some() } /// Tests whether this and another value have the same type. pub fn same_type(&self, other: &Self) -> bool { discriminant(self) == discriminant(other) } /// Returns a human-readable representation of the type of this value. pub fn type_str(&self) -> &'static str { match *self { Self::String(..) => "string", Self::Integer(..) => "integer", Self::Float(..) => "float", Self::Boolean(..) => "boolean", Self::Datetime(..) => "datetime", Self::Array(..) => "array", Self::Table(..) => "table", } } } impl ops::Index for Value where I: Index, { type Output = Self; fn index(&self, index: I) -> &Self { self.get(index).expect("index not found") } } impl ops::IndexMut for Value where I: Index, { fn index_mut(&mut self, index: I) -> &mut Self { self.get_mut(index).expect("index not found") } } impl<'a> From<&'a str> for Value { #[inline] fn from(val: &'a str) -> Self { Self::String(val.to_owned()) } } impl> From> for Value { fn from(val: Vec) -> Self { Self::Array(val.into_iter().map(|v| v.into()).collect()) } } impl, V: Into> From> for Value { fn from(val: BTreeMap) -> Self { let table = val.into_iter().map(|(s, v)| (s.into(), v.into())).collect(); Self::Table(table) } } #[cfg(feature = "std")] impl + Hash + Eq, V: Into> From> for Value { fn from(val: HashMap) -> Self { let table = val.into_iter().map(|(s, v)| (s.into(), v.into())).collect(); Self::Table(table) } } macro_rules! impl_into_value { ($variant:ident : $T:ty) => { impl From<$T> for Value { #[inline] fn from(val: $T) -> Value { Value::$variant(val.into()) } } }; } impl_into_value!(String: String); impl_into_value!(Integer: i64); impl_into_value!(Integer: i32); impl_into_value!(Integer: i8); impl_into_value!(Integer: u8); impl_into_value!(Integer: u32); impl_into_value!(Float: f64); impl_into_value!(Float: f32); impl_into_value!(Boolean: bool); impl_into_value!(Datetime: Datetime); impl_into_value!(Table: Table); /// Types that can be used to index a `toml::Value` /// /// Currently this is implemented for `usize` to index arrays and `str` to index /// tables. /// /// This trait is sealed and not intended for implementation outside of the /// `toml` crate. pub trait Index: Sealed { #[doc(hidden)] fn index<'a>(&self, val: &'a Value) -> Option<&'a Value>; #[doc(hidden)] fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value>; } /// An implementation detail that should not be implemented, this will change in /// the future and break code otherwise. #[doc(hidden)] pub trait Sealed {} impl Sealed for usize {} impl Sealed for str {} impl Sealed for String {} impl Sealed for &T {} impl Index for usize { fn index<'a>(&self, val: &'a Value) -> Option<&'a Value> { match *val { Value::Array(ref a) => a.get(*self), _ => None, } } fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> { match *val { Value::Array(ref mut a) => a.get_mut(*self), _ => None, } } } impl Index for str { fn index<'a>(&self, val: &'a Value) -> Option<&'a Value> { match *val { Value::Table(ref a) => a.get(self), _ => None, } } fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> { match *val { Value::Table(ref mut a) => a.get_mut(self), _ => None, } } } impl Index for String { fn index<'a>(&self, val: &'a Value) -> Option<&'a Value> { self[..].index(val) } fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> { self[..].index_mut(val) } } impl Index for &T where T: Index + ?Sized, { fn index<'a>(&self, val: &'a Value) -> Option<&'a Value> { (**self).index(val) } fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> { (**self).index_mut(val) } } #[cfg(feature = "display")] impl fmt::Display for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use serde_core::Serialize as _; let mut output = String::new(); let serializer = crate::ser::ValueSerializer::new(&mut output); self.serialize(serializer).unwrap(); output.fmt(f) } } #[cfg(feature = "parse")] impl core::str::FromStr for Value { type Err = crate::de::Error; fn from_str(s: &str) -> Result { use serde_core::Deserialize as _; Self::deserialize(crate::de::ValueDeserializer::parse(s)?) } } impl ser::Serialize for Value { fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { match *self { Self::String(ref s) => serializer.serialize_str(s), Self::Integer(i) => serializer.serialize_i64(i), Self::Float(f) => serializer.serialize_f64(f), Self::Boolean(b) => serializer.serialize_bool(b), Self::Datetime(ref s) => s.serialize(serializer), Self::Array(ref a) => a.serialize(serializer), Self::Table(ref t) => t.serialize(serializer), } } } impl<'de> de::Deserialize<'de> for Value { fn deserialize(deserializer: D) -> Result where D: de::Deserializer<'de>, { struct ValueVisitor; impl<'de> de::Visitor<'de> for ValueVisitor { type Value = Value; fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("any valid TOML value") } fn visit_bool(self, value: bool) -> Result { Ok(Value::Boolean(value)) } fn visit_i64(self, value: i64) -> Result { Ok(Value::Integer(value)) } fn visit_u64(self, value: u64) -> Result { if i64::try_from(value).is_ok() { Ok(Value::Integer(value as i64)) } else { Err(de::Error::custom("u64 value was too large")) } } fn visit_u32(self, value: u32) -> Result { Ok(Value::Integer(value.into())) } fn visit_i32(self, value: i32) -> Result { Ok(Value::Integer(value.into())) } fn visit_f64(self, value: f64) -> Result { Ok(Value::Float(value)) } fn visit_str(self, value: &str) -> Result { Ok(Value::String(value.into())) } fn visit_string(self, value: String) -> Result { Ok(Value::String(value)) } fn visit_some(self, deserializer: D) -> Result where D: de::Deserializer<'de>, { de::Deserialize::deserialize(deserializer) } fn visit_seq(self, mut visitor: V) -> Result where V: de::SeqAccess<'de>, { let mut vec = Vec::new(); while let Some(elem) = visitor.next_element()? { vec.push(elem); } Ok(Value::Array(vec)) } fn visit_map(self, mut visitor: V) -> Result where V: de::MapAccess<'de>, { let key = match toml_datetime::de::VisitMap::next_key_seed(&mut visitor)? { Some(toml_datetime::de::VisitMap::Datetime(datetime)) => { return Ok(Value::Datetime(datetime)); } None => return Ok(Value::Table(Table::new())), Some(toml_datetime::de::VisitMap::Key(key)) => key, }; let mut map = Table::new(); map.insert(key.into_owned(), visitor.next_value()?); while let Some(key) = visitor.next_key::()? { if let crate::map::Entry::Vacant(vacant) = map.entry(&key) { vacant.insert(visitor.next_value()?); } else { let msg = format!("duplicate key: `{key}`"); return Err(de::Error::custom(msg)); } } Ok(Value::Table(map)) } } deserializer.deserialize_any(ValueVisitor) } } // This is wrapped by `Table` and any trait methods implemented here need to be wrapped there. impl<'de> de::Deserializer<'de> for Value { type Error = crate::de::Error; fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { match self { Self::Boolean(v) => visitor.visit_bool(v), Self::Integer(n) => visitor.visit_i64(n), Self::Float(n) => visitor.visit_f64(n), Self::String(v) => visitor.visit_string(v), Self::Datetime(v) => visitor.visit_string(v.to_string()), Self::Array(v) => { let len = v.len(); let mut deserializer = SeqDeserializer::new(v); let seq = visitor.visit_seq(&mut deserializer)?; let remaining = deserializer.iter.len(); if remaining == 0 { Ok(seq) } else { Err(de::Error::invalid_length(len, &"fewer elements in array")) } } Self::Table(v) => { let len = v.len(); let mut deserializer = MapDeserializer::new(v); let map = visitor.visit_map(&mut deserializer)?; let remaining = deserializer.iter.len(); if remaining == 0 { Ok(map) } else { Err(de::Error::invalid_length(len, &"fewer elements in map")) } } } } #[inline] fn deserialize_enum( self, _name: &'static str, _variants: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { match self { Self::String(variant) => visitor.visit_enum(variant.into_deserializer()), Self::Table(variant) => { if variant.is_empty() { Err(crate::de::Error::custom( "wanted exactly 1 element, found 0 elements", None, )) } else if variant.len() != 1 { Err(crate::de::Error::custom( "wanted exactly 1 element, more than 1 element", None, )) } else { let deserializer = MapDeserializer::new(variant); visitor.visit_enum(deserializer) } } _ => Err(de::Error::invalid_type( de::Unexpected::UnitVariant, &"string only", )), } } // `None` is interpreted as a missing field so be sure to implement `Some` // as a present field. fn deserialize_option(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_some(self) } fn deserialize_newtype_struct( self, _name: &'static str, visitor: V, ) -> Result where V: de::Visitor<'de>, { visitor.visit_newtype_struct(self) } serde_core::forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq bytes byte_buf map unit_struct tuple_struct struct tuple ignored_any identifier } } pub(crate) struct SeqDeserializer { iter: vec::IntoIter, } impl SeqDeserializer { fn new(vec: Vec) -> Self { Self { iter: vec.into_iter(), } } } impl<'de> de::SeqAccess<'de> for SeqDeserializer { type Error = crate::de::Error; fn next_element_seed(&mut self, seed: T) -> Result, crate::de::Error> where T: de::DeserializeSeed<'de>, { match self.iter.next() { Some(value) => seed.deserialize(value).map(Some), None => Ok(None), } } fn size_hint(&self) -> Option { match self.iter.size_hint() { (lower, Some(upper)) if lower == upper => Some(upper), _ => None, } } } pub(crate) struct MapDeserializer { iter:
::IntoIter, value: Option<(String, Value)>, } impl MapDeserializer { fn new(map: Table) -> Self { Self { iter: map.into_iter(), value: None, } } } impl<'de> de::MapAccess<'de> for MapDeserializer { type Error = crate::de::Error; fn next_key_seed(&mut self, seed: T) -> Result, crate::de::Error> where T: de::DeserializeSeed<'de>, { match self.iter.next() { Some((key, value)) => { self.value = Some((key.clone(), value)); seed.deserialize(Value::String(key)).map(Some) } None => Ok(None), } } fn next_value_seed(&mut self, seed: T) -> Result where T: de::DeserializeSeed<'de>, { let (key, res) = match self.value.take() { Some((key, value)) => (key, seed.deserialize(value)), None => return Err(de::Error::custom("value is missing")), }; res.map_err(|mut error| { error.add_key(key); error }) } fn size_hint(&self) -> Option { match self.iter.size_hint() { (lower, Some(upper)) if lower == upper => Some(upper), _ => None, } } } impl<'de> de::EnumAccess<'de> for MapDeserializer { type Error = crate::de::Error; type Variant = MapEnumDeserializer; fn variant_seed(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> where V: de::DeserializeSeed<'de>, { use de::Error; let (key, value) = match self.iter.next() { Some(pair) => pair, None => { return Err(Error::custom( "expected table with exactly 1 entry, found empty table", )); } }; let val = seed.deserialize(key.into_deserializer())?; let variant = MapEnumDeserializer::new(value); Ok((val, variant)) } } /// Deserializes table values into enum variants. pub(crate) struct MapEnumDeserializer { value: Value, } impl MapEnumDeserializer { pub(crate) fn new(value: Value) -> Self { Self { value } } } impl<'de> de::VariantAccess<'de> for MapEnumDeserializer { type Error = crate::de::Error; fn unit_variant(self) -> Result<(), Self::Error> { use de::Error; match self.value { Value::Array(values) => { if values.is_empty() { Ok(()) } else { Err(Error::custom("expected empty array")) } } Value::Table(values) => { if values.is_empty() { Ok(()) } else { Err(Error::custom("expected empty table")) } } e => Err(Error::custom(format!( "expected table, found {}", e.type_str() ))), } } fn newtype_variant_seed(self, seed: T) -> Result where T: de::DeserializeSeed<'de>, { seed.deserialize(self.value.into_deserializer()) } fn tuple_variant(self, len: usize, visitor: V) -> Result where V: de::Visitor<'de>, { use de::Error; match self.value { Value::Array(values) => { if values.len() == len { de::Deserializer::deserialize_seq(values.into_deserializer(), visitor) } else { Err(Error::custom(format!("expected tuple with length {len}"))) } } Value::Table(values) => { let tuple_values: Result, _> = values .into_iter() .enumerate() .map(|(index, (key, value))| match key.parse::() { Ok(key_index) if key_index == index => Ok(value), Ok(_) | Err(_) => Err(Error::custom(format!( "expected table key `{index}`, but was `{key}`" ))), }) .collect(); let tuple_values = tuple_values?; if tuple_values.len() == len { de::Deserializer::deserialize_seq(tuple_values.into_deserializer(), visitor) } else { Err(Error::custom(format!("expected tuple with length {len}"))) } } e => Err(Error::custom(format!( "expected table, found {}", e.type_str() ))), } } fn struct_variant( self, fields: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { de::Deserializer::deserialize_struct( self.value.into_deserializer(), "", // TODO: this should be the variant name fields, visitor, ) } } impl IntoDeserializer<'_, crate::de::Error> for Value { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } pub(crate) struct ValueSerializer; impl ser::Serializer for ValueSerializer { type Ok = Value; type Error = crate::ser::Error; type SerializeSeq = ValueSerializeVec; type SerializeTuple = ValueSerializeVec; type SerializeTupleStruct = ValueSerializeVec; type SerializeTupleVariant = ValueSerializeTupleVariant; type SerializeMap = ValueSerializeMap; type SerializeStruct = ValueSerializeMap; type SerializeStructVariant = ValueSerializeStructVariant; fn serialize_bool(self, value: bool) -> Result { Ok(Value::Boolean(value)) } fn serialize_i8(self, value: i8) -> Result { self.serialize_i64(value.into()) } fn serialize_i16(self, value: i16) -> Result { self.serialize_i64(value.into()) } fn serialize_i32(self, value: i32) -> Result { self.serialize_i64(value.into()) } fn serialize_i64(self, value: i64) -> Result { Ok(Value::Integer(value)) } fn serialize_u8(self, value: u8) -> Result { self.serialize_i64(value.into()) } fn serialize_u16(self, value: u16) -> Result { self.serialize_i64(value.into()) } fn serialize_u32(self, value: u32) -> Result { self.serialize_i64(value.into()) } fn serialize_u64(self, value: u64) -> Result { if i64::try_from(value).is_ok() { self.serialize_i64(value as i64) } else { Err(ser::Error::custom("u64 value was too large")) } } fn serialize_f32(self, value: f32) -> Result { self.serialize_f64(value as f64) } fn serialize_f64(self, mut value: f64) -> Result { // Discard sign of NaN. See ValueSerializer::serialize_f64. if value.is_nan() { value = value.copysign(1.0); } Ok(Value::Float(value)) } fn serialize_char(self, value: char) -> Result { let mut s = String::new(); s.push(value); self.serialize_str(&s) } fn serialize_str(self, value: &str) -> Result { Ok(Value::String(value.to_owned())) } fn serialize_bytes(self, value: &[u8]) -> Result { let vec = value.iter().map(|&b| Value::Integer(b.into())).collect(); Ok(Value::Array(vec)) } fn serialize_unit(self) -> Result { Err(crate::ser::Error::unsupported_type(Some("unit"))) } fn serialize_unit_struct(self, name: &'static str) -> Result { Err(crate::ser::Error::unsupported_type(Some(name))) } fn serialize_unit_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, ) -> Result { self.serialize_str(_variant) } fn serialize_newtype_struct( self, _name: &'static str, value: &T, ) -> Result where T: ser::Serialize + ?Sized, { value.serialize(self) } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, value: &T, ) -> Result where T: ser::Serialize + ?Sized, { let value = value.serialize(Self)?; let mut table = Table::new(); table.insert(variant.to_owned(), value); Ok(table.into()) } fn serialize_none(self) -> Result { Err(crate::ser::Error::unsupported_none()) } fn serialize_some(self, value: &T) -> Result where T: ser::Serialize + ?Sized, { value.serialize(self) } fn serialize_seq(self, len: Option) -> Result { Ok(ValueSerializeVec { vec: Vec::with_capacity(len.unwrap_or(0)), }) } fn serialize_tuple(self, len: usize) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_struct( self, _name: &'static str, len: usize, ) -> Result { self.serialize_seq(Some(len)) } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize, ) -> Result { Ok(ValueSerializeTupleVariant::tuple(variant, len)) } fn serialize_map(self, _len: Option) -> Result { Ok(ValueSerializeMap { ser: crate::table::SerializeMap::new(), }) } fn serialize_struct( self, _name: &'static str, len: usize, ) -> Result { self.serialize_map(Some(len)) } fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize, ) -> Result { Ok(ValueSerializeStructVariant::struct_(variant, len)) } } pub(crate) struct ValueSerializeVec { vec: Vec, } impl ser::SerializeSeq for ValueSerializeVec { type Ok = Value; type Error = crate::ser::Error; fn serialize_element(&mut self, value: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { self.vec.push(Value::try_from(value)?); Ok(()) } fn end(self) -> Result { Ok(Value::Array(self.vec)) } } impl ser::SerializeTuple for ValueSerializeVec { type Ok = Value; type Error = crate::ser::Error; fn serialize_element(&mut self, value: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { ser::SerializeSeq::serialize_element(self, value) } fn end(self) -> Result { ser::SerializeSeq::end(self) } } impl ser::SerializeTupleStruct for ValueSerializeVec { type Ok = Value; type Error = crate::ser::Error; fn serialize_field(&mut self, value: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { ser::SerializeSeq::serialize_element(self, value) } fn end(self) -> Result { ser::SerializeSeq::end(self) } } impl ser::SerializeTupleVariant for ValueSerializeVec { type Ok = Value; type Error = crate::ser::Error; fn serialize_field(&mut self, value: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { ser::SerializeSeq::serialize_element(self, value) } fn end(self) -> Result { ser::SerializeSeq::end(self) } } pub(crate) struct ValueSerializeMap { ser: crate::table::SerializeMap, } impl ser::SerializeMap for ValueSerializeMap { type Ok = Value; type Error = crate::ser::Error; fn serialize_key(&mut self, key: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { self.ser.serialize_key(key) } fn serialize_value(&mut self, value: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { self.ser.serialize_value(value) } fn end(self) -> Result { self.ser.end().map(Value::Table) } } impl ser::SerializeStruct for ValueSerializeMap { type Ok = Value; type Error = crate::ser::Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), crate::ser::Error> where T: ser::Serialize + ?Sized, { ser::SerializeMap::serialize_key(self, key)?; ser::SerializeMap::serialize_value(self, value) } fn end(self) -> Result { ser::SerializeMap::end(self) } } type ValueSerializeTupleVariant = ValueSerializeVariant; type ValueSerializeStructVariant = ValueSerializeVariant; pub(crate) struct ValueSerializeVariant { variant: &'static str, inner: T, } impl ValueSerializeVariant { pub(crate) fn tuple(variant: &'static str, len: usize) -> Self { Self { variant, inner: ValueSerializeVec { vec: Vec::with_capacity(len), }, } } } impl ValueSerializeVariant { pub(crate) fn struct_(variant: &'static str, len: usize) -> Self { Self { variant, inner: ValueSerializeMap { ser: crate::table::SerializeMap::with_capacity(len), }, } } } impl ser::SerializeTupleVariant for ValueSerializeVariant { type Ok = Value; type Error = crate::ser::Error; fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> where T: ser::Serialize + ?Sized, { ser::SerializeSeq::serialize_element(&mut self.inner, value) } fn end(self) -> Result { let inner = ser::SerializeSeq::end(self.inner)?; let mut table = Table::new(); table.insert(self.variant.to_owned(), inner); Ok(Value::Table(table)) } } impl ser::SerializeStructVariant for ValueSerializeVariant { type Ok = Value; type Error = crate::ser::Error; #[inline] fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: ser::Serialize + ?Sized, { ser::SerializeStruct::serialize_field(&mut self.inner, key, value) } #[inline] fn end(self) -> Result { let inner = ser::SerializeStruct::end(self.inner)?; let mut table = Table::new(); table.insert(self.variant.to_owned(), inner); Ok(Value::Table(table)) } }