diff --git a/Cargo.lock b/Cargo.lock index 0677ac5..0251c31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,12 +42,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - [[package]] name = "android_system_properties" version = "0.1.5" @@ -119,6 +113,17 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "autocfg" version = "1.4.0" @@ -133,9 +138,9 @@ checksum = "d27c3610c36aee21ce8ac510e6224498de4228ad772a171ed65643a24693a5a8" [[package]] name = "base45" -version = "3.1.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3103f6d6af63726e86b026ac66769fc578ea38aba33f409e5855a4def97a564" +checksum = "240e56f4d3c453c36faacb695c535a4d5f8c7d23dac175014f32eb0a71012a03" [[package]] name = "base64" @@ -145,27 +150,33 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "base64-url" -version = "3.0.0" +version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38e2b6c78c06f7288d5e3c3d683bde35a79531127c83b087e5d0d77c974b4b28" +checksum = "261c4eaab63106ddf34d377f47e5428aa863b61e4a647c872778d9caf8e2c819" dependencies = [ "base64", ] [[package]] -name = "bitflags" -version = "2.9.0" +name = "bit-set" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec", +] [[package]] -name = "block-buffer" -version = "0.10.4" +name = "bit-vec" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" [[package]] name = "bumpalo" @@ -173,20 +184,27 @@ version = "3.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cc" -version = "1.2.16" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be714c154be609ec7f5dad223a33bf1482fff90472de28f7362806e6d4832b8c" +checksum = "6651c9ed80effdc7db0ff72512157f901af5e3549e341e24b1dd4887d836d838" dependencies = [ + "find-msvc-tools", "shlex", ] [[package]] name = "cddl" -version = "0.10.1" +version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4638392444f5f847788d1b91796d3b750dd0b2cc24c6c21aa7a23cc8fbf67fba" +checksum = "c5251326096ece604213e731457558294e178c9560fe3885436a1e465250edbe" dependencies = [ "abnf_to_pest", "base16", @@ -194,18 +212,22 @@ dependencies = [ "base64-url", "chrono", "ciborium", + "ciborium-io", + "ciborium-ll", "clap", "codespan-reporting", "console_error_panic_hook", - "crossterm", + "csv", "data-encoding", "displaydoc", + "fancy-regex", "hex", "hexf-parse", - "itertools 0.14.0", + "itertools 0.15.0", "js-sys", - "lexical-core", "log", + "pest", + "pest_derive", "pest_meta", "pest_vm", "regex", @@ -239,16 +261,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.40" +version = "0.4.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" dependencies = [ - "android-tzdata", "iana-time-zone", "js-sys", "num-traits", "wasm-bindgen", - "windows-link 0.1.0", + "windows-link", ] [[package]] @@ -320,9 +341,9 @@ checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" [[package]] name = "codespan-reporting" -version = "0.12.0" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe6d2e5af09e8c8ad56c969f2157a3d4238cebc7c55f0a517728c38f7b200f81" +checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681" dependencies = [ "serde", "termcolor", @@ -372,60 +393,37 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] -name = "cpufeatures" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" -dependencies = [ - "libc", -] - -[[package]] -name = "crossterm" -version = "0.28.1" +name = "crunchy" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6" -dependencies = [ - "bitflags", - "crossterm_winapi", - "mio", - "parking_lot", - "rustix 0.38.44", - "signal-hook", - "signal-hook-mio", - "winapi", -] +checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929" [[package]] -name = "crossterm_winapi" -version = "0.9.1" +name = "csv" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +checksum = "52cd9d68cf7efc6ddfaaee42e7288d3a99d613d4b50f76ce9827ae0c6e14f938" dependencies = [ - "winapi", + "csv-core", + "itoa", + "ryu", + "serde_core", ] [[package]] -name = "crunchy" -version = "0.2.3" +name = "csv-core" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929" - -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +checksum = "704a3c26996a80471189265814dbc2c257598b96b8a7feae2d31ace646bb9782" dependencies = [ - "generic-array", - "typenum", + "memchr", ] [[package]] name = "data-encoding" -version = "2.8.0" +version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "575f75dfd25738df5b91b8e43e14d44bda14637a58fae779fd2b064f8bf3e010" +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" [[package]] name = "deranged" @@ -436,16 +434,6 @@ dependencies = [ "powerfmt", ] -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", -] - [[package]] name = "displaydoc" version = "0.2.5" @@ -479,12 +467,29 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "fancy-regex" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1e1dacd0d2082dfcf1351c4bdd566bbe89a2b263235a2b50058f1e130a47277" +dependencies = [ + "bit-set", + "regex-automata", + "regex-syntax", +] + [[package]] name = "fastrand" version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" +[[package]] +name = "find-msvc-tools" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" + [[package]] name = "fnv" version = "1.0.7" @@ -492,13 +497,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] -name = "generic-array" -version = "0.14.7" +name = "futures-core" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ - "typenum", - "version_check", + "futures-core", + "futures-task", + "pin-project-lite", + "slab", ] [[package]] @@ -609,9 +628,9 @@ dependencies = [ [[package]] name = "itertools" -version = "0.14.0" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc" dependencies = [ "either", ] @@ -624,11 +643,12 @@ checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "js-sys" -version = "0.3.77" +version = "0.3.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +checksum = "7883d941dae510fb2d978fc3fe018c71c9e2892fd38854de3e8b92c2e5ad9cc5" dependencies = [ - "once_cell", + "cfg-if", + "futures-util", "wasm-bindgen", ] @@ -638,70 +658,6 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" -[[package]] -name = "lexical-core" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b765c31809609075565a70b4b71402281283aeda7ecaf4818ac14a7b2ade8958" -dependencies = [ - "lexical-parse-float", - "lexical-parse-integer", - "lexical-util", - "lexical-write-float", - "lexical-write-integer", -] - -[[package]] -name = "lexical-parse-float" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de6f9cb01fb0b08060209a057c048fcbab8717b4c1ecd2eac66ebfe39a65b0f2" -dependencies = [ - "lexical-parse-integer", - "lexical-util", - "static_assertions", -] - -[[package]] -name = "lexical-parse-integer" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72207aae22fc0a121ba7b6d479e42cbfea549af1479c3f3a4f12c70dd66df12e" -dependencies = [ - "lexical-util", - "static_assertions", -] - -[[package]] -name = "lexical-util" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a82e24bf537fd24c177ffbbdc6ebcc8d54732c35b50a3f28cc3f4e4c949a0b3" -dependencies = [ - "static_assertions", -] - -[[package]] -name = "lexical-write-float" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5afc668a27f460fb45a81a757b6bf2f43c2d7e30cb5a2dcd3abf294c78d62bd" -dependencies = [ - "lexical-util", - "lexical-write-integer", - "static_assertions", -] - -[[package]] -name = "lexical-write-integer" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "629ddff1a914a836fb245616a7888b62903aae58fa771e1d83943035efa0f978" -dependencies = [ - "lexical-util", - "static_assertions", -] - [[package]] name = "libc" version = "0.2.171" @@ -709,10 +665,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" [[package]] -name = "linux-raw-sys" -version = "0.4.15" +name = "libm" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" [[package]] name = "linux-raw-sys" @@ -720,21 +676,11 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" -[[package]] -name = "lock_api" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" -dependencies = [ - "autocfg", - "scopeguard", -] - [[package]] name = "log" -version = "0.4.26" +version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" [[package]] name = "memchr" @@ -744,9 +690,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "minicov" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27fe9f1cc3c22e1687f9446c2083c4c5fc7f0bcf1c7a86bdbded14985895b4b" +checksum = "4869b6a491569605d66d3952bcdf03df789e5b536e5f0cf7758a7f08a55ae24d" dependencies = [ "cc", "walkdir", @@ -758,18 +704,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" -[[package]] -name = "mio" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c" -dependencies = [ - "libc", - "log", - "wasi", - "windows-sys 0.59.0", -] - [[package]] name = "nom" version = "7.1.3" @@ -780,6 +714,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -793,6 +736,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -811,59 +755,71 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d75b0bedcc4fe52caa0e03d9f1151a323e4aa5e2d78ba3580400cd3c9e2bc4bc" [[package]] -name = "parking_lot" -version = "0.12.3" +name = "oorandom" +version = "11.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + +[[package]] +name = "pest" +version = "2.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd47a460a9cbc3b3429c7e22bf0d76653e779319bae45a499ca010c9a990bb23" dependencies = [ - "lock_api", - "parking_lot_core", + "memchr", + "psm", + "stacker", + "ucd-trie", ] [[package]] -name = "parking_lot_core" -version = "0.9.10" +name = "pest_derive" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +checksum = "203e46697708f7519ab1608393cf89239cafb46b8e6d08a8ffe359359b11ef44" dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets", + "pest", + "pest_generator", ] [[package]] -name = "pest" -version = "2.8.1" +name = "pest_generator" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1db05f56d34358a8b1066f67cbb203ee3e7ed2ba674a6263a1d5ec6db2204323" +checksum = "e84ae6340a458d3d6cfc73cf328cf3cfe90b2484cd0de25eb66aa954615ed4d5" dependencies = [ - "memchr", - "thiserror", - "ucd-trie", + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn 3.0.6", ] [[package]] name = "pest_meta" -version = "2.8.1" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edd1101f170f5903fde0914f899bb503d9ff5271d7ba76bbb70bea63690cc0d5" +checksum = "b57f92f32b3dbce6598fd8f5cdf1f06a6072570b8103fa21728fa9036923e710" dependencies = [ "pest", - "sha2", ] [[package]] name = "pest_vm" -version = "2.8.1" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "267f825394d1787eb30fc82b1e5903b4a6eb600a78322f91f412dd189a6005e0" +checksum = "2ffe3bf2f6db355c70e7fff71429b1185f2018da4b86867aa105ad0aa5c99234" dependencies = [ "pest", "pest_meta", ] +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + [[package]] name = "powerfmt" version = "0.2.0" @@ -891,6 +847,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "psm" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "200b9ff220857e53e184257720a14553b2f4aa02577d2ed9842d45d4b9654810" +dependencies = [ + "cc", +] + [[package]] name = "quote" version = "1.0.47" @@ -906,20 +871,11 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" -[[package]] -name = "redox_syscall" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1" -dependencies = [ - "bitflags", -] - [[package]] name = "regex" -version = "1.11.1" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" dependencies = [ "aho-corasick", "memchr", @@ -929,9 +885,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.9" +version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" dependencies = [ "aho-corasick", "memchr", @@ -940,22 +896,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" - -[[package]] -name = "rustix" -version = "0.38.44" +version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys 0.4.15", - "windows-sys 0.59.0", -] +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" [[package]] name = "rustix" @@ -966,8 +909,8 @@ dependencies = [ "bitflags", "errno", "libc", - "linux-raw-sys 0.11.0", - "windows-sys 0.59.0", + "linux-raw-sys", + "windows-sys 0.61.2", ] [[package]] @@ -991,18 +934,13 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - [[package]] name = "serde" -version = "1.0.219" +version = "1.0.229" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" dependencies = [ + "serde_core", "serde_derive", ] @@ -1017,75 +955,44 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + [[package]] name = "serde_derive" -version = "1.0.219" +version = "1.0.229" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" dependencies = [ "proc-macro2", "quote", - "syn 2.0.100", + "syn 3.0.6", ] [[package]] name = "serde_json" -version = "1.0.140" +version = "1.0.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" dependencies = [ "itoa", "memchr", - "ryu", "serde", -] - -[[package]] -name = "sha2" -version = "0.10.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", + "serde_core", + "zmij", ] [[package]] name = "shlex" -version = "1.3.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "signal-hook" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" -dependencies = [ - "libc", - "signal-hook-registry", -] - -[[package]] -name = "signal-hook-mio" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd" -dependencies = [ - "libc", - "mio", - "signal-hook", -] - -[[package]] -name = "signal-hook-registry" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" -dependencies = [ - "libc", -] +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" [[package]] name = "similar" @@ -1105,16 +1012,23 @@ dependencies = [ ] [[package]] -name = "smallvec" -version = "1.14.0" +name = "slab" +version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" [[package]] -name = "static_assertions" -version = "1.1.0" +name = "stacker" +version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +checksum = "707f49d46706bacf8a2b00d51dace3f9de527c13eec3778f570c411f89e69967" +dependencies = [ + "cc", + "cfg-if", + "libc", + "psm", + "windows-sys 0.61.2", +] [[package]] name = "strsim" @@ -1153,8 +1067,8 @@ dependencies = [ "fastrand", "getrandom", "once_cell", - "rustix 1.1.2", - "windows-sys 0.59.0", + "rustix", + "windows-sys 0.61.2", ] [[package]] @@ -1166,26 +1080,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "thiserror" -version = "2.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "2.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.100", -] - [[package]] name = "time" version = "0.3.44" @@ -1220,16 +1114,19 @@ dependencies = [ ] [[package]] -name = "typed-arena" -version = "2.0.2" +name = "tokio" +version = "1.53.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" +checksum = "ce3335fa71841cda333a58d7615b03901380ecf09d59b3296d21f8bbac0dde4e" +dependencies = [ + "pin-project-lite", +] [[package]] -name = "typenum" -version = "1.18.0" +name = "typed-arena" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" +checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" [[package]] name = "ucd-trie" @@ -1271,12 +1168,6 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - [[package]] name = "walkdir" version = "2.5.0" @@ -1287,12 +1178,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - [[package]] name = "wasip2" version = "1.0.2+wasi-0.2.9" @@ -1304,48 +1189,33 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.100" +version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409" dependencies = [ "cfg-if", "once_cell", "rustversion", "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" -dependencies = [ - "bumpalo", - "log", - "proc-macro2", - "quote", - "syn 2.0.100", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.50" +version = "0.4.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "555d470ec0bc3bb57890405e5d4322cc9ea83cebb085523ced7be4144dac1e61" +checksum = "3cbab34de2d982e9b48e18d216d04c4a6f641066ff19ffb699980f591ee3610e" dependencies = [ - "cfg-if", "js-sys", - "once_cell", + "tokio", "wasm-bindgen", - "web-sys", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.100" +version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +checksum = "2e29d0c35b16e224a7eeb5cd2d25e3e1968fbd65604117b44d3b789d00ee8535" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1353,76 +1223,75 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.100" +version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +checksum = "6f501a8bc3719dba86ef8ae4728879c08001bea749eb1333ac5b91e040e2a6b7" dependencies = [ + "bumpalo", "proc-macro2", "quote", - "syn 2.0.100", - "wasm-bindgen-backend", + "syn 3.0.6", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.100" +version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +checksum = "23f0c9c52aa7cd7d77769a4cfe2a9adb1b331f489a41d912ce14513d5ab995c6" dependencies = [ "unicode-ident", ] [[package]] name = "wasm-bindgen-test" -version = "0.3.50" +version = "0.3.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66c8d5e33ca3b6d9fa3b4676d774c5778031d27a578c2b007f905acf816152c3" +checksum = "ae7499dfd45780a0a91d7ee6bb9ac51970a4479a41a89da443fdda5a39547d42" dependencies = [ + "async-trait", + "cast", "js-sys", + "libm", "minicov", + "nu-ansi-term", + "num-traits", + "oorandom", + "serde", + "serde_json", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test-macro", + "wasm-bindgen-test-shared", ] [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.50" +version = "0.3.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17d5042cc5fa009658f9a7333ef24291b1291a25b6382dd68862a7f3b969f69b" +checksum = "3b84b5ac638bfb168196a1a461fcc8f46a294a18b1b6be52133b4e0db122cc9f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.100", + "syn 3.0.6", ] [[package]] -name = "web-sys" -version = "0.3.77" +name = "wasm-bindgen-test-shared" +version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" -dependencies = [ - "js-sys", - "wasm-bindgen", -] +checksum = "4f692aa943ccd88363733b77063f32cfed5bc6cbea8e6e8b251b302f881606fe" [[package]] -name = "winapi" -version = "0.3.9" +name = "web-sys" +version = "0.3.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +checksum = "88261b9deccee56594c11a3460c462c41f58d148598fe70ad77070126a68aba4" dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", + "js-sys", + "wasm-bindgen", ] -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - [[package]] name = "winapi-util" version = "0.1.9" @@ -1432,12 +1301,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - [[package]] name = "windows-core" version = "0.52.0" @@ -1447,12 +1310,6 @@ dependencies = [ "windows-targets", ] -[[package]] -name = "windows-link" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6dccfd733ce2b1753b03b6d3c65edf020262ea35e20ccdf3e288043e6dd620e3" - [[package]] name = "windows-link" version = "0.2.1" @@ -1474,7 +1331,7 @@ version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ - "windows-link 0.2.1", + "windows-link", ] [[package]] @@ -1546,3 +1403,9 @@ name = "wit-bindgen" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/Cargo.toml b/Cargo.toml index 51c9849..0c39c13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ vector_groups = [] [dependencies] anyhow = "1.0.102" -cddl = "=0.10.1" +cddl = "=0.10.7" clap = { version = "4.5.60", features = ["derive"] } convert_case = "0.12.0" diff --git a/examples/string_literals.cddl b/examples/string_literals.cddl index ac2fc49..48946d7 100644 --- a/examples/string_literals.cddl +++ b/examples/string_literals.cddl @@ -1,8 +1,18 @@ quoted-bytes = 'say "hello"' +escaped-bytes = 'a \\ backslash \n' +hex-bytes = h'FF' +hex-backslash = h'5C6E' text-escapes = "quote \" backslash \\ newline \n unicode \u00e9" glob-pattern = tstr .default "src/**/*.ts" +version = tstr .regexp "\\d+\\.\\d+" +word = tstr .regexp "\\bword\\b" headers = { - 'content-type': tstr, + 'content-type' => tstr, "x-\"custom\"": tstr, } greeting = "hi" / "hello \"there\"" / 'say "hey"' +quoted-enum = "say \"hi\"" / "back\\slash" +codes = { + 1 => tstr, + "na\\me" => tstr, +} diff --git a/src/engines/typescript.rs b/src/engines/typescript.rs index 3dd2e17..e2fe6fd 100644 --- a/src/engines/typescript.rs +++ b/src/engines/typescript.rs @@ -19,7 +19,9 @@ use std::io::Write; use cddl::{ast::Occurrence, visitor::Visitor, Error}; use crate::util::{ - escape_js_string, is_enum_value, split_namespaced, to_namespaced, to_pascalcase, + array_group_entry_as_type_entry, byte_value_to_js_string, collect_group_rules, escape_js_string, + is_enum_value, is_non_cut_non_literal_member_key, member_key_literal_value, split_namespaced, + to_namespaced, to_pascalcase, type_rule_as_group_rule, unwrap_single_ident_parens, }; const MAX_ELEMENTS: usize = 1 << 3; @@ -92,6 +94,7 @@ where Stderr: Write, { in_comment: bool, + group_rules: std::collections::HashSet, nested_group_choices: Vec, nested_type1: Vec, #[allow(dead_code)] @@ -118,6 +121,7 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine { pub fn with_writers(stdout: Stdout, stderr: Stderr) -> Engine { Engine { in_comment: false, + group_rules: std::collections::HashSet::new(), nested_group_choices: Vec::new(), nested_type1: Vec::new(), postamble_options: PostambleOptions { @@ -150,7 +154,7 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine { self.stdout, "{}: \"{}\",", to_pascalcase(value), - escape_js_string(value.to_string()) + escape_js_string(value) ); } else { panic!("Called `visit_enum_type` on non-textual type"); @@ -357,6 +361,9 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine { ) -> cddl::visitor::Result { self.print_group_joiner(); self.enter_array(); + if let Some(ge) = array_group_entry_as_type_entry(entry, &self.group_rules) { + return self.visit_value_array_member_key_entry(&ge); + } match entry { cddl::ast::GroupEntry::ValueMemberKey { ge, .. } => { self.visit_value_array_member_key_entry(ge)?; @@ -546,7 +553,14 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine }; Ok(()) } + fn visit_cddl(&mut self, cddl: &'b cddl::ast::CDDL<'a>) -> cddl::visitor::Result { + self.group_rules = collect_group_rules(cddl); + cddl::visitor::walk_cddl(self, cddl) + } fn visit_type_rule(&mut self, tr: &'b cddl::ast::TypeRule<'a>) -> cddl::visitor::Result { + if let Some(gr) = type_rule_as_group_rule(tr, &self.group_rules) { + return self.visit_group_rule(&gr); + } let (namespaces, type_name) = split_namespaced(&tr.name); for namespace in &namespaces { writeln!(self.stdout, "export namespace {} {{", namespace); @@ -590,7 +604,7 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine &tr.generic_params, )?; write!(self.stdout, " = "); - self.visit_type(&tr.value)?; + self.visit_type(unwrap_single_ident_parens(&tr.value))?; writeln!(self.stdout, ";"); } for _ in &namespaces { @@ -722,7 +736,7 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine self.visit_memberkey(&mk)?; if matches!(calculate_occurrence(&entry.occur), (0, max) if max > 0) - && !matches!(&mk, cddl::ast::MemberKey::Type1 { is_cut: false, .. }) + && !is_non_cut_non_literal_member_key(&mk) { write!(self.stdout, "?"); } @@ -775,6 +789,20 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine &mut self, mk: &'b cddl::ast::MemberKey<'a>, ) -> cddl::visitor::Result { + if let Some(value) = member_key_literal_value(mk) { + match value { + cddl::token::Value::INT(value) => write!(self.stdout, "\"{}\"", value), + cddl::token::Value::UINT(value) => write!(self.stdout, "\"{}\"", value), + cddl::token::Value::FLOAT(value) => write!(self.stdout, "\"{}\"", value), + cddl::token::Value::TEXT(value) => { + write!(self.stdout, "\"{}\"", escape_js_string(value)) + } + cddl::token::Value::BYTE(value) => { + write!(self.stdout, "\"{}\"", byte_value_to_js_string(&value)) + } + }; + return Ok(()); + } match mk { cddl::ast::MemberKey::Type1 { t1, is_cut, .. } => { write!(self.stdout, "["); @@ -789,18 +817,8 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine cddl::ast::MemberKey::Bareword { ident, .. } => { write!(self.stdout, "\"{}\"", &ident); } - cddl::ast::MemberKey::Value { value, .. } => { - match value { - cddl::token::Value::INT(value) => write!(self.stdout, "\"{}\"", value), - cddl::token::Value::UINT(value) => write!(self.stdout, "\"{}\"", value), - cddl::token::Value::FLOAT(value) => write!(self.stdout, "\"{}\"", value), - cddl::token::Value::TEXT(value) => { - write!(self.stdout, "\"{}\"", escape_js_string(value.to_string())) - } - cddl::token::Value::BYTE(value) => { - write!(self.stdout, "\"{}\"", escape_js_string(value.to_string())) - } - }; + cddl::ast::MemberKey::Value { .. } => { + unreachable!() } cddl::ast::MemberKey::NonMemberKey { .. } => { unimplemented!() @@ -882,10 +900,10 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine cddl::token::Value::UINT(value) => write!(self.stdout, "{}", value), cddl::token::Value::FLOAT(value) => write!(self.stdout, "{}", value), cddl::token::Value::TEXT(value) => { - write!(self.stdout, "\"{}\"", escape_js_string(value.to_string())) + write!(self.stdout, "\"{}\"", escape_js_string(value)) } cddl::token::Value::BYTE(value) => { - write!(self.stdout, "\"{}\"", escape_js_string(value.to_string())) + write!(self.stdout, "\"{}\"", byte_value_to_js_string(value)) } }; } diff --git a/src/engines/zod.rs b/src/engines/zod.rs index e0b4642..d70a03e 100644 --- a/src/engines/zod.rs +++ b/src/engines/zod.rs @@ -18,7 +18,11 @@ use std::io::Write; use cddl::{ast::Occurrence, visitor::Visitor, Error}; -use crate::util::{escape_js_string, split_namespaced, to_namespaced}; +use crate::util::{ + array_group_entry_as_type_entry, byte_value_to_js_string, collect_group_rules, escape_js_string, + is_non_cut_non_literal_member_key, member_key_literal_value, split_namespaced, to_namespaced, + type_rule_as_group_rule, +}; const MAX_ARRAYS: usize = 1 << 3; @@ -55,6 +59,7 @@ where nested_type1: Vec, is_type_mode: bool, cyclic_rules: std::collections::HashSet, + group_rules: std::collections::HashSet, #[allow(dead_code)] postamble_options: PostambleOptions, stdout: Stdout, @@ -83,6 +88,7 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine { nested_type1: Vec::new(), is_type_mode: false, cyclic_rules: std::collections::HashSet::new(), + group_rules: std::collections::HashSet::new(), postamble_options: PostambleOptions { #[cfg(feature = "vector_groups")] print_flatten: false, @@ -116,7 +122,7 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine { write!(self.stdout, "z.ZodEnum<{{"); for type2 in t.type_choices.iter().map(|choice| &choice.type1.type2) { if let cddl::ast::Type2::TextValue { value, .. } = type2 { - let value = escape_js_string(value.to_string()); + let value = escape_js_string(value); write!(self.stdout, "\"{}\":\"{}\",", value, value); } } @@ -125,7 +131,7 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine { write!(self.stdout, "z.enum(["); for type2 in t.type_choices.iter().map(|choice| &choice.type1.type2) { if let cddl::ast::Type2::TextValue { value, .. } = type2 { - write!(self.stdout, "\"{}\",", escape_js_string(value.to_string())); + write!(self.stdout, "\"{}\",", escape_js_string(value)); } } write!(self.stdout, "])"); @@ -309,6 +315,9 @@ impl<'a, 'b: 'a, 'c, Stdout: Write, Stderr: Write> Engine { &mut self, entry: &'b cddl::ast::GroupEntry<'a>, ) -> cddl::visitor::Result { + if let Some(ge) = array_group_entry_as_type_entry(entry, &self.group_rules) { + return self.visit_value_array_member_key_entry(&ge); + } match entry { cddl::ast::GroupEntry::ValueMemberKey { ge, .. } => { self.visit_value_array_member_key_entry(ge)?; @@ -541,9 +550,13 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine } fn visit_cddl(&mut self, cddl: &'b cddl::ast::CDDL<'a>) -> cddl::visitor::Result { self.cyclic_rules = find_cyclic_rules(cddl); + self.group_rules = collect_group_rules(cddl); cddl::visitor::walk_cddl(self, cddl) } fn visit_type_rule(&mut self, tr: &'b cddl::ast::TypeRule<'a>) -> cddl::visitor::Result { + if let Some(gr) = type_rule_as_group_rule(tr, &self.group_rules) { + return self.visit_group_rule(&gr); + } let (namespaces, type_name) = split_namespaced(&tr.name); for namespace in &namespaces { writeln!(self.stdout, "export namespace {} {{", namespace); @@ -728,7 +741,7 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine )); self.visit_memberkey(&mk)?; let is_optional = matches!(calculate_occurrence(&entry.occur), (0, max) if max > 0) - && !matches!(&mk, cddl::ast::MemberKey::Type1 { is_cut: false, .. }); + && !is_non_cut_non_literal_member_key(&mk); if self.is_type_mode && is_optional { write!(self.stdout, "z.ZodOptional<"); } @@ -804,6 +817,22 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine &mut self, mk: &'b cddl::ast::MemberKey<'a>, ) -> cddl::visitor::Result { + if let Some(value) = member_key_literal_value(mk) { + self.print_group_joiner(); + self.enter_map(); + match value { + cddl::token::Value::INT(value) => write!(self.stdout, "\"{}\":", value), + cddl::token::Value::UINT(value) => write!(self.stdout, "\"{}\":", value), + cddl::token::Value::FLOAT(value) => write!(self.stdout, "\"{}\":", value), + cddl::token::Value::TEXT(value) => { + write!(self.stdout, "\"{}\":", escape_js_string(value)) + } + cddl::token::Value::BYTE(value) => { + write!(self.stdout, "\"{}\":", byte_value_to_js_string(&value)) + } + }; + return Ok(()); + } match mk { cddl::ast::MemberKey::Type1 { t1, .. } => { self.exit_map(); @@ -817,20 +846,8 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine self.enter_map(); write!(self.stdout, "\"{}\":", &ident); } - cddl::ast::MemberKey::Value { value, .. } => { - self.print_group_joiner(); - self.enter_map(); - match value { - cddl::token::Value::INT(value) => write!(self.stdout, "\"{}\":", value), - cddl::token::Value::UINT(value) => write!(self.stdout, "\"{}\":", value), - cddl::token::Value::FLOAT(value) => write!(self.stdout, "\"{}\":", value), - cddl::token::Value::TEXT(value) => { - write!(self.stdout, "\"{}\":", escape_js_string(value.to_string())) - } - cddl::token::Value::BYTE(value) => { - write!(self.stdout, "\"{}\":", escape_js_string(value.to_string())) - } - }; + cddl::ast::MemberKey::Value { .. } => { + unreachable!() } cddl::ast::MemberKey::NonMemberKey { .. } => { unimplemented!() @@ -998,14 +1015,14 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine write!( self.stdout, "z.ZodLiteral<\"{}\">", - escape_js_string(value.to_string()) + escape_js_string(value) ) } cddl::token::Value::BYTE(value) => { write!( self.stdout, "z.ZodLiteral<\"{}\">", - escape_js_string(value.to_string()) + byte_value_to_js_string(value) ) } }, @@ -1029,12 +1046,12 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine cddl::token::Value::TEXT(value) => write!( self.stdout, "z.literal(\"{}\")", - escape_js_string(value.to_string()) + escape_js_string(value) ), cddl::token::Value::BYTE(value) => write!( self.stdout, "z.literal(\"{}\")", - escape_js_string(value.to_string()) + byte_value_to_js_string(value) ), }, ValueMode::Generic => match value { @@ -1051,10 +1068,10 @@ impl<'a, 'b: 'a, Stdout: Write, Stderr: Write> Visitor<'a, 'b, Error> for Engine cddl::token::Value::UINT(value) => write!(self.stdout, "{}", value), cddl::token::Value::FLOAT(value) => write!(self.stdout, "{}", value), cddl::token::Value::TEXT(value) => { - write!(self.stdout, "\"{}\"", escape_js_string(value.to_string())) + write!(self.stdout, "\"{}\"", escape_js_string(value)) } cddl::token::Value::BYTE(value) => { - write!(self.stdout, "\"{}\"", escape_js_string(value.to_string())) + write!(self.stdout, "\"{}\"", byte_value_to_js_string(value)) } }, }; @@ -1072,17 +1089,27 @@ fn count_group_choice_items(gc: &cddl::ast::GroupChoice) -> usize { match entry { cddl::ast::GroupEntry::ValueMemberKey { ge, .. } => { let mk = ge.member_key.as_ref().unwrap(); + if member_key_literal_value(mk).is_some() { + if !in_object { + in_object = true; + count += 1; + } + continue; + } match mk { cddl::ast::MemberKey::Type1 { .. } => { in_object = false; count += 1; } - cddl::ast::MemberKey::Bareword { .. } | cddl::ast::MemberKey::Value { .. } => { + cddl::ast::MemberKey::Bareword { .. } => { if !in_object { in_object = true; count += 1; } } + cddl::ast::MemberKey::Value { .. } => { + unreachable!() + } cddl::ast::MemberKey::NonMemberKey { .. } => { unimplemented!() } diff --git a/src/util.rs b/src/util.rs index c1d178e..cb6cc7d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -42,46 +42,99 @@ pub fn split_namespaced(value: T) -> (Vec, String) { /// Escapes `value` for use between double quotes in a generated JavaScript or /// TypeScript string literal (or string literal type). /// -/// The CDDL lexer keeps escape sequences such as `\"`, `\n` and `\u00e9` in the -/// raw value. These sequences are also valid in JavaScript, so this function -/// keeps them as-is. It escapes other characters as follows: +/// `value` must be the decoded string. cddl 0.10.7 resolves escape sequences +/// in text literals at parse time, so every `\` here is a literal backslash. +/// Use [`escape_js_raw_string`] for values that still hold raw CDDL source, +/// such as single-quoted byte strings. /// -/// - An unpaired `\` becomes `\\`. -/// - A raw `"` becomes `\"`. +/// - `\` becomes `\\`. +/// - `"` becomes `\"`. /// - A `/` after `*` becomes `\/`, so the value can appear in a doc comment. /// - Line terminators and control characters become escape sequences. pub fn escape_js_string>(value: T) -> String { + let value = value.as_ref(); + let mut out = String::with_capacity(value.len()); + for ch in value.chars() { + if ch == '\\' { + out.push_str("\\\\"); + } else { + push_escaped_char(&mut out, ch); + } + } + out +} + +/// Like [`escape_js_string`], but for raw CDDL source text. +/// +/// The parser does not decode single-quoted byte strings (`'...'`), so they +/// keep escape sequences such as `\"`, `\n` and `\u00e9`. These sequences are +/// also valid in JavaScript, so this function keeps them as-is. Only a `\` +/// that does not start a valid escape sequence becomes `\\`. +/// +/// The parser does decode `h'...'` byte strings. Use +/// [`byte_value_to_js_string`] to handle any byte string. +pub fn escape_js_raw_string>(value: T) -> String { let chars: Vec = value.as_ref().chars().collect(); let mut out = String::with_capacity(chars.len()); let mut i = 0; while i < chars.len() { let ch = chars[i]; - match ch { - '\\' => { - let len = valid_escape_len(&chars[i + 1..]); - if len > 0 { - out.push('\\'); - out.extend(&chars[i + 1..i + 1 + len]); - i += len; - } else { - out.push_str("\\\\"); - } + if ch == '\\' { + let len = valid_escape_len(&chars[i + 1..]); + if len > 0 { + out.push('\\'); + out.extend(&chars[i + 1..i + 1 + len]); + i += len; + } else { + out.push_str("\\\\"); } - '"' => out.push_str("\\\""), - '/' if out.ends_with('*') => out.push_str("\\/"), - '\n' => out.push_str("\\n"), - '\r' => out.push_str("\\r"), - '\t' => out.push_str("\\t"), - c if c.is_control() || c == '\u{2028}' || c == '\u{2029}' => { - out.push_str(&format!("\\u{:04x}", c as u32)); - } - c => out.push(c), + } else { + push_escaped_char(&mut out, ch); } i += 1; } out } +/// Returns `value` in CDDL notation, escaped for use between double quotes in +/// a generated JavaScript or TypeScript string literal. +/// +/// This does not use the `Display` impl of `ByteValue`: cddl 0.10.7 stores +/// `h'...'` values as decoded bytes, and `Display` fails if they are not valid +/// UTF-8. +pub(crate) fn byte_value_to_js_string(value: &cddl::token::ByteValue) -> String { + match value { + // Raw source text between the quotes. + cddl::token::ByteValue::UTF8(bytes) => { + escape_js_raw_string(format!("'{}'", String::from_utf8_lossy(bytes))) + } + // Decoded bytes. Encode them again, as in the CDDL source. + cddl::token::ByteValue::B16(bytes) => { + let hex: String = bytes.iter().map(|byte| format!("{:02X}", byte)).collect(); + format!("h'{}'", hex) + } + cddl::token::ByteValue::B64(_) => { + unreachable!("cddl 0.10.7 does not produce ByteValue::B64") + } + } +} + +/// Appends `ch` to `out`, escaped for a double-quoted JavaScript string. +/// The callers handle `\`. +fn push_escaped_char(out: &mut String, ch: char) { + match ch { + '"' => out.push_str("\\\""), + '/' if out.ends_with('*') => out.push_str("\\/"), + '\n' => out.push_str("\\n"), + '\r' => out.push_str("\\r"), + '\t' => out.push_str("\\t"), + c if c.is_control() || c == '\u{2028}' || c == '\u{2029}' => { + out.push_str(&format!("\\u{:04x}", c as u32)); + } + c => out.push(c), + } +} + /// Returns the number of characters after a `\` that form a valid JavaScript /// escape sequence, or 0 if they do not. fn valid_escape_len(rest: &[char]) -> usize { @@ -131,6 +184,301 @@ pub fn is_enum_value>(value: T) -> bool { && chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '_') } +/// Returns the names of all group rules, including `Name = ( SingleIdent )` rules that +/// cddl 0.10.7 parses as `Rule::Type(ParenthesizedType)` when `SingleIdent` is a group. +pub(crate) fn collect_group_rules(cddl: &cddl::ast::CDDL) -> std::collections::HashSet { + let mut group_rules = std::collections::HashSet::new(); + for rule in &cddl.rules { + if let cddl::ast::Rule::Group { rule, .. } = rule { + group_rules.insert(rule.name.ident.to_string()); + } + } + loop { + let mut changed = false; + for rule in &cddl.rules { + if let cddl::ast::Rule::Type { rule, .. } = rule { + if !rule.is_type_choice_alternate + && !group_rules.contains(rule.name.ident) + && type_is_group_ref(&rule.value, &group_rules) + { + group_rules.insert(rule.name.ident.to_string()); + changed = true; + } + } + } + if !changed { + break; + } + } + group_rules +} + +fn type_is_group_ref(t: &cddl::ast::Type, group_rules: &std::collections::HashSet) -> bool { + if t.type_choices.len() != 1 { + return false; + } + let t1 = &t.type_choices[0].type1; + if t1.operator.is_some() { + return false; + } + match &t1.type2 { + cddl::ast::Type2::Typename { ident, .. } => { + group_rules.contains(ident.ident) + || matches!(ident.socket, Some(cddl::token::SocketPlug::GROUP)) + } + cddl::ast::Type2::ParenthesizedType { pt, .. } => type_is_group_ref(pt, group_rules), + _ => false, + } +} + +/// Converts a `Name = ( SingleIdent )` rule, which cddl 0.10.7 parses as +/// `Rule::Type(ParenthesizedType)`, back to a group rule. +pub(crate) fn type_rule_as_group_rule<'a>( + tr: &cddl::ast::TypeRule<'a>, + group_rules: &std::collections::HashSet, +) -> Option> { + if tr.is_type_choice_alternate || !group_rules.contains(tr.name.ident) { + return None; + } + let entry = type_to_group_entry(&tr.value)?; + Some(cddl::ast::GroupRule { + name: tr.name.clone(), + generic_params: tr.generic_params.clone(), + is_group_choice_alternate: false, + entry, + comments_before_assigng: tr.comments_before_assignt.clone(), + comments_after_assigng: tr.comments_after_assignt.clone(), + }) +} + +/// Unwraps the outer `Type2::ParenthesizedType` of a `Name = ( SingleIdent )` rule, which +/// cddl 0.10.7 parses as a parenthesized type instead of a group rule. +pub(crate) fn unwrap_single_ident_parens<'a, 'b>( + t: &'b cddl::ast::Type<'a>, +) -> &'b cddl::ast::Type<'a> { + if let [choice] = t.type_choices.as_slice() { + if choice.type1.operator.is_none() { + if let cddl::ast::Type2::ParenthesizedType { pt, .. } = &choice.type1.type2 { + if let [inner] = pt.type_choices.as_slice() { + if inner.type1.operator.is_none() + && matches!(inner.type1.type2, cddl::ast::Type2::Typename { .. }) + { + return pt; + } + } + } + } + } + t +} + +fn type_to_group_entry<'a>(t: &cddl::ast::Type<'a>) -> Option> { + if t.type_choices.len() != 1 { + return None; + } + let t1 = &t.type_choices[0].type1; + if t1.operator.is_some() { + return None; + } + match &t1.type2 { + cddl::ast::Type2::Typename { + ident, + generic_args, + span, + } => Some(cddl::ast::GroupEntry::TypeGroupname { + ge: cddl::ast::TypeGroupnameEntry { + occur: None, + name: ident.clone(), + generic_args: generic_args.clone(), + }, + leading_comments: None, + trailing_comments: None, + span: *span, + }), + cddl::ast::Type2::ParenthesizedType { + pt, + comments_before_type, + comments_after_type, + span, + } => { + let inner_entry = type_to_group_entry(pt)?; + Some(cddl::ast::GroupEntry::InlineGroup { + occur: None, + group: cddl::ast::Group { + group_choices: vec![cddl::ast::GroupChoice { + group_entries: vec![( + inner_entry, + cddl::ast::OptionalComma { + optional_comma: false, + trailing_comments: None, + _a: std::marker::PhantomData, + }, + )], + span: pt.span, + comments_before_grpchoice: None, + }], + span: *span, + }, + comments_before_group: comments_before_type.clone(), + comments_after_group: comments_after_type.clone(), + span: *span, + }) + } + _ => None, + } +} + +/// Returns the literal value of a member key. This includes arrow map keys +/// (`? "key" => int`), which cddl 0.10.7 parses as `MemberKey::Type1` instead of +/// `MemberKey::Value`. +pub(crate) fn member_key_literal_value<'a>( + mk: &cddl::ast::MemberKey<'a>, +) -> Option> { + match mk { + cddl::ast::MemberKey::Value { value, .. } => Some(value.clone()), + cddl::ast::MemberKey::Type1 { t1, .. } if t1.operator.is_none() => match &t1.type2 { + cddl::ast::Type2::IntValue { value, .. } => Some(cddl::token::Value::INT(*value)), + cddl::ast::Type2::UintValue { value, .. } => Some(cddl::token::Value::UINT(*value)), + cddl::ast::Type2::FloatValue { value, .. } => Some(cddl::token::Value::FLOAT(*value)), + cddl::ast::Type2::TextValue { value, .. } => { + Some(cddl::token::Value::TEXT(value.clone())) + } + cddl::ast::Type2::UTF8ByteString { value, .. } => Some(cddl::token::Value::BYTE( + cddl::token::ByteValue::UTF8(value.clone()), + )), + cddl::ast::Type2::B16ByteString { value, .. } => Some(cddl::token::Value::BYTE( + cddl::token::ByteValue::B16(value.clone()), + )), + // cddl 0.10.7 does not produce `Type2::B64ByteString` (see `byte_value_to_js_string`). + _ => None, + }, + _ => None, + } +} + +/// Returns true for a non-cut `MemberKey::Type1` that is not a literal key (see +/// [`member_key_literal_value`]), such as `* text => any`. +pub(crate) fn is_non_cut_non_literal_member_key(mk: &cddl::ast::MemberKey) -> bool { + matches!(mk, cddl::ast::MemberKey::Type1 { is_cut: false, .. }) + && member_key_literal_value(mk).is_none() +} + +/// Converts a bare type name or parenthesized type inside an array, which cddl 0.10.7 +/// parses as `GroupEntry::TypeGroupname` or `GroupEntry::InlineGroup`, back to a type entry. +pub(crate) fn array_group_entry_as_type_entry<'a>( + entry: &cddl::ast::GroupEntry<'a>, + group_rules: &std::collections::HashSet, +) -> Option> { + match entry { + cddl::ast::GroupEntry::TypeGroupname { ge, .. } => { + if group_rules.contains(ge.name.ident) + || matches!(ge.name.socket, Some(cddl::token::SocketPlug::GROUP)) + { + return None; + } + Some(cddl::ast::ValueMemberKeyEntry { + occur: ge.occur.clone(), + member_key: None, + entry_type: typename_to_type(&ge.name, &ge.generic_args), + }) + } + cddl::ast::GroupEntry::InlineGroup { occur, group, .. } => { + let entry_type = inline_group_as_type(group, group_rules)?; + Some(cddl::ast::ValueMemberKeyEntry { + occur: occur.clone(), + member_key: None, + entry_type, + }) + } + cddl::ast::GroupEntry::ValueMemberKey { .. } => None, + } +} + +fn typename_to_type<'a>( + name: &cddl::ast::Identifier<'a>, + generic_args: &Option>, +) -> cddl::ast::Type<'a> { + cddl::ast::Type { + type_choices: vec![cddl::ast::TypeChoice { + type1: cddl::ast::Type1 { + type2: cddl::ast::Type2::Typename { + ident: name.clone(), + generic_args: generic_args.clone(), + span: name.span, + }, + operator: None, + span: name.span, + comments_after_type: None, + }, + comments_before_type: None, + comments_after_type: None, + }], + span: name.span, + } +} + +fn inline_group_as_type<'a>( + group: &cddl::ast::Group<'a>, + group_rules: &std::collections::HashSet, +) -> Option> { + if group.group_choices.len() != 1 { + return None; + } + let choice = &group.group_choices[0]; + if choice.group_entries.len() != 1 { + return None; + } + let (inner_entry, _) = &choice.group_entries[0]; + match inner_entry { + cddl::ast::GroupEntry::ValueMemberKey { ge, .. } => { + if ge.occur.is_none() && ge.member_key.is_none() { + Some(ge.entry_type.clone()) + } else { + None + } + } + cddl::ast::GroupEntry::TypeGroupname { ge, .. } => { + if ge.occur.is_some() + || group_rules.contains(ge.name.ident) + || matches!(ge.name.socket, Some(cddl::token::SocketPlug::GROUP)) + { + return None; + } + Some(typename_to_type(&ge.name, &ge.generic_args)) + } + cddl::ast::GroupEntry::InlineGroup { + occur, + group: inner_group, + span, + comments_before_group, + comments_after_group, + } => { + if occur.is_some() { + return None; + } + let pt = inline_group_as_type(inner_group, group_rules)?; + Some(cddl::ast::Type { + type_choices: vec![cddl::ast::TypeChoice { + type1: cddl::ast::Type1 { + type2: cddl::ast::Type2::ParenthesizedType { + pt, + comments_before_type: comments_before_group.clone(), + comments_after_type: comments_after_group.clone(), + span: *span, + }, + operator: None, + span: *span, + comments_after_type: None, + }, + comments_before_type: None, + comments_after_type: None, + }], + span: *span, + }) + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -152,33 +500,222 @@ mod tests { fn test_escape_js_string() { // Plain values do not change. assert_eq!(escape_js_string("hello world"), "hello world"); + assert_eq!(escape_js_string("unicode é"), "unicode é"); + // Every backslash is a literal backslash. + assert_eq!(escape_js_string(r"\d+\.\d+"), r"\\d+\\.\\d+"); + assert_eq!(escape_js_string(r"\n\b\u00e9"), r"\\n\\b\\u00e9"); + assert_eq!(escape_js_string(r"a\"), r"a\\"); + // Double quotes are escaped. + assert_eq!(escape_js_string(r#"a"b"#), r#"a\"b"#); + assert_eq!(escape_js_string(r#"\""#), r#"\\\""#); + // Line terminators and control characters are escaped. + assert_eq!( + escape_js_string("a\nb\r\t\u{0}\u{8}\u{2028}\u{2029}"), + r"a\nb\r\t\u0000\u0008\u2028\u2029" + ); + // Glob-like values are safe to use in doc comments. + assert_eq!(escape_js_string("src/**/*.ts"), r"src/**\/*.ts"); + } + + #[test] + fn test_escape_js_raw_string() { + // Plain values do not change. + assert_eq!(escape_js_raw_string("hello world"), "hello world"); // Valid escape sequences from the CDDL source do not change. - assert_eq!(escape_js_string(r#"a\"b\\c\n\/"#), r#"a\"b\\c\n\/"#); - assert_eq!(escape_js_string(r"\u00e9\u{1F600}\'"), r"\u00e9\u{1F600}\'"); + assert_eq!(escape_js_raw_string(r#"a\"b\\c\n\/"#), r#"a\"b\\c\n\/"#); + assert_eq!(escape_js_raw_string(r"\u00e9\u{1F600}\'"), r"\u00e9\u{1F600}\'"); // Code point escapes accept leading zeros, up to U+10FFFF. assert_eq!( - escape_js_string(r"\u{0041}\u{000000041}"), + escape_js_raw_string(r"\u{0041}\u{000000041}"), r"\u{0041}\u{000000041}" ); - assert_eq!(escape_js_string(r"\u{10FFFF}"), r"\u{10FFFF}"); - assert_eq!(escape_js_string(r"\u{110000}"), r"\\u{110000}"); + assert_eq!(escape_js_raw_string(r"\u{10FFFF}"), r"\u{10FFFF}"); + assert_eq!(escape_js_raw_string(r"\u{110000}"), r"\\u{110000}"); assert_eq!( - escape_js_string(r"\u{FFFFFFFFFF}\u{}"), + escape_js_raw_string(r"\u{FFFFFFFFFF}\u{}"), r"\\u{FFFFFFFFFF}\\u{}" ); // Raw double quotes (allowed in byte strings) are escaped. - assert_eq!(escape_js_string(r#"'say "hi"'"#), r#"'say \"hi\"'"#); + assert_eq!(escape_js_raw_string(r#"'say "hi"'"#), r#"'say \"hi\"'"#); // An escaped backslash followed by a raw quote. - assert_eq!(escape_js_string(r#"\\""#), r#"\\\""#); + assert_eq!(escape_js_raw_string(r#"\\""#), r#"\\\""#); // Unpaired or invalid backslashes are escaped. - assert_eq!(escape_js_string(r"a\"), r"a\\"); - assert_eq!(escape_js_string(r"\x41\u12"), r"\\x41\\u12"); + assert_eq!(escape_js_raw_string(r"a\"), r"a\\"); + assert_eq!(escape_js_raw_string(r"\x41\u12"), r"\\x41\\u12"); // Line terminators and control characters are escaped. assert_eq!( - escape_js_string("a\nb\r\t\u{0}\u{2028}\u{2029}"), + escape_js_raw_string("a\nb\r\t\u{0}\u{2028}\u{2029}"), r"a\nb\r\t\u0000\u2028\u2029" ); // Glob-like values are safe to use in doc comments. - assert_eq!(escape_js_string("src/**/*.ts"), r"src/**\/*.ts"); + assert_eq!(escape_js_raw_string("src/**/*.ts"), r"src/**\/*.ts"); + } + + #[test] + fn test_byte_value_to_js_string() { + use cddl::token::ByteValue; + // Single-quoted byte strings hold raw source text. + assert_eq!( + byte_value_to_js_string(&ByteValue::UTF8(br#"say "hi" \n"#.as_ref().into())), + r#"'say \"hi\" \n'"# + ); + // Hex byte strings hold decoded bytes, which need not be valid UTF-8. + assert_eq!( + byte_value_to_js_string(&ByteValue::B16([0xFF].as_ref().into())), + "h'FF'" + ); + assert_eq!( + byte_value_to_js_string(&ByteValue::B16(br"\n".as_ref().into())), + "h'5C6E'" + ); + // `member_key_literal_value` does not construct `ByteValue::B64` from `B64ByteString`. + let b64_mk = cddl::ast::MemberKey::Type1 { + t1: Box::new(cddl::ast::Type1 { + type2: cddl::ast::Type2::B64ByteString { + value: [0xFF].as_ref().into(), + span: Default::default(), + }, + operator: None, + span: Default::default(), + comments_after_type: None, + }), + is_cut: false, + span: Default::default(), + comments_before_cut: None, + comments_after_cut: None, + comments_after_arrowmap: None, + }; + assert_eq!(member_key_literal_value(&b64_mk), None); + // cddl 0.10.7 has no `b64'...'` token: as a value it is a parse error, and inside a map + // `{ b64'...' => int }` lexes as group name `b64` followed by `'...' => int`. + assert!(cddl::parser::cddl_from_str("x = b64'aGVsbG8='", false).is_err()); + let parsed = cddl::parser::cddl_from_str("m = { b64'aGVsbG8=' => int }", false).unwrap(); + let cddl::ast::Rule::Type { rule, .. } = &parsed.rules[0] else { + panic!("Expected type rule"); + }; + let cddl::ast::Type2::Map { group, .. } = &rule.value.type_choices[0].type1.type2 else { + panic!("Expected map"); + }; + assert_eq!(group.group_choices[0].group_entries.len(), 2); + } + + #[test] + fn test_collect_group_rules_and_type_rule_as_group_rule() { + let input = r#" + BrowserCommand = ( browser.Close ) + browser.Close = ( method: "browser.close" ) + NetworkResult = ( network.AddInterceptResult ) + network.AddInterceptResult = { intercept: text } + BrowserCommand /= ( browser.Close ) + MultiChoice = ( network.AddInterceptResult / text ) + "#; + let cddl = cddl::parser::cddl_from_str(input, true).unwrap(); + let group_rules = collect_group_rules(&cddl); + + assert!(group_rules.contains("browser.Close")); + assert!(group_rules.contains("BrowserCommand")); + assert!(!group_rules.contains("network.AddInterceptResult")); + assert!(!group_rules.contains("NetworkResult")); + + if let cddl::ast::Rule::Type { rule, .. } = &cddl.rules[0] { + let gr = type_rule_as_group_rule(rule, &group_rules).unwrap(); + assert!(!gr.is_group_choice_alternate); + } else { + panic!("Expected BrowserCommand to parse as Rule::Type in cddl 0.10.7"); + } + + if let cddl::ast::Rule::Type { rule, .. } = &cddl.rules[2] { + assert!(type_rule_as_group_rule(rule, &group_rules).is_none()); + let unwrapped = unwrap_single_ident_parens(&rule.value); + assert!(matches!( + unwrapped.type_choices[0].type1.type2, + cddl::ast::Type2::Typename { .. } + )); + } else { + panic!("Expected NetworkResult to parse as Rule::Type"); + } + + if let cddl::ast::Rule::Type { rule, .. } = &cddl.rules[4] { + assert!(type_rule_as_group_rule(rule, &group_rules).is_none()); + } else { + panic!("Expected BrowserCommand /= to parse as Rule::Type"); + } + + if let cddl::ast::Rule::Type { rule, .. } = &cddl.rules[5] { + let unwrapped = unwrap_single_ident_parens(&rule.value); + assert!(matches!( + unwrapped.type_choices[0].type1.type2, + cddl::ast::Type2::ParenthesizedType { .. } + )); + } else { + panic!("Expected MultiChoice to parse as Rule::Type"); + } + } + + #[test] + fn test_member_key_literal_value() { + let input = r#" + m = { + ? "optional-key" => int, + ? "cut-key" ^ => int, + * text => any, + } + "#; + let cddl = cddl::parser::cddl_from_str(input, true).unwrap(); + let cddl::ast::Rule::Type { rule, .. } = &cddl.rules[0] else { + panic!("Expected type rule"); + }; + let cddl::ast::Type2::Map { group, .. } = &rule.value.type_choices[0].type1.type2 else { + panic!("Expected map"); + }; + let entries = &group.group_choices[0].group_entries; + let cddl::ast::GroupEntry::ValueMemberKey { ge: first, .. } = &entries[0].0 else { + panic!("Expected first entry"); + }; + let first_mk = first.member_key.as_ref().unwrap(); + assert_eq!( + member_key_literal_value(first_mk), + Some(cddl::token::Value::TEXT("optional-key".into())) + ); + assert!(!is_non_cut_non_literal_member_key(first_mk)); + + let cddl::ast::GroupEntry::ValueMemberKey { ge: second, .. } = &entries[1].0 else { + panic!("Expected second entry"); + }; + let second_mk = second.member_key.as_ref().unwrap(); + assert_eq!( + member_key_literal_value(second_mk), + Some(cddl::token::Value::TEXT("cut-key".into())) + ); + assert!(!is_non_cut_non_literal_member_key(second_mk)); + + let cddl::ast::GroupEntry::ValueMemberKey { ge: third, .. } = &entries[2].0 else { + panic!("Expected third entry"); + }; + let third_mk = third.member_key.as_ref().unwrap(); + assert_eq!(member_key_literal_value(third_mk), None); + assert!(is_non_cut_non_literal_member_key(third_mk)); + } + + #[test] + fn test_array_group_entry_as_type_entry() { + let input = r#" + Arr = [* MyType, *(uint / text), *(uint / text,), * MyGroup] + MyType = text + MyGroup = ( a: int ) + "#; + let cddl = cddl::parser::cddl_from_str(input, true).unwrap(); + let group_rules = collect_group_rules(&cddl); + let cddl::ast::Rule::Type { rule, .. } = &cddl.rules[0] else { + panic!("Expected type rule"); + }; + let cddl::ast::Type2::Array { group, .. } = &rule.value.type_choices[0].type1.type2 else { + panic!("Expected array"); + }; + let entries = &group.group_choices[0].group_entries; + assert!(array_group_entry_as_type_entry(&entries[0].0, &group_rules).is_some()); + assert!(array_group_entry_as_type_entry(&entries[1].0, &group_rules).is_some()); + assert!(array_group_entry_as_type_entry(&entries[2].0, &group_rules).is_some()); + assert!(array_group_entry_as_type_entry(&entries[3].0, &group_rules).is_none()); } } diff --git a/tests/snapshots/typescript__it_works_with_maps-2.snap b/tests/snapshots/typescript__it_works_with_maps-2.snap index e6edd07..e0d1295 100644 --- a/tests/snapshots/typescript__it_works_with_maps-2.snap +++ b/tests/snapshots/typescript__it_works_with_maps-2.snap @@ -9,6 +9,6 @@ export type ExtensibleMapExample2 = (({ export type ExtensibleMapExample3 = (({ "optional-key"?:(number),[key: string]:(any)})); export type ExtensibleMapExample4 = (({ -["optional-key"]?:(number),[key: string]:(any)})); +"optional-key"?:(number),[key: string]:(any)})); export type ExampleMap = (({ "hyphenated-key"?:(string),"another-key"?:(string),"standardIdentifier"?:(string),"1"?:(string)})); diff --git a/tests/snapshots/typescript__it_works_with_string_literals-2.snap b/tests/snapshots/typescript__it_works_with_string_literals-2.snap index b24dbb0..5a50b92 100644 --- a/tests/snapshots/typescript__it_works_with_string_literals-2.snap +++ b/tests/snapshots/typescript__it_works_with_string_literals-2.snap @@ -3,12 +3,28 @@ source: tests/typescript.rs expression: "String :: from_utf8(stdout.into_inner().unwrap()).unwrap()" --- export type QuotedBytes = ("'say \"hello\"'"); -export type TextEscapes = ("quote \" backslash \\ newline \n unicode \u00e9"); +export type EscapedBytes = ("'a \\ backslash \n'"); +export type HexBytes = ("h'FF'"); +export type HexBackslash = ("h'5C6E'"); +export type TextEscapes = ("quote \" backslash \\ newline \n unicode é"); /** * @defaultValue `"src/**\/*.ts"` */ export type GlobPattern = (string); + +/** + * Must match the pattern `"\\d+\\.\\d+"`. + */ +export type Version = (string); + +/** + * Must match the pattern `"\\bword\\b"`. + */ +export type Word = (string); export type Headers = (({ "'content-type'":(string),"x-\"custom\"":(string)})); export type Greeting = ("hi"| "hello \"there\""| "'say \"hey\"'"); +export type QuotedEnum = ("say \"hi\""| "back\\slash"); +export type Codes = (({ +"1":(string),"na\\me":(string)})); diff --git a/tests/snapshots/zod__it_works-2.snap b/tests/snapshots/zod__it_works-2.snap index 137868b..28e09cf 100644 --- a/tests/snapshots/zod__it_works-2.snap +++ b/tests/snapshots/zod__it_works-2.snap @@ -501,8 +501,7 @@ export const UserPromptOpenedParametersSchema: z.ZodLazy> = z.lazy(() => z.union([Network.AddInterceptSchema,Network.ContinueRequestSchema,Network.ContinueResponseSchema,Network.ContinueWithAuthSchema,Network.FailRequestSchema,Network.ProvideResponseSchema,Network.RemoveInterceptSchema])); -export const -NetworkResultSchema: z.ZodLazy = z.lazy(() => Network.AddInterceptResultSchema); +export const NetworkResultSchema: z.ZodLazy = z.lazy(() => Network.AddInterceptResultSchema); export const NetworkEventSchema: z.ZodLazy> = z.lazy(() => z.union([Network.AuthRequiredSchema,Network.BeforeRequestSentSchema,Network.FetchErrorSchema,Network.ResponseCompletedSchema,Network.ResponseStartedSchema])); export namespace Network { diff --git a/tests/snapshots/zod__it_works_with_maps-2.snap b/tests/snapshots/zod__it_works_with_maps-2.snap index f03b64d..120b085 100644 --- a/tests/snapshots/zod__it_works_with_maps-2.snap +++ b/tests/snapshots/zod__it_works_with_maps-2.snap @@ -23,10 +23,10 @@ z.ZodString,z.ZodAny>>> = z.lazy(() => z.object({ z.record( z.string(),z.any())) ); -export const ExtensibleMapExample4Schema: z.ZodLazy,z.ZodOptional>,z.ZodRecord< -z.ZodString,z.ZodAny>>> = z.lazy(() => z.record( -z.literal("optional-key"),z.number().int().optional()).and( +export const ExtensibleMapExample4Schema: z.ZodLazy}>,z.ZodRecord< +z.ZodString,z.ZodAny>>> = z.lazy(() => z.object({ +"optional-key":z.number().int().optional()}).and( z.record( z.string(),z.any())) ); diff --git a/tests/snapshots/zod__it_works_with_string_literals-2.snap b/tests/snapshots/zod__it_works_with_string_literals-2.snap index cdeed7c..05788ea 100644 --- a/tests/snapshots/zod__it_works_with_string_literals-2.snap +++ b/tests/snapshots/zod__it_works_with_string_literals-2.snap @@ -3,9 +3,18 @@ source: tests/zod.rs expression: "String :: from_utf8(stdout.into_inner().unwrap()).unwrap()" --- export const QuotedBytesSchema: z.ZodLiteral<"'say \"hello\"'"> = z.literal("'say \"hello\"'"); -export const TextEscapesSchema: z.ZodLiteral<"quote \" backslash \\ newline \n unicode \u00e9"> = z.literal("quote \" backslash \\ newline \n unicode \u00e9"); +export const EscapedBytesSchema: z.ZodLiteral<"'a \\ backslash \n'"> = z.literal("'a \\ backslash \n'"); +export const HexBytesSchema: z.ZodLiteral<"h'FF'"> = z.literal("h'FF'"); +export const HexBackslashSchema: z.ZodLiteral<"h'5C6E'"> = z.literal("h'5C6E'"); +export const TextEscapesSchema: z.ZodLiteral<"quote \" backslash \\ newline \n unicode é"> = z.literal("quote \" backslash \\ newline \n unicode é"); export const GlobPatternSchema: z.ZodLazy> = z.lazy(() => z.string().default("src/**\/*.ts")); +export const VersionSchema: z.ZodLazy = z.lazy(() => z.string().regex(new RegExp("\\d+\\.\\d+"))); +export const WordSchema: z.ZodLazy = z.lazy(() => z.string().regex(new RegExp("\\bword\\b"))); export const HeadersSchema: z.ZodLazy> = z.lazy(() => z.object({ "'content-type'":z.string(),"x-\"custom\"":z.string()})); export const GreetingSchema: z.ZodLazy,z.ZodLiteral<"hello \"there\"">,z.ZodLiteral<"'say \"hey\"'">]>> = z.lazy(() => z.union([z.literal("hi"),z.literal("hello \"there\""),z.literal("'say \"hey\"'")])); +export const QuotedEnumSchema: z.ZodLazy> = z.lazy(() => z.enum(["say \"hi\"","back\\slash",])); +export const CodesSchema: z.ZodLazy> = z.lazy(() => z.object({ +"1":z.string(),"na\\me":z.string()}));