diff --git a/Cargo.lock b/Cargo.lock index 5e255b308b..c78841f4a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" +dependencies = [ + "lazy_static", + "regex", +] + [[package]] name = "addr2line" version = "0.24.2" @@ -73,6 +83,7 @@ dependencies = [ "const-random", "getrandom 0.2.15", "once_cell", + "serde", "version_check", "zerocopy", ] @@ -150,7 +161,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d73c455ae09fa2223a75114789f30ad605e9e297f79537953523366c05995f5f" dependencies = [ "regex", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -163,7 +174,7 @@ dependencies = [ "ratatui", "simdutf8", "smallvec", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -246,7 +257,7 @@ dependencies = [ "rsa", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", "x509-certificate", ] @@ -319,7 +330,7 @@ dependencies = [ "scroll", "security-framework", "security-framework-sys", - "semver", + "semver 1.0.23", "serde", "serde_json", "serde_yaml", @@ -330,7 +341,7 @@ dependencies = [ "spki 0.7.3", "subtle", "tempfile", - "thiserror", + "thiserror 1.0.69", "tokio", "tungstenite 0.21.0", "uuid", @@ -358,7 +369,7 @@ dependencies = [ "scroll", "serde", "serde-xml-rs", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -384,7 +395,7 @@ dependencies = [ "sha1", "sha2", "signature 2.2.0", - "thiserror", + "thiserror 1.0.69", "url", "x509-certificate", "xml-rs", @@ -485,7 +496,7 @@ dependencies = [ "nom", "num-traits", "rusticata-macros", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -511,6 +522,18 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ast_node" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9184f2b369b3e8625712493c89b785881f27eedc6cde480a81883cef78868b2" +dependencies = [ + "proc-macro2", + "quote", + "swc_macros_common", + "syn 2.0.87", +] + [[package]] name = "async-broadcast" version = "0.7.1" @@ -791,6 +814,17 @@ dependencies = [ "terminal-prompt", ] +[[package]] +name = "auto_impl" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c87f3f15e7794432337fc718554eaa4dc8f04c9677a950ffe366f20a162ae42" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "autocfg" version = "1.4.0" @@ -864,9 +898,9 @@ dependencies = [ [[package]] name = "aws-lc-rs" -version = "1.10.0" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdd82dba44d209fddb11c190e0a94b78651f95299598e472215667417a03ff1d" +checksum = "fe7c2840b66236045acd2607d5866e274380afd87ef99d6226e961e2cb47df45" dependencies = [ "aws-lc-sys", "mirai-annotations", @@ -877,9 +911,9 @@ dependencies = [ [[package]] name = "aws-lc-sys" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df7a4168111d7eb622a31b214057b8509c0a7e1794f44c546d742330dc793972" +checksum = "ad3a619a9de81e1d7de1f1186dcba4506ed661a0e483d84410fdef0ee87b2f96" dependencies = [ "bindgen", "cc", @@ -918,9 +952,9 @@ dependencies = [ [[package]] name = "aws-sdk-s3" -version = "1.60.0" +version = "1.61.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0506cc60e392e33712d47717d5ae5760a3b134bf8ee7aea7e43df3d7e2669ae0" +checksum = "0e531658a0397d22365dfe26c3e1c0c8448bf6a3a2d8a098ded802f2b1261615" dependencies = [ "aws-credential-types", "aws-runtime", @@ -941,7 +975,7 @@ dependencies = [ "hmac", "http 0.2.12", "http-body 0.4.6", - "lru", + "lru 0.12.5", "once_cell", "percent-encoding", "regex-lite", @@ -996,9 +1030,9 @@ dependencies = [ [[package]] name = "aws-sdk-sts" -version = "1.49.0" +version = "1.50.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53dcf5e7d9bd1517b8b998e170e650047cea8a2b85fe1835abe3210713e541b7" +checksum = "6ada54e5f26ac246dc79727def52f7f8ed38915cb47781e2a72213957dc3a7d5" dependencies = [ "aws-credential-types", "aws-runtime", @@ -1179,7 +1213,7 @@ version = "1.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fbd94a32b3a7d55d3806fe27d98d3ad393050439dd05eb53ece36ec5e3d3510" dependencies = [ - "base64-simd", + "base64-simd 0.8.0", "bytes", "bytes-utils", "futures-core", @@ -1218,7 +1252,7 @@ dependencies = [ "aws-smithy-async", "aws-smithy-runtime-api", "aws-smithy-types", - "rustc_version", + "rustc_version 0.4.1", "tracing", ] @@ -1252,9 +1286,9 @@ dependencies = [ [[package]] name = "axum" -version = "0.7.7" +version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "504e3947307ac8326a5437504c517c4b56716c9d98fac0028c2acc7ca47d70ae" +checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" dependencies = [ "async-trait", "axum-core 0.4.5", @@ -1329,25 +1363,26 @@ dependencies = [ [[package]] name = "axum-extra" -version = "0.9.4" +version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73c3220b188aea709cf1b6c5f9b01c3bd936bb08bd2b5184a12b35ac8131b1f9" +checksum = "c794b30c904f0a1c2fb7740f7df7f7972dfaa14ef6f57cb6178dc63e5dca2f04" dependencies = [ - "axum 0.7.7", + "axum 0.7.9", "axum-core 0.4.5", "bytes", + "fastrand", "futures-util", "headers", "http 1.1.0", "http-body 1.0.1", "http-body-util", "mime", + "multer", "pin-project-lite", "serde", "tower 0.5.1", "tower-layer", "tower-service", - "tracing", ] [[package]] @@ -1376,7 +1411,7 @@ dependencies = [ "hyper 1.5.0", "hyper-util", "pin-project-lite", - "rustls 0.23.16", + "rustls 0.23.17", "rustls-pemfile 2.2.0", "rustls-pki-types", "tokio", @@ -1393,7 +1428,7 @@ checksum = "d0f0035d1d5a70ac279e80a75e60592023ba8b327c929c3827f6c5ed749b9f8b" dependencies = [ "aes-gcm", "async-trait", - "axum 0.7.7", + "axum 0.7.9", "base64 0.21.7", "bytes", "chrono", @@ -1409,7 +1444,7 @@ dependencies = [ "serde_json", "sha2", "sqlx", - "thiserror", + "thiserror 1.0.69", "tokio", "tower-layer", "tower-service", @@ -1480,13 +1515,22 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "base64-simd" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "781dd20c3aff0bd194fe7d2a977dd92f21c173891f3a03b677359e5fa457e5d5" +dependencies = [ + "simd-abstraction", +] + [[package]] name = "base64-simd" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "339abbe78e73178762e23bea9dfd08e697eb3f3301cd4be981c0f78ba5859195" dependencies = [ - "outref", + "outref 0.5.1", "vsimd", ] @@ -1506,6 +1550,15 @@ dependencies = [ "smallvec", ] +[[package]] +name = "better_scoped_tls" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "297b153aa5e573b5863108a6ddc9d5c968bd0b20e75cc614ee9821d2f45679c7" +dependencies = [ + "scoped-tls", +] + [[package]] name = "bigdecimal" version = "0.3.1" @@ -1580,7 +1633,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57792b99d555ebf109c83169228076f7d997e2b37ba1a653850ccd703ac7bab0" dependencies = [ "sysctl", - "thiserror", + "thiserror 1.0.69", "uname", "winapi", ] @@ -1680,9 +1733,9 @@ dependencies = [ [[package]] name = "borsh" -version = "1.5.2" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5327f6c99920069d1fe374aa743be1af0031dea9f250852cdf1ae6a0861ee24" +checksum = "2506947f73ad44e344215ccd6403ac2ae18cd8e046e581a441bf8d199f257f03" dependencies = [ "borsh-derive", "cfg_aliases 0.2.1", @@ -1690,9 +1743,9 @@ dependencies = [ [[package]] name = "borsh-derive" -version = "1.5.2" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10aedd8f1a81a8aafbfde924b0e3061cd6fedd6f6bbcfc6a76e6fd426d7bfe26" +checksum = "c2593a3b8b938bd68373196c9832f516be11fa487ef4ae745eb282e6a56a7244" dependencies = [ "once_cell", "proc-macro-crate 3.2.0", @@ -1733,11 +1786,29 @@ dependencies = [ "alloc-stdlib", ] +[[package]] +name = "browserslist-rs" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdf0ca73de70c3da94e4194e4a01fe732378f55d47cf4c0588caab22a0dbfa14" +dependencies = [ + "ahash 0.8.11", + "chrono", + "either", + "indexmap 2.6.0", + "itertools 0.13.0", + "nom", + "once_cell", + "serde", + "serde_json", + "thiserror 1.0.69", +] + [[package]] name = "bstr" -version = "1.10.0" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" +checksum = "1a68f1f47cdf0ec8ee4b941b2eee2a80cb796db73118c0dd09ac63fbe405be22" dependencies = [ "memchr", "regex-automata 0.4.9", @@ -1767,6 +1838,9 @@ name = "bumpalo" version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +dependencies = [ + "allocator-api2", +] [[package]] name = "bytecheck" @@ -1862,7 +1936,7 @@ dependencies = [ "glib", "libc", "once_cell", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -1940,10 +2014,10 @@ dependencies = [ "remove_dir_all", "rhai", "sanitize-filename", - "semver", + "semver 1.0.23", "serde", "tempfile", - "thiserror", + "thiserror 1.0.69", "time", "toml", "walkdir", @@ -1966,10 +2040,10 @@ checksum = "2d886547e41f740c616ae73108f6eb70afe6d940c7bc697cb30f13daec073037" dependencies = [ "camino", "cargo-platform", - "semver", + "semver 1.0.23", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -2023,9 +2097,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1aeb932158bd710538c73702db6945cb68a8fb08c519e6e12706b94263b36db8" +checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" dependencies = [ "jobserver", "libc", @@ -2079,9 +2153,9 @@ dependencies = [ [[package]] name = "cfg-expr" -version = "0.17.0" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0890061c4d3223e7267f3bad2ec40b997d64faac1c2815a4a9d95018e2b9e9c" +checksum = "c360837f8f19e2e4468275138f1c0dec1647d1e17bb7c0215fe3cd7530e93c25" dependencies = [ "smallvec", ] @@ -2169,9 +2243,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.20" +version = "4.5.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" +checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" dependencies = [ "clap_builder", "clap_derive", @@ -2179,9 +2253,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.20" +version = "4.5.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" +checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" dependencies = [ "anstream", "anstyle", @@ -2204,9 +2278,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" +checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" [[package]] name = "cmac" @@ -2440,6 +2514,45 @@ dependencies = [ "tiny-keccak", ] +[[package]] +name = "const-serialize" +version = "0.6.0-alpha.5" +dependencies = [ + "const-serialize", + "const-serialize-macro", + "rand 0.8.5", + "serde", +] + +[[package]] +name = "const-serialize-macro" +version = "0.6.0-alpha.5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + +[[package]] +name = "const-str" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21077772762a1002bb421c3af42ac1725fa56066bfc53d9a55bb79905df2aaf3" +dependencies = [ + "const-str-proc-macro", +] + +[[package]] +name = "const-str-proc-macro" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e1e0fdd2e5d3041e530e1b21158aeeef8b5d0e306bc5c1e3d6cf0930d10e25a" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "const_format" version = "0.2.33" @@ -2594,7 +2707,7 @@ dependencies = [ "chrono", "is_executable", "simple-file-manifest", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -2633,7 +2746,7 @@ version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a47af21622d091a8f0fb295b88bc886ac74efcc613efc19f5d0b21de5c89e47" dependencies = [ - "rustc_version", + "rustc_version 0.4.1", ] [[package]] @@ -2827,6 +2940,28 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "cssparser" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9be934d936a0fbed5bcdc01042b770de1398bf79d0e192f49fa7faea0e99281e" +dependencies = [ + "cssparser-macros", + "dtoa-short", + "itoa 1.0.11", + "phf 0.11.2", + "smallvec", +] + +[[package]] +name = "cssparser-color" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "556c099a61d85989d7af52b692e35a8d68a57e7df8c6d07563dc0778b3960c9f" +dependencies = [ + "cssparser 0.33.0", +] + [[package]] name = "cssparser-macros" version = "0.6.1" @@ -2878,7 +3013,7 @@ dependencies = [ "digest", "fiat-crypto", "rand_core 0.6.4", - "rustc_version", + "rustc_version 0.4.1", "subtle", "zeroize", ] @@ -3001,6 +3136,15 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" +[[package]] +name = "data-url" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a30bfce702bcfa94e906ef82421f2c0e61c076ad76030c16ee5d2e9a32fe193" +dependencies = [ + "matches", +] + [[package]] name = "dbl" version = "0.3.2" @@ -3010,6 +3154,16 @@ dependencies = [ "generic-array 0.14.7", ] +[[package]] +name = "debugid" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef552e6f588e446098f6ba40d89ac146c8c7b64aade83c051ee00bb5d2bc18d" +dependencies = [ + "serde", + "uuid", +] + [[package]] name = "der" version = "0.6.1" @@ -3092,7 +3246,7 @@ dependencies = [ "convert_case 0.4.0", "proc-macro2", "quote", - "rustc_version", + "rustc_version 0.4.1", "syn 2.0.87", ] @@ -3114,7 +3268,7 @@ dependencies = [ "console", "shell-words", "tempfile", - "thiserror", + "thiserror 1.0.69", "zeroize", ] @@ -3169,7 +3323,7 @@ dependencies = [ "manganis", "rand 0.8.5", "serde", - "thiserror", + "thiserror 1.0.69", "tokio", "tracing", ] @@ -3206,7 +3360,7 @@ dependencies = [ "ansi-to-html", "ansi-to-tui", "anyhow", - "axum 0.7.7", + "axum 0.7.9", "axum-extra", "axum-server", "brotli 6.0.0", @@ -3219,6 +3373,7 @@ dependencies = [ "clap", "console", "console-subscriber", + "const-serialize", "convert_case 0.6.0", "crossterm", "ctrlc", @@ -3246,31 +3401,87 @@ dependencies = [ "hyper-rustls 0.27.3", "hyper-util", "ignore", + "image", + "imagequant", "include_dir", "itertools 0.13.0", "krates", + "lightningcss", "log", "manganis-core", + "mozjpeg", "notify", "object 0.36.5", "once_cell", "open", "path-absolutize", + "png", "prettyplease", "proc-macro2", "ratatui", "rayon", "regex", "reqwest 0.12.9", - "rustls 0.23.16", + "rustls 0.23.17", "serde", "serde_json", "strum 0.26.3", + "swc", + "swc_allocator", + "swc_atoms", + "swc_cached", + "swc_common", + "swc_compiler_base 0.19.0", + "swc_config", + "swc_config_macro", + "swc_ecma_ast", + "swc_ecma_codegen", + "swc_ecma_codegen_macros", + "swc_ecma_compat_bugfixes 0.12.0", + "swc_ecma_compat_common", + "swc_ecma_compat_es2015 0.12.0", + "swc_ecma_compat_es2016 0.12.0", + "swc_ecma_compat_es2017 0.12.0", + "swc_ecma_compat_es2018 0.12.0", + "swc_ecma_compat_es2019 0.12.0", + "swc_ecma_compat_es2020 0.12.0", + "swc_ecma_compat_es2021 0.12.0", + "swc_ecma_compat_es2022 0.12.0", + "swc_ecma_compat_es3 0.12.0", + "swc_ecma_ext_transforms", + "swc_ecma_lints 0.100.0", + "swc_ecma_loader", + "swc_ecma_minifier 0.204.0", + "swc_ecma_parser", + "swc_ecma_preset_env 0.217.0", + "swc_ecma_transforms 0.239.0", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_classes 0.134.0", + "swc_ecma_transforms_compat 0.171.0", + "swc_ecma_transforms_macros", + "swc_ecma_transforms_module 0.190.0", + "swc_ecma_transforms_optimization 0.208.0", + "swc_ecma_transforms_proposal 0.178.0", + "swc_ecma_transforms_react 0.191.0", + "swc_ecma_transforms_typescript 0.198.1", + "swc_ecma_usage_analyzer", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_eq_ignore_macros", + "swc_error_reporters", + "swc_fast_graph", + "swc_macros_common", + "swc_node_comments", + "swc_timer", + "swc_trace_macro", + "swc_transform_common", + "swc_typescript", + "swc_visit", "syn 2.0.87", "tauri-bundler", "tauri-utils", "tempfile", - "thiserror", + "thiserror 1.0.69", "throbber-widgets-tui", "tokio", "tokio-stream", @@ -3399,7 +3610,7 @@ dependencies = [ "signal-hook", "slab", "tao", - "thiserror", + "thiserror 1.0.69", "tokio", "tokio-tungstenite 0.23.1", "tracing", @@ -3499,7 +3710,7 @@ version = "0.6.0-alpha.5" dependencies = [ "async-trait", "aws-lc-rs", - "axum 0.7.7", + "axum 0.7.9", "base64 0.22.1", "bytes", "ciborium", @@ -3524,10 +3735,10 @@ dependencies = [ "once_cell", "parking_lot", "pin-project", - "rustls 0.23.16", + "rustls 0.23.17", "serde", "server_fn", - "thiserror", + "thiserror 1.0.69", "tokio", "tokio-stream", "tokio-util", @@ -3631,9 +3842,9 @@ version = "0.6.0-alpha.5" dependencies = [ "chrono", "http 1.1.0", - "lru", + "lru 0.12.5", "rustc-hash 1.1.0", - "thiserror", + "thiserror 1.0.69", "tracing", ] @@ -3657,7 +3868,7 @@ dependencies = [ name = "dioxus-liveview" version = "0.6.0-alpha.5" dependencies = [ - "axum 0.7.7", + "axum 0.7.9", "dioxus", "dioxus-cli-config", "dioxus-core", @@ -3674,7 +3885,7 @@ dependencies = [ "serde", "serde_json", "slab", - "thiserror", + "thiserror 1.0.69", "tokio", "tokio-stream", "tokio-util", @@ -3716,7 +3927,7 @@ dependencies = [ name = "dioxus-playwright-liveview-test" version = "0.0.1" dependencies = [ - "axum 0.7.7", + "axum 0.7.9", "dioxus", "dioxus-liveview", "tokio", @@ -3746,7 +3957,7 @@ dependencies = [ name = "dioxus-router" version = "0.6.0-alpha.5" dependencies = [ - "axum 0.7.7", + "axum 0.7.9", "base64 0.22.1", "ciborium", "criterion", @@ -3901,7 +4112,7 @@ dependencies = [ name = "dioxus_server_macro" version = "0.6.0-alpha.5" dependencies = [ - "axum 0.7.7", + "axum 0.7.9", "dioxus", "proc-macro2", "quote", @@ -4502,7 +4713,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38e2275cc4e4fc009b0669731a1e5ab7ebf11f469eaede2bab9309a5b4d6057f" dependencies = [ "memoffset", - "rustc_version", + "rustc_version 0.4.1", ] [[package]] @@ -4549,9 +4760,9 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" [[package]] name = "flate2" -version = "1.0.34" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1b589b4dc103969ad3cf85c950899926ec64300a1a46d76c03a6072957036f0" +checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" dependencies = [ "crc32fast", "miniz_oxide", @@ -4647,7 +4858,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8835f84f38484cc86f110a805655697908257fb9a7af005234060891557198e9" dependencies = [ "nonempty", - "thiserror", + "thiserror 1.0.69", +] + +[[package]] +name = "from_variant" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32016f1242eb82af5474752d00fd8ebcd9004bd69b462b1c91de833972d08ed4" +dependencies = [ + "proc-macro2", + "swc_macros_common", + "syn 2.0.87", ] [[package]] @@ -4694,7 +4916,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", - "axum 0.7.7", + "axum 0.7.9", "axum_session", "axum_session_auth", "dioxus", @@ -4748,7 +4970,7 @@ dependencies = [ name = "fullstack-router-example" version = "0.1.0" dependencies = [ - "axum 0.7.7", + "axum 0.7.9", "dioxus", "serde", "tokio", @@ -5114,7 +5336,7 @@ dependencies = [ "once_cell", "pin-project-lite", "smallvec", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -5155,7 +5377,7 @@ dependencies = [ "gix-date", "gix-utils", "itoa 1.0.11", - "thiserror", + "thiserror 1.0.69", "winnow 0.6.20", ] @@ -5175,7 +5397,7 @@ dependencies = [ "memchr", "once_cell", "smallvec", - "thiserror", + "thiserror 1.0.69", "unicode-bom", "winnow 0.6.20", ] @@ -5190,7 +5412,7 @@ dependencies = [ "bstr", "gix-path", "libc", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -5201,7 +5423,7 @@ checksum = "9eed6931f21491ee0aeb922751bd7ec97b4b2fe8fbfedcb678e2a2dce5f3b8c0" dependencies = [ "bstr", "itoa 1.0.11", - "thiserror", + "thiserror 1.0.69", "time", ] @@ -5250,7 +5472,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f93d7df7366121b5018f947a04d37f034717e113dcf9ccd85c34b58e57a74d5e" dependencies = [ "faster-hex", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -5261,7 +5483,7 @@ checksum = "e3bc7fe297f1f4614774989c00ec8b1add59571dc9b024b4c00acb7dedd4e19d" dependencies = [ "gix-tempfile", "gix-utils", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -5279,7 +5501,7 @@ dependencies = [ "gix-validate", "itoa 1.0.11", "smallvec", - "thiserror", + "thiserror 1.0.69", "winnow 0.6.20", ] @@ -5293,7 +5515,7 @@ dependencies = [ "gix-trace", "home", "once_cell", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -5314,7 +5536,7 @@ dependencies = [ "gix-utils", "gix-validate", "memmap2", - "thiserror", + "thiserror 1.0.69", "winnow 0.6.20", ] @@ -5366,7 +5588,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82c27dd34a49b1addf193c92070bcbf3beaf6e10f16a78544de6372e146a0acf" dependencies = [ "bstr", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -5389,7 +5611,7 @@ dependencies = [ "memchr", "once_cell", "smallvec", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -5434,7 +5656,7 @@ dependencies = [ "keyboard-types", "objc", "once_cell", - "thiserror", + "thiserror 1.0.69", "windows-sys 0.52.0", "x11-dl", ] @@ -5477,7 +5699,7 @@ dependencies = [ "pin-project", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", @@ -5674,7 +5896,7 @@ dependencies = [ "pest_derive", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -5686,6 +5908,15 @@ dependencies = [ "ahash 0.7.8", ] +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash 0.8.11", +] + [[package]] name = "hashbrown" version = "0.14.5" @@ -5814,6 +6045,20 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "hstr" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dae404c0c5d4e95d4858876ab02eecd6a196bb8caa42050dfa809938833fc412" +dependencies = [ + "hashbrown 0.14.5", + "new_debug_unreachable", + "once_cell", + "phf 0.11.2", + "rustc-hash 1.1.0", + "triomphe", +] + [[package]] name = "html5ever" version = "0.26.0" @@ -5840,7 +6085,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -5851,7 +6096,7 @@ checksum = "dd54ae4f69adcc1a43637dcff230852832c3ad50df31a90e0cb5f001dd441359" dependencies = [ "anyhow", "lazy_static", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -6012,7 +6257,7 @@ dependencies = [ "hyper 1.5.0", "hyper-util", "log", - "rustls 0.23.16", + "rustls 0.23.17", "rustls-native-certs 0.8.0", "rustls-pki-types", "tokio", @@ -6254,6 +6499,12 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "if_chain" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed" + [[package]] name = "ignore" version = "0.4.23" @@ -6303,6 +6554,19 @@ dependencies = [ "quick-error", ] +[[package]] +name = "imagequant" +version = "4.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecc99538c9061ee4d88476f6cd704c9f06575a34f0083affdaa1337a331aa7" +dependencies = [ + "arrayvec", + "once_cell", + "rayon", + "rgb", + "thread_local", +] + [[package]] name = "imgref" version = "1.11.0" @@ -6507,6 +6771,18 @@ dependencies = [ "once_cell", ] +[[package]] +name = "is-macro" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d57a3e447e24c22647738e4607f1df1e0ec6f72e16182c4cd199f647cdfb0e4" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "is-terminal" version = "0.4.13" @@ -6637,7 +6913,7 @@ dependencies = [ "combine", "jni-sys", "log", - "thiserror", + "thiserror 1.0.69", "walkdir", "windows-sys 0.45.0", ] @@ -6681,7 +6957,16 @@ dependencies = [ "jsonptr", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", +] + +[[package]] +name = "jsonc-parser" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b56a20e76235284255a09fcd1f45cf55d3c524ea657ebd3854735925c57743d" +dependencies = [ + "serde_json", ] [[package]] @@ -6792,14 +7077,14 @@ dependencies = [ [[package]] name = "krates" -version = "0.17.3" +version = "0.17.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e34c533fa7eee6063e640fd1cb059add729588f28f22f3b2a5650275e20f3ee3" +checksum = "cd5bdd9794c39f6eb77da784fdcd065cc730a95fd0ca7d88ec945ed26c3c5109" dependencies = [ "camino", - "cfg-expr 0.17.0", + "cfg-expr 0.17.1", "petgraph", - "semver", + "semver 1.0.23", "serde", "serde_json", ] @@ -6820,7 +7105,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f29e4755b7b995046f510a7520c42b2fed58b77bd94d5a87a8eb43d2fd126da8" dependencies = [ - "cssparser", + "cssparser 0.27.2", "html5ever", "indexmap 1.9.3", "matches", @@ -6893,9 +7178,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.162" +version = "0.2.164" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" +checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" [[package]] name = "libfuzzer-sys" @@ -7014,6 +7299,44 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "lightningcss" +version = "1.0.0-alpha.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f3aad0f3d9105ab72b02caf1b2a998a6d549f3fff8cca0c558e590c3245edfd" +dependencies = [ + "ahash 0.8.11", + "bitflags 2.6.0", + "const-str", + "cssparser 0.33.0", + "cssparser-color", + "dashmap", + "data-encoding", + "getrandom 0.2.15", + "itertools 0.10.5", + "lazy_static", + "lightningcss-derive", + "parcel_selectors", + "parcel_sourcemap", + "paste", + "pathdiff", + "rayon", + "serde", + "smallvec", +] + +[[package]] +name = "lightningcss-derive" +version = "1.0.0-alpha.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84c12744d1279367caed41739ef094c325d53fb0ffcd4f9b84a368796f870252" +dependencies = [ + "convert_case 0.6.0", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "link-cplusplus" version = "1.0.9" @@ -7132,6 +7455,15 @@ dependencies = [ "imgref", ] +[[package]] +name = "lru" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "718e8fae447df0c7e1ba7f5189829e63fd536945c8988d61444c19039f16b670" +dependencies = [ + "hashbrown 0.13.2", +] + [[package]] name = "lru" version = "0.12.5" @@ -7181,32 +7513,30 @@ dependencies = [ name = "manganis" version = "0.6.0-alpha.5" dependencies = [ - "anyhow", - "base64 0.22.1", - "dioxus-core-types", - "dunce", + "const-serialize", + "manganis-core", "manganis-macro", - "once_cell", - "serde", ] [[package]] name = "manganis-core" version = "0.6.0-alpha.5" dependencies = [ + "const-serialize", + "dioxus", + "dioxus-core-types", + "manganis", "serde", - "serde_json", ] [[package]] name = "manganis-macro" version = "0.6.0-alpha.5" dependencies = [ + "manganis", "manganis-core", "proc-macro2", "quote", - "serde", - "serde_json", "syn 2.0.87", ] @@ -7296,14 +7626,39 @@ dependencies = [ ] [[package]] -name = "mime" -version = "0.3.17" +name = "miette" +version = "7.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" - -[[package]] -name = "mime_guess" -version = "2.0.5" +checksum = "4edc8853320c2a0dab800fbda86253c8938f6ea88510dc92c5f1ed20e794afc1" +dependencies = [ + "cfg-if", + "miette-derive", + "owo-colors", + "textwrap", + "thiserror 1.0.69", + "unicode-width 0.1.14", +] + +[[package]] +name = "miette-derive" +version = "7.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf09caffaac8068c346b6df2a7fc27a177fd20b39421a39ce0a211bde679a6c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mime_guess" +version = "2.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" dependencies = [ @@ -7388,6 +7743,31 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9be0862c1b3f26a88803c4a49de6889c10e608b3ee9344e6ef5b45fb37ad3d1" +[[package]] +name = "mozjpeg" +version = "0.10.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "969e1dbc9af2f18ffe6ddba72bbe86506c7214ecb28670d98ecfba51cb9b8c6b" +dependencies = [ + "arrayvec", + "bytemuck", + "libc", + "mozjpeg-sys", + "rgb", +] + +[[package]] +name = "mozjpeg-sys" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27e31c0171e0b1158c0dfb7386dbdf999f4a9afaa83fd68de39c7929f4d5c16f" +dependencies = [ + "cc", + "dunce", + "libc", + "nasm-rs", +] + [[package]] name = "muda" version = "0.11.5" @@ -7402,7 +7782,7 @@ dependencies = [ "objc", "once_cell", "png", - "thiserror", + "thiserror 1.0.69", "windows-sys 0.52.0", ] @@ -7422,7 +7802,7 @@ dependencies = [ "objc2-foundation", "once_cell", "png", - "thiserror", + "thiserror 1.0.69", "windows-sys 0.59.0", ] @@ -7452,6 +7832,15 @@ dependencies = [ "rand 0.8.5", ] +[[package]] +name = "nasm-rs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12fcfa1bd49e0342ec1d07ed2be83b59963e7acbeb9310e1bb2c07b69dadd959" +dependencies = [ + "jobserver", +] + [[package]] name = "native-tls" version = "0.2.12" @@ -7481,7 +7870,7 @@ dependencies = [ "ndk-sys", "num_enum", "raw-window-handle 0.6.2", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -7568,6 +7957,15 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" +[[package]] +name = "normpath" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a9da8c9922c35a1033d76f7272dfc2e7ee20392083d75aeea6ced23c6266578" +dependencies = [ + "winapi", +] + [[package]] name = "normpath" version = "1.3.0" @@ -7629,6 +8027,7 @@ checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" dependencies = [ "num-integer", "num-traits", + "serde", ] [[package]] @@ -7731,6 +8130,16 @@ dependencies = [ "libm", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi 0.3.9", + "libc", +] + [[package]] name = "num_enum" version = "0.7.3" @@ -7982,9 +8391,9 @@ checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" [[package]] name = "open" -version = "5.3.0" +version = "5.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61a877bf6abd716642a53ef1b89fb498923a4afca5c754f9050b4d081c05c4b3" +checksum = "3ecd52f0b8d15c40ce4820aa251ed5de032e5d91fab27f7db2f40d42a8bdf69c" dependencies = [ "is-wsl", "libc", @@ -8025,9 +8434,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-src" -version = "300.3.2+3.3.2" +version = "300.4.1+3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a211a18d945ef7e648cc6e0058f4c548ee46aab922ea203e0d30e966ea23647b" +checksum = "faa4eac4138c62414b5622d1b31c5c304f34b406b013c079c2bbc652fdd6678c" dependencies = [ "cc", ] @@ -8071,6 +8480,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "outref" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f222829ae9293e33a9f5e9f440c6760a3d450a64affe1846486b140db81c1f4" + [[package]] name = "outref" version = "0.5.1" @@ -8184,6 +8599,36 @@ dependencies = [ "system-deps", ] +[[package]] +name = "parcel_selectors" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7645c578d3a5c4cdf667af1ad39765f5f751c4883d251e050d5e1204b5cad0a9" +dependencies = [ + "bitflags 2.6.0", + "cssparser 0.33.0", + "log", + "phf 0.11.2", + "phf_codegen 0.11.2", + "precomputed-hash", + "rustc-hash 2.0.0", + "smallvec", +] + +[[package]] +name = "parcel_sourcemap" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "485b74d7218068b2b7c0e3ff12fbc61ae11d57cb5d8224f525bd304c6be05bbb" +dependencies = [ + "base64-simd 0.7.0", + "data-url", + "rkyv", + "serde", + "serde_json", + "vlq", +] + [[package]] name = "parking" version = "2.2.1" @@ -8239,6 +8684,18 @@ dependencies = [ "path-dedot", ] +[[package]] +name = "path-clean" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecba01bf2678719532c5e3059e0b5f0811273d94b397088b82e3bd0a78c78fdd" + +[[package]] +name = "path-clean" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17359afc20d7ab31fdb42bb844c8b3bb1dabd7dcf7e68428492da7f16966fcef" + [[package]] name = "path-dedot" version = "3.1.1" @@ -8309,7 +8766,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "879952a81a83930934cbf1786752d6dedc3b1f29e8f8fb2ad1d0a36f377cf442" dependencies = [ "memchr", - "thiserror", + "thiserror 1.0.69", "ucd-trie", ] @@ -8415,7 +8872,7 @@ dependencies = [ "sha3", "signature 2.2.0", "smallvec", - "thiserror", + "thiserror 1.0.69", "twofish", "x25519-dalek", "zeroize", @@ -8471,6 +8928,16 @@ dependencies = [ "phf_shared 0.10.0", ] +[[package]] +name = "phf_codegen" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8d39688d359e6b34654d328e262234662d16cc0f60ec8dcbe5e718709342a5a" +dependencies = [ + "phf_generator 0.11.2", + "phf_shared 0.11.2", +] + [[package]] name = "phf_generator" version = "0.8.0" @@ -8755,6 +9222,24 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "preset_env_base" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b30eab18be480c194938e433e269d5298a279f6410f02fbc73f3576a325c110" +dependencies = [ + "ahash 0.8.11", + "anyhow", + "browserslist-rs", + "dashmap", + "from_variant", + "once_cell", + "semver 1.0.23", + "serde", + "st-map", + "tracing", +] + [[package]] name = "prettier-please" version = "0.3.0" @@ -8941,6 +9426,15 @@ dependencies = [ "prost", ] +[[package]] +name = "psm" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "200b9ff220857e53e184257720a14553b2f4aa02577d2ed9842d45d4b9654810" +dependencies = [ + "cc", +] + [[package]] name = "ptr_meta" version = "0.1.4" @@ -8987,37 +9481,40 @@ dependencies = [ [[package]] name = "quinn" -version = "0.11.5" +version = "0.11.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c7c5fdde3cdae7203427dc4f0a68fe0ed09833edc525a03456b153b79828684" +checksum = "62e96808277ec6f97351a2380e6c25114bc9e67037775464979f3037c92d05ef" dependencies = [ "bytes", "pin-project-lite", "quinn-proto", "quinn-udp", "rustc-hash 2.0.0", - "rustls 0.23.16", + "rustls 0.23.17", "socket2", - "thiserror", + "thiserror 2.0.3", "tokio", "tracing", ] [[package]] name = "quinn-proto" -version = "0.11.8" +version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fadfaed2cd7f389d0161bb73eeb07b7b78f8691047a6f3e73caaeae55310a4a6" +checksum = "a2fe5ef3495d7d2e377ff17b1a8ce2ee2ec2a18cde8b6ad6619d65d0701c135d" dependencies = [ "bytes", + "getrandom 0.2.15", "rand 0.8.5", "ring", "rustc-hash 2.0.0", - "rustls 0.23.16", + "rustls 0.23.17", + "rustls-pki-types", "slab", - "thiserror", + "thiserror 2.0.3", "tinyvec", "tracing", + "web-time", ] [[package]] @@ -9049,6 +9546,12 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "radix_fmt" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce082a9940a7ace2ad4a8b7d0b1eac6aa378895f18be598230c5f2284ac05426" + [[package]] name = "rand" version = "0.7.3" @@ -9180,7 +9683,7 @@ dependencies = [ "crossterm", "instability", "itertools 0.13.0", - "lru", + "lru 0.12.5", "paste", "strum 0.26.3", "strum_macros 0.26.4", @@ -9219,7 +9722,7 @@ dependencies = [ "rand_chacha 0.3.1", "simd_helpers", "system-deps", - "thiserror", + "thiserror 1.0.69", "v_frame", "wasm-bindgen", ] @@ -9297,7 +9800,7 @@ checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" dependencies = [ "getrandom 0.2.15", "libredox", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -9360,7 +9863,7 @@ dependencies = [ "cvt", "fs_at", "libc", - "normpath", + "normpath 1.3.0", "windows-sys 0.59.0", ] @@ -9445,7 +9948,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "quinn", - "rustls 0.23.16", + "rustls 0.23.17", "rustls-pemfile 2.2.0", "rustls-pki-types", "serde", @@ -9516,6 +10019,9 @@ name = "rgb" version = "0.8.50" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57397d16646700483b67d2dd6511d79318f9d057fdbd21a4066aeac8b41d310a" +dependencies = [ + "bytemuck", +] [[package]] name = "rhai" @@ -9623,7 +10129,7 @@ dependencies = [ "pgp", "sha1", "sha2", - "thiserror", + "thiserror 1.0.69", "xz2", "zstd", ] @@ -9682,13 +10188,22 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver 0.9.0", +] + [[package]] name = "rustc_version" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" dependencies = [ - "semver", + "semver 1.0.23", ] [[package]] @@ -9741,9 +10256,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.16" +version = "0.23.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eee87ff5d9b36712a58574e12e9f0ea80f915a5b0ac518d322b24a465617925e" +checksum = "7f1a745511c54ba6d4465e8d5dfbd81b45791756de28d4981af70d6dca128f1e" dependencies = [ "aws-lc-rs", "log", @@ -9816,6 +10331,9 @@ name = "rustls-pki-types" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "16f1201b3c9a7ee8039bcadc17b7e605e2945b27eee7631788c1bd2b0643674b" +dependencies = [ + "web-time", +] [[package]] name = "rustls-webpki" @@ -9871,6 +10389,12 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "ryu-js" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad97d4ce1560a5e27cec89519dc8300d1aa6035b099821261c651486a19e44d5" + [[package]] name = "same-file" version = "1.0.6" @@ -10011,7 +10535,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df320f1889ac4ba6bc0cdc9c9af7af4bd64bb927bccdf32d81140dc1f9be12fe" dependencies = [ "bitflags 1.3.2", - "cssparser", + "cssparser 0.27.2", "derive_more", "fxhash", "log", @@ -10024,6 +10548,15 @@ dependencies = [ "thin-slice", ] +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + [[package]] name = "semver" version = "1.0.23" @@ -10033,6 +10566,12 @@ dependencies = [ "serde", ] +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + [[package]] name = "send_wrapper" version = "0.6.0" @@ -10087,7 +10626,7 @@ checksum = "fb3aa78ecda1ebc9ec9847d5d3aba7d618823446a049ba2491940506da6e2782" dependencies = [ "log", "serde", - "thiserror", + "thiserror 1.0.69", "xml-rs", ] @@ -10104,9 +10643,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.132" +version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" dependencies = [ "itoa 1.0.11", "memchr", @@ -10132,7 +10671,7 @@ checksum = "0431a35568651e363364210c91983c1da5eb29404d9f0928b67d4ebcfa7d330c" dependencies = [ "percent-encoding", "serde", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -10216,7 +10755,7 @@ version = "0.6.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fae7a3038a32e5a34ba32c6c45eb4852f8affaf8b794ebfcd4b1099e2d62ebe" dependencies = [ - "axum 0.7.7", + "axum 0.7.9", "bytes", "const_format", "dashmap", @@ -10234,7 +10773,7 @@ dependencies = [ "serde_json", "serde_qs", "server_fn_macro_default", - "thiserror", + "thiserror 1.0.69", "tower 0.4.13", "tower-layer", "url", @@ -10398,6 +10937,15 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "simd-abstraction" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cadb29c57caadc51ff8346233b5cec1d240b68ce55cf1afc764818791876987" +dependencies = [ + "outref 0.1.0", +] + [[package]] name = "simd-adler32" version = "0.3.7" @@ -10433,7 +10981,7 @@ checksum = "adc4e5204eb1910f40f9cfa375f6f05b68c3abac4b6fd879c8ff5e7ae8a0a085" dependencies = [ "num-bigint", "num-traits", - "thiserror", + "thiserror 1.0.69", "time", ] @@ -10520,6 +11068,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "smawk" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c" + [[package]] name = "snafu" version = "0.7.5" @@ -10590,6 +11144,25 @@ dependencies = [ "system-deps", ] +[[package]] +name = "sourcemap" +version = "9.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dab08a862c70980b8e23698b507e272317ae52a608a164a844111f5372374f1f" +dependencies = [ + "base64-simd 0.7.0", + "bitvec", + "data-encoding", + "debugid", + "if_chain", + "rustc-hash 1.1.0", + "rustc_version 0.2.3", + "serde", + "serde_json", + "unicode-id-start", + "url", +] + [[package]] name = "spake2" version = "0.4.0" @@ -10695,7 +11268,7 @@ dependencies = [ "sha2", "smallvec", "sqlformat", - "thiserror", + "thiserror 1.0.69", "time", "tokio", "tokio-stream", @@ -10784,7 +11357,7 @@ dependencies = [ "smallvec", "sqlx-core", "stringprep", - "thiserror", + "thiserror 1.0.69", "time", "tracing", "uuid", @@ -10831,7 +11404,7 @@ dependencies = [ "smallvec", "sqlx-core", "stringprep", - "thiserror", + "thiserror 1.0.69", "time", "tracing", "uuid", @@ -10864,12 +11437,46 @@ dependencies = [ "uuid", ] +[[package]] +name = "st-map" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8257dd592de7614be71a2342d36ba2d527ddad3f9a0c8d09d6ceed4c371531e4" +dependencies = [ + "arrayvec", + "static-map-macro", +] + [[package]] name = "stable_deref_trait" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" +[[package]] +name = "stacker" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799c883d55abdb5e98af1a7b3f23b9b6de8ecada0ecac058672d7635eb48ca7b" +dependencies = [ + "cc", + "cfg-if", + "libc", + "psm", + "windows-sys 0.59.0", +] + +[[package]] +name = "static-map-macro" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "710e9696ef338691287aeb937ee6ffe60022f579d3c8d2fd9d58973a9a10a466" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "static_assertions" version = "1.1.0" @@ -10902,6 +11509,18 @@ dependencies = [ "quote", ] +[[package]] +name = "string_enum" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05e383308aebc257e7d7920224fa055c632478d92744eca77f99be8fa1545b90" +dependencies = [ + "proc-macro2", + "quote", + "swc_macros_common", + "syn 2.0.87", +] + [[package]] name = "stringprep" version = "0.1.5" @@ -10995,36 +11614,1460 @@ dependencies = [ ] [[package]] -name = "syn" -version = "1.0.109" +name = "swc" +version = "0.283.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +checksum = "9cb7fe4bd6a604528819c84fc9acc5d5ec1b3bd0d11d13ee5d8a77d49ff678fa" dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", + "anyhow", + "base64 0.21.7", + "dashmap", + "either", + "indexmap 2.6.0", + "jsonc-parser", + "lru 0.10.1", + "once_cell", + "parking_lot", + "pathdiff", + "regex", + "rustc-hash 1.1.0", + "serde", + "serde_json", + "sourcemap", + "swc_atoms", + "swc_cached", + "swc_common", + "swc_compiler_base 0.16.0", + "swc_config", + "swc_ecma_ast", + "swc_ecma_codegen", + "swc_ecma_ext_transforms", + "swc_ecma_lints 0.99.0", + "swc_ecma_loader", + "swc_ecma_minifier 0.201.0", + "swc_ecma_parser", + "swc_ecma_preset_env 0.214.0", + "swc_ecma_transforms 0.236.0", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_compat 0.170.0", + "swc_ecma_transforms_optimization 0.205.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_error_reporters", + "swc_node_comments", + "swc_timer", + "swc_transform_common", + "swc_typescript", + "swc_visit", + "tracing", + "url", ] [[package]] -name = "syn" -version = "2.0.87" +name = "swc_allocator" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +checksum = "adc8bd3075d1c6964010333fae9ddcd91ad422a4f8eb8b3206a9b2b6afb4209e" dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", + "bumpalo", + "hashbrown 0.14.5", + "ptr_meta", + "rustc-hash 1.1.0", + "triomphe", ] [[package]] -name = "sync_wrapper" -version = "0.1.2" +name = "swc_atoms" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" - -[[package]] -name = "sync_wrapper" -version = "1.0.1" +checksum = "bb6567e4e67485b3e7662b486f1565bdae54bd5b9d6b16b2ba1a9babb1e42125" +dependencies = [ + "hstr", + "once_cell", + "rustc-hash 1.1.0", + "serde", +] + +[[package]] +name = "swc_cached" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83406221c501860fce9c27444f44125eafe9e598b8b81be7563d7036784cd05c" +dependencies = [ + "ahash 0.8.11", + "anyhow", + "dashmap", + "once_cell", + "regex", + "serde", +] + +[[package]] +name = "swc_common" +version = "0.37.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12d0a8eaaf1606c9207077d75828008cb2dfb51b095a766bd2b72ef893576e31" +dependencies = [ + "ahash 0.8.11", + "ast_node", + "better_scoped_tls", + "cfg-if", + "either", + "from_variant", + "new_debug_unreachable", + "num-bigint", + "once_cell", + "parking_lot", + "rustc-hash 1.1.0", + "serde", + "siphasher", + "sourcemap", + "swc_allocator", + "swc_atoms", + "swc_eq_ignore_macros", + "swc_visit", + "tracing", + "unicode-width 0.1.14", + "url", +] + +[[package]] +name = "swc_compiler_base" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9b47fac27c2e84e033bdea294ac12f575322c81c9d5d44f423e3f11ff239a86" +dependencies = [ + "anyhow", + "base64 0.21.7", + "once_cell", + "pathdiff", + "rustc-hash 1.1.0", + "serde", + "serde_json", + "sourcemap", + "swc_allocator", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_codegen", + "swc_ecma_minifier 0.201.0", + "swc_ecma_parser", + "swc_ecma_visit", + "swc_timer", +] + +[[package]] +name = "swc_compiler_base" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "feb87f8dc7be1a034d5c29bcc4be9d504ddfd2f8aa1a1338fc568e104e087d29" +dependencies = [ + "anyhow", + "base64 0.21.7", + "once_cell", + "pathdiff", + "rustc-hash 1.1.0", + "serde", + "serde_json", + "sourcemap", + "swc_allocator", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_codegen", + "swc_ecma_minifier 0.204.0", + "swc_ecma_parser", + "swc_ecma_visit", + "swc_timer", +] + +[[package]] +name = "swc_config" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4740e53eaf68b101203c1df0937d5161a29f3c13bceed0836ddfe245b72dd000" +dependencies = [ + "anyhow", + "indexmap 2.6.0", + "serde", + "serde_json", + "sourcemap", + "swc_cached", + "swc_config_macro", +] + +[[package]] +name = "swc_config_macro" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c5f56139042c1a95b54f5ca48baa0e0172d369bcc9d3d473dad1de36bae8399" +dependencies = [ + "proc-macro2", + "quote", + "swc_macros_common", + "syn 2.0.87", +] + +[[package]] +name = "swc_ecma_ast" +version = "0.118.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6f866d12e4d519052b92a0a86d1ac7ff17570da1272ca0c89b3d6f802cd79df" +dependencies = [ + "bitflags 2.6.0", + "is-macro", + "num-bigint", + "phf 0.11.2", + "scoped-tls", + "serde", + "string_enum", + "swc_atoms", + "swc_common", + "unicode-id-start", +] + +[[package]] +name = "swc_ecma_codegen" +version = "0.155.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc7641608ef117cfbef9581a99d02059b522fcca75e5244fa0cbbd8606689c6f" +dependencies = [ + "memchr", + "num-bigint", + "once_cell", + "serde", + "sourcemap", + "swc_allocator", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_codegen_macros", + "tracing", +] + +[[package]] +name = "swc_ecma_codegen_macros" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "859fabde36db38634f3fad548dd5e3410c1aebba1b67a3c63e67018fa57a0bca" +dependencies = [ + "proc-macro2", + "quote", + "swc_macros_common", + "syn 2.0.87", +] + +[[package]] +name = "swc_ecma_compat_bugfixes" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f9cac39f19d6509db921f4b75934aaa64fc84b599416e5c1fcaed1c313132f" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_compat_es2015 0.11.1", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_bugfixes" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75429b44cc479cbe018d5994eddae5ac7ab887ebefeb3596720921bc4cdff551" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_compat_es2015 0.12.0", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_common" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9acdf402b36f8e83084b10e119d7ba9d07e5229ef39e1343f147db816c7b73e" +dependencies = [ + "swc_common", + "swc_ecma_ast", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", +] + +[[package]] +name = "swc_ecma_compat_es2015" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dade6e0c6e8ddb61281fee2331c3775a920c31535b91e8cace2e0c4eed6158d3" +dependencies = [ + "arrayvec", + "indexmap 2.6.0", + "is-macro", + "serde", + "serde_derive", + "smallvec", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_compat_common", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_classes 0.133.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2015" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c988d9018d6abb22b0fcc2da6a624be2db7c56681b6180d1cb5faa2672fd8001" +dependencies = [ + "arrayvec", + "indexmap 2.6.0", + "is-macro", + "rustc-hash 1.1.0", + "serde", + "serde_derive", + "smallvec", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_compat_common", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_classes 0.134.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2016" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "209e347cdc3fb56632a1d882f981f3448f5f529c16d8da9d770207fffda4a8f6" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2016" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b7a3e086151c70ff940531ddcd04c01351ae80aa4593fd2906255d18a836b4f" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2017" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4aa87a6861b2adc8b0178fb450165101c4396409481c8726ec90ad28398cae5d" +dependencies = [ + "serde", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2017" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3b74c89c9bd4fa532fba3d1ec47b129ec450b4143d3914118cd61b0e44d4a4b" +dependencies = [ + "serde", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2018" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f577f098e7c3738ade709caadb17c9f3bd911ea2ee6cfacca561d12addcc5761" +dependencies = [ + "serde", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_compat_common", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2018" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a40bf74a06c433eee502ea6347596d5766d77da8baf32653d14a6655df4e181a" +dependencies = [ + "serde", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_compat_common", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2019" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d52253dc2f83a3fca526c387c33e4ff9a8423b68c271414c9f870e1ced3231" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2019" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10afb20890ffda37eefdfe06c3bb0d12e5ec8698667cb9e3689b74066b398845" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2020" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed343932876fad34b1d4a13e30c55b94531e89916f45e7c04203bc49a29565b9" +dependencies = [ + "serde", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_compat_es2022 0.11.0", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2020" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0608c4814a362d5362bc536507d8c89b287521778e8b678fe4590bfa1843803a" +dependencies = [ + "serde", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_compat_es2022 0.12.0", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2021" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6b28a2c109466eaa809d9b9a5b81dcbb4e269ba293a9c5c34aabc67b6427bc" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2021" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f12ffb0f4282f4b333efa98c9653d181d89e1b5339d4be0d789189a246ef34b" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2022" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab3a644e271ea2a9df88e3e456c5c204c4916ef5136b7d946f9cd25607f47ec6" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_compat_common", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_classes 0.133.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es2022" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc16be9dd64e1b32569375b0b73ecc7dc74f9d848e8caaf2007896e2cf8d68a7" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_compat_common", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_classes 0.134.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es3" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e55ffadc12067b21524bf7b5d6938021ee918f65f18937ed27245c23544bc910" +dependencies = [ + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_compat_es3" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e684ae87d26ad3012e588d0e268158cadee10ddc0cda261069f0f280a8b23ce7" +dependencies = [ + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_ext_transforms" +version = "0.120.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad03ee53c734eb74757d03c07ec71b1a982261830c9253ef3e2e4a089f9af25d" +dependencies = [ + "phf 0.11.2", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_lints" +version = "0.99.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20c11bcc9e3dc49929500c07c8b0c84a88064847d31e9ee16204b257e6bd315c" +dependencies = [ + "auto_impl", + "dashmap", + "parking_lot", + "rayon", + "regex", + "serde", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_lints" +version = "0.100.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89907376ce67b56d8fbf79cca830a12cb41f93dccc306008c07d8eba8f6d388e" +dependencies = [ + "auto_impl", + "dashmap", + "parking_lot", + "rayon", + "regex", + "serde", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_loader" +version = "0.49.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55fa3d55045b97894bfb04d38aff6d6302ac8a6a38e3bb3dfb0d20475c4974a9" +dependencies = [ + "anyhow", + "dashmap", + "lru 0.10.1", + "normpath 0.2.0", + "once_cell", + "parking_lot", + "path-clean 0.1.0", + "pathdiff", + "serde", + "serde_json", + "swc_atoms", + "swc_cached", + "swc_common", + "tracing", +] + +[[package]] +name = "swc_ecma_minifier" +version = "0.201.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d75a42254926bad8b7fa9390767a18ac608d99cfd5a3be675e4739c2cf7db1b" +dependencies = [ + "arrayvec", + "indexmap 2.6.0", + "num-bigint", + "num_cpus", + "once_cell", + "parking_lot", + "phf 0.11.2", + "radix_fmt", + "regex", + "rustc-hash 1.1.0", + "ryu-js", + "serde", + "serde_json", + "swc_allocator", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_codegen", + "swc_ecma_parser", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_optimization 0.205.0", + "swc_ecma_usage_analyzer", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_timer", + "tracing", +] + +[[package]] +name = "swc_ecma_minifier" +version = "0.204.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34d88917a66b8f457c5953d2ff2d7788259658c89578636b28e9ac6ae56bbfd9" +dependencies = [ + "arrayvec", + "indexmap 2.6.0", + "num-bigint", + "num_cpus", + "once_cell", + "parking_lot", + "phf 0.11.2", + "radix_fmt", + "regex", + "rustc-hash 1.1.0", + "ryu-js", + "serde", + "serde_json", + "swc_allocator", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_codegen", + "swc_ecma_parser", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_optimization 0.208.0", + "swc_ecma_usage_analyzer", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_timer", + "tracing", +] + +[[package]] +name = "swc_ecma_parser" +version = "0.149.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683dada14722714588b56481399c699378b35b2ba4deb5c4db2fb627a97fb54b" +dependencies = [ + "either", + "new_debug_unreachable", + "num-bigint", + "num-traits", + "phf 0.11.2", + "serde", + "smallvec", + "smartstring", + "stacker", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "tracing", + "typed-arena", +] + +[[package]] +name = "swc_ecma_preset_env" +version = "0.214.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e85162c77f8c80b55e5aed3a5e5477b74a53ce9cca05dec6e3dabec1de0f49af" +dependencies = [ + "anyhow", + "dashmap", + "indexmap 2.6.0", + "once_cell", + "preset_env_base", + "rustc-hash 1.1.0", + "semver 1.0.23", + "serde", + "serde_json", + "st-map", + "string_enum", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms 0.236.0", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_preset_env" +version = "0.217.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51992e6bb854ef2e6c7a1b9a14ed8d0e3c8f905d348f694759f9a97bfa6a425" +dependencies = [ + "anyhow", + "dashmap", + "indexmap 2.6.0", + "once_cell", + "preset_env_base", + "rustc-hash 1.1.0", + "semver 1.0.23", + "serde", + "serde_json", + "st-map", + "string_enum", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms 0.239.0", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms" +version = "0.236.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d289a83ab829d076d6c2bf2cc0beea7fbf0480ac47a415401f683181a65f4856" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_compat 0.170.0", + "swc_ecma_transforms_module 0.187.0", + "swc_ecma_transforms_optimization 0.205.0", + "swc_ecma_transforms_proposal 0.178.0", + "swc_ecma_transforms_react 0.190.0", + "swc_ecma_transforms_typescript 0.195.1", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms" +version = "0.239.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82df2dd8048fe23f1df72acd52bfebf846b3d5a76e048eee32acf9af9bee6a98" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_compat 0.171.0", + "swc_ecma_transforms_proposal 0.179.0", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms_base" +version = "0.144.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c0a71579d030e12fd3cfbfc8712c4ce21afc526f2a759903c77d8df61950f5e" +dependencies = [ + "better_scoped_tls", + "bitflags 2.6.0", + "indexmap 2.6.0", + "once_cell", + "phf 0.11.2", + "rustc-hash 1.1.0", + "serde", + "smallvec", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_parser", + "swc_ecma_utils", + "swc_ecma_visit", + "tracing", +] + +[[package]] +name = "swc_ecma_transforms_base" +version = "0.145.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65f21494e75d0bd8ef42010b47cabab9caaed8f2207570e809f6f4eb51a710d1" +dependencies = [ + "better_scoped_tls", + "bitflags 2.6.0", + "indexmap 2.6.0", + "once_cell", + "phf 0.11.2", + "rustc-hash 1.1.0", + "serde", + "smallvec", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_parser", + "swc_ecma_utils", + "swc_ecma_visit", + "tracing", +] + +[[package]] +name = "swc_ecma_transforms_classes" +version = "0.133.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f37ec04525798a09ce02e52dc15433acee2d86664da0b8ede55bb5cefd95384" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms_classes" +version = "0.134.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3d884594385bea9405a2e1721151470d9a14d3ceec5dd773c0ca6894791601" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms_compat" +version = "0.170.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bb500b65423646da940e289ad37e7c88332d7194248c33fc63a9e768e104fe5" +dependencies = [ + "arrayvec", + "indexmap 2.6.0", + "is-macro", + "num-bigint", + "serde", + "smallvec", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_compat_bugfixes 0.11.0", + "swc_ecma_compat_common", + "swc_ecma_compat_es2015 0.11.1", + "swc_ecma_compat_es2016 0.11.0", + "swc_ecma_compat_es2017 0.11.1", + "swc_ecma_compat_es2018 0.11.0", + "swc_ecma_compat_es2019 0.11.0", + "swc_ecma_compat_es2020 0.11.0", + "swc_ecma_compat_es2021 0.11.0", + "swc_ecma_compat_es2022 0.11.0", + "swc_ecma_compat_es3 0.11.0", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_classes 0.133.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_transforms_compat" +version = "0.171.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f23da29c1279b6e0c1ac0df9d0f7fd6c955a141a9770e5a0a2d54292509bcf6" +dependencies = [ + "arrayvec", + "indexmap 2.6.0", + "is-macro", + "num-bigint", + "serde", + "smallvec", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_compat_bugfixes 0.12.0", + "swc_ecma_compat_common", + "swc_ecma_compat_es2015 0.12.0", + "swc_ecma_compat_es2016 0.12.0", + "swc_ecma_compat_es2017 0.12.0", + "swc_ecma_compat_es2018 0.12.0", + "swc_ecma_compat_es2019 0.12.0", + "swc_ecma_compat_es2020 0.12.0", + "swc_ecma_compat_es2021 0.12.0", + "swc_ecma_compat_es2022 0.12.0", + "swc_ecma_compat_es3 0.12.0", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_classes 0.134.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_trace_macro", + "tracing", +] + +[[package]] +name = "swc_ecma_transforms_macros" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500a1dadad1e0e41e417d633b3d6d5de677c9e0d3159b94ba3348436cdb15aab" +dependencies = [ + "proc-macro2", + "quote", + "swc_macros_common", + "syn 2.0.87", +] + +[[package]] +name = "swc_ecma_transforms_module" +version = "0.187.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc022be297cdc70d5e71720587c7e810401a958577d77830f3108411e73c466d" +dependencies = [ + "Inflector", + "anyhow", + "bitflags 2.6.0", + "indexmap 2.6.0", + "is-macro", + "path-clean 1.0.1", + "pathdiff", + "regex", + "serde", + "swc_atoms", + "swc_cached", + "swc_common", + "swc_ecma_ast", + "swc_ecma_loader", + "swc_ecma_parser", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_utils", + "swc_ecma_visit", + "tracing", +] + +[[package]] +name = "swc_ecma_transforms_module" +version = "0.190.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c4d0255362149854b923125e9910ce0a5405ce6d03fb325c5fdd8e9f13a0845" +dependencies = [ + "Inflector", + "anyhow", + "bitflags 2.6.0", + "indexmap 2.6.0", + "is-macro", + "path-clean 1.0.1", + "pathdiff", + "regex", + "serde", + "swc_atoms", + "swc_cached", + "swc_common", + "swc_ecma_ast", + "swc_ecma_loader", + "swc_ecma_parser", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_utils", + "swc_ecma_visit", + "tracing", +] + +[[package]] +name = "swc_ecma_transforms_optimization" +version = "0.205.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17446e46b75654901d962251ec4d0063423ee81759a325ed82fcf073308d97ca" +dependencies = [ + "dashmap", + "indexmap 2.6.0", + "once_cell", + "petgraph", + "rustc-hash 1.1.0", + "serde_json", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_parser", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_fast_graph", + "tracing", +] + +[[package]] +name = "swc_ecma_transforms_optimization" +version = "0.208.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98d8447ea20ef76958a8240feef95743702485a84331e6df5bdbe7e383c87838" +dependencies = [ + "dashmap", + "indexmap 2.6.0", + "once_cell", + "petgraph", + "rustc-hash 1.1.0", + "serde_json", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_parser", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_fast_graph", + "tracing", +] + +[[package]] +name = "swc_ecma_transforms_proposal" +version = "0.178.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9c7ddb3aae86f19eb9e41b0c62509d8e400c1dc79c0889df98f6df1ab893f3f" +dependencies = [ + "either", + "rustc-hash 1.1.0", + "serde", + "smallvec", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_classes 0.133.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms_proposal" +version = "0.179.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79938ff510fc647febd8c6c3ef4143d099fdad87a223680e632623d056dae2dd" +dependencies = [ + "either", + "rustc-hash 1.1.0", + "serde", + "smallvec", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_classes 0.134.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms_react" +version = "0.190.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e54a8c87d90812bf69b0f07931bb629111a3f24efe83b9190c3a40a5ebc25e" +dependencies = [ + "base64 0.21.7", + "dashmap", + "indexmap 2.6.0", + "once_cell", + "serde", + "sha1", + "string_enum", + "swc_allocator", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_parser", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms_react" +version = "0.191.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76c76d8b9792ce51401d38da0fa62158d61f6d80d16d68fe5b03ce4bf5fba383" +dependencies = [ + "base64 0.21.7", + "dashmap", + "indexmap 2.6.0", + "once_cell", + "serde", + "sha1", + "string_enum", + "swc_allocator", + "swc_atoms", + "swc_common", + "swc_config", + "swc_ecma_ast", + "swc_ecma_parser", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_macros", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms_typescript" +version = "0.195.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f814b32ec83fde097df19e7346c429825390d156d0015f321f1f6434b6a06c0c" +dependencies = [ + "ryu-js", + "serde", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.144.0", + "swc_ecma_transforms_react 0.190.0", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_transforms_typescript" +version = "0.198.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15455da4768f97186c40523e83600495210c11825d3a44db43383fd81eace88d" +dependencies = [ + "ryu-js", + "serde", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_transforms_base 0.145.0", + "swc_ecma_transforms_react 0.191.0", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_ecma_usage_analyzer" +version = "0.30.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c7689421c6a892642c5907fd608c56d982fdef0d6456f9dba3cc418c6ea7e07" +dependencies = [ + "indexmap 2.6.0", + "rustc-hash 1.1.0", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_timer", + "tracing", +] + +[[package]] +name = "swc_ecma_utils" +version = "0.134.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "029eec7dd485923a75b5a45befd04510288870250270292fc2c1b3a9e7547408" +dependencies = [ + "indexmap 2.6.0", + "num_cpus", + "once_cell", + "rustc-hash 1.1.0", + "ryu-js", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_visit", + "tracing", + "unicode-id", +] + +[[package]] +name = "swc_ecma_visit" +version = "0.104.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b1c6802e68e51f336e8bc9644e9ff9da75d7da9c1a6247d532f2e908aa33e81" +dependencies = [ + "new_debug_unreachable", + "num-bigint", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_visit", + "tracing", +] + +[[package]] +name = "swc_eq_ignore_macros" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63db0adcff29d220c3d151c5b25c0eabe7e32dd936212b84cdaa1392e3130497" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + +[[package]] +name = "swc_error_reporters" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d049e9256abf29d9fc66d3db3ea44b6815a64ad565ce31e117a74ee96478bb3" +dependencies = [ + "anyhow", + "miette", + "once_cell", + "parking_lot", + "swc_common", +] + +[[package]] +name = "swc_fast_graph" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357e2c97bb51431d65080f25b436bc4e2fc1a7f64a643bc21a8353e478dc799f" +dependencies = [ + "indexmap 2.6.0", + "petgraph", + "rustc-hash 1.1.0", + "swc_common", +] + +[[package]] +name = "swc_macros_common" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f486687bfb7b5c560868f69ed2d458b880cebc9babebcb67e49f31b55c5bf847" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + +[[package]] +name = "swc_node_comments" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d016ab18b432523b2a3c104ce3aaf7d869db46c0a41477dbfb6201ddc86c1eb0" +dependencies = [ + "dashmap", + "swc_atoms", + "swc_common", +] + +[[package]] +name = "swc_timer" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b5fb6f8b8b85512aacbb3d7140a828666e0e0b1bcc69bf84000a0cd36306bab" +dependencies = [ + "tracing", +] + +[[package]] +name = "swc_trace_macro" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff9719b6085dd2824fd61938a881937be14b08f95e2d27c64c825a9f65e052ba" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + +[[package]] +name = "swc_transform_common" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eda3e80e1ad638d3575bc07745a914af13dcb02215098659f864731078271f2c" +dependencies = [ + "better_scoped_tls", + "once_cell", + "rustc-hash 1.1.0", + "serde", + "serde_json", +] + +[[package]] +name = "swc_typescript" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5d043347b109a8aebfe01aaeada4af322304ea0f54ae8e5721df9afcb9305ca" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "thiserror 1.0.69", +] + +[[package]] +name = "swc_visit" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ceb044142ba2719ef9eb3b6b454fce61ab849eb696c34d190f04651955c613d" +dependencies = [ + "either", + "new_debug_unreachable", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + +[[package]] +name = "sync_wrapper" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" dependencies = [ @@ -11063,7 +13106,7 @@ dependencies = [ "bitflags 1.3.2", "byteorder", "libc", - "thiserror", + "thiserror 1.0.69", "walkdir", ] @@ -11225,7 +13268,7 @@ dependencies = [ "plist", "regex", "rpm", - "semver", + "semver 1.0.23", "serde", "serde_json", "sha1", @@ -11236,7 +13279,7 @@ dependencies = [ "tauri-macos-sign", "tauri-utils", "tempfile", - "thiserror", + "thiserror 1.0.69", "time", "ureq", "url", @@ -11296,12 +13339,12 @@ dependencies = [ "memchr", "phf 0.11.2", "regex", - "semver", + "semver 1.0.23", "serde", "serde-untagged", "serde_json", "serde_with", - "thiserror", + "thiserror 1.0.69", "toml", "url", "urlpattern", @@ -11361,6 +13404,17 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "textwrap" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" +dependencies = [ + "smawk", + "unicode-linebreak", + "unicode-width 0.1.14", +] + [[package]] name = "thin-slice" version = "0.1.1" @@ -11379,7 +13433,16 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" +dependencies = [ + "thiserror-impl 2.0.3", ] [[package]] @@ -11393,6 +13456,17 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "thiserror-impl" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "thread_local" version = "1.1.8" @@ -11567,7 +13641,7 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" dependencies = [ - "rustls 0.23.16", + "rustls 0.23.17", "rustls-pki-types", "tokio", ] @@ -11593,7 +13667,7 @@ dependencies = [ "futures-util", "log", "native-tls", - "rustls 0.23.16", + "rustls 0.23.17", "tokio", "tokio-native-tls", "tungstenite 0.23.0", @@ -11914,10 +13988,20 @@ dependencies = [ "objc2-foundation", "once_cell", "png", - "thiserror", + "thiserror 1.0.69", "windows-sys 0.59.0", ] +[[package]] +name = "triomphe" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef8f7726da4807b58ea5c96fdc122f80702030edc33b35aff9190a51148ccc85" +dependencies = [ + "serde", + "stable_deref_trait", +] + [[package]] name = "try-lock" version = "0.2.5" @@ -11957,7 +14041,7 @@ dependencies = [ "rustls-native-certs 0.7.3", "rustls-pki-types", "sha1", - "thiserror", + "thiserror 1.0.69", "url", "utf-8", ] @@ -11977,7 +14061,7 @@ dependencies = [ "native-tls", "rand 0.8.5", "sha1", - "thiserror", + "thiserror 1.0.69", "utf-8", ] @@ -11995,7 +14079,7 @@ dependencies = [ "log", "rand 0.8.5", "sha1", - "thiserror", + "thiserror 1.0.69", "utf-8", ] @@ -12018,6 +14102,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "typed-arena" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" + [[package]] name = "typeid" version = "1.0.2" @@ -12139,12 +14229,30 @@ version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7eec5d1121208364f6793f7d2e222bf75a915c19557537745b195b253dd64217" +[[package]] +name = "unicode-id" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10103c57044730945224467c09f71a4db0071c123a0648cc3e818913bde6b561" + +[[package]] +name = "unicode-id-start" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f322b60f6b9736017344fa0635d64be2f458fbc04eef65f6be22976dd1ffd5b" + [[package]] name = "unicode-ident" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "unicode-linebreak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" + [[package]] name = "unicode-normalization" version = "0.1.24" @@ -12238,7 +14346,7 @@ dependencies = [ "base64 0.22.1", "log", "once_cell", - "rustls 0.23.16", + "rustls 0.23.17", "rustls-pki-types", "socks", "url", @@ -12351,6 +14459,12 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "vlq" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65dd7eed29412da847b0f78bcec0ac98588165988a8cfe41d4ea1d429f8ccfff" + [[package]] name = "vsimd" version = "0.8.0" @@ -12650,7 +14764,7 @@ dependencies = [ "strum 0.24.1", "strum_macros 0.24.3", "tempfile", - "thiserror", + "thiserror 1.0.69", "wasm-opt-cxx-sys", "wasm-opt-sys", ] @@ -12702,7 +14816,7 @@ dependencies = [ "bitflags 2.6.0", "hashbrown 0.14.5", "indexmap 2.6.0", - "semver", + "semver 1.0.23", "serde", ] @@ -12842,7 +14956,7 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3a3e2eeb58f82361c93f9777014668eb3d07e7d174ee4c819575a9208011886" dependencies = [ - "thiserror", + "thiserror 1.0.69", "windows", "windows-core 0.58.0", ] @@ -13332,7 +15446,7 @@ dependencies = [ "sha2", "soup3", "tao-macros", - "thiserror", + "thiserror 1.0.69", "webkit2gtk", "webkit2gtk-sys", "webview2-com", @@ -13409,7 +15523,7 @@ dependencies = [ "ring", "signature 2.2.0", "spki 0.7.3", - "thiserror", + "thiserror 1.0.69", "zeroize", ] @@ -13663,7 +15777,7 @@ dependencies = [ "flate2", "indexmap 2.6.0", "memchr", - "thiserror", + "thiserror 1.0.69", "zopfli", ] @@ -13675,7 +15789,7 @@ checksum = "ce824a6bfffe8942820fa36d24973b7c83a40896749a42e33de0abdd11750ee5" dependencies = [ "byteorder", "bytesize", - "thiserror", + "thiserror 1.0.69", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 346927630a..de05579e6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,8 @@ members = [ "packages/devtools-types", "packages/isrg", "packages/rsx-hotreload", + "packages/const-serialize", + "packages/const-serialize-macro", "packages/dx-wire-format", # Playwright tests @@ -71,9 +73,8 @@ members = [ # manganis "packages/manganis/manganis", - "packages/manganis/manganis-macro", "packages/manganis/manganis-core", - + "packages/manganis/manganis-macro", # Full project examples "example-projects/fullstack-hackernews", @@ -135,6 +136,8 @@ dioxus-devtools = { path = "packages/devtools", version = "0.6.0-alpha.5" } dioxus-devtools-types = { path = "packages/devtools-types", version = "0.6.0-alpha.5" } dioxus-fullstack = { path = "packages/fullstack", version = "0.6.0-alpha.5" } dioxus_server_macro = { path = "packages/server-macro", version = "0.6.0-alpha.5", default-features = false } +const-serialize = { path = "packages/const-serialize", version = "0.6.0-alpha.5" } +const-serialize-macro = { path = "packages/const-serialize-macro", version = "0.6.0-alpha.5" } dioxus-dx-wire-format = { path = "packages/dx-wire-format", version = "0.6.0-alpha.5" } lazy-js-bundle = { path = "packages/lazy-js-bundle", version = "0.6.0-alpha.5" } diff --git a/packages/cli/Cargo.toml b/packages/cli/Cargo.toml index 4174fddeda..30a486d504 100644 --- a/packages/cli/Cargo.toml +++ b/packages/cli/Cargo.toml @@ -88,6 +88,76 @@ prettyplease = { workspace = true } brotli = "6.0.0" ignore = "0.4.22" env_logger = { workspace = true } +const-serialize = { workspace = true, features = ["serde"] } + +# Image compression/conversion +# - JPEG +mozjpeg = { version = "0.10.7", default-features = false, features = [ + "parallel", +] } +# - PNG +imagequant = "4.2.0" +png = "0.17.9" +# Image format/conversion +image = { version = "0.25", features = ["avif"] } + +# CSS Minification +lightningcss = "1.0.0-alpha.60" + +# Js minification - swc has introduces minor versions with breaking changes in the past so we pin all of their crates +swc = "=0.283.0" +swc_allocator = { version = "=0.1.8", default-features = false } +swc_atoms = { version = "=0.6.7", default-features = false } +swc_cached = { version = "=0.3.20", default-features = false } +swc_common = { version = "=0.37.5", default-features = false } +swc_compiler_base = { version = "=0.19.0", default-features = false } +swc_config = { version = "=0.1.15", default-features = false } +swc_config_macro = { version = "=0.1.4", default-features = false } +swc_ecma_ast = { version = "=0.118.2", default-features = false } +swc_ecma_codegen = { version = "=0.155.1", default-features = false } +swc_ecma_codegen_macros = { version = "=0.7.7", default-features = false } +swc_ecma_compat_bugfixes = { version = "=0.12.0", default-features = false } +swc_ecma_compat_common = { version = "=0.11.0", default-features = false } +swc_ecma_compat_es2015 = { version = "=0.12.0", default-features = false } +swc_ecma_compat_es2016 = { version = "=0.12.0", default-features = false } +swc_ecma_compat_es2017 = { version = "=0.12.0", default-features = false } +swc_ecma_compat_es2018 = { version = "=0.12.0", default-features = false } +swc_ecma_compat_es2019 = { version = "=0.12.0", default-features = false } +swc_ecma_compat_es2020 = { version = "=0.12.0", default-features = false } +swc_ecma_compat_es2021 = { version = "=0.12.0", default-features = false } +swc_ecma_compat_es2022 = { version = "=0.12.0", default-features = false } +swc_ecma_compat_es3 = { version = "=0.12.0", default-features = false } +swc_ecma_ext_transforms = { version = "=0.120.0", default-features = false } +swc_ecma_lints = { version = "=0.100.0", default-features = false } +swc_ecma_loader = { version = "=0.49.1", default-features = false } +swc_ecma_minifier = { version = "=0.204.0", default-features = false } +swc_ecma_parser = { version = "=0.149.1", default-features = false } +swc_ecma_preset_env = { version = "=0.217.0", default-features = false, features = [ + "serde", +] } +swc_ecma_transforms = { version = "=0.239.0", default-features = false } +swc_ecma_transforms_base = { version = "=0.145.0", default-features = false } +swc_ecma_transforms_classes = { version = "=0.134.0", default-features = false } +swc_ecma_transforms_compat = { version = "=0.171.0", default-features = false } +swc_ecma_transforms_macros = { version = "=0.5.5", default-features = false } +swc_ecma_transforms_module = { version = "=0.190.0", default-features = false } +swc_ecma_transforms_optimization = { version = "=0.208.0", default-features = false } +swc_ecma_transforms_proposal = { version = "=0.178.0", default-features = false } +swc_ecma_transforms_react = { version = "=0.191.0", default-features = false } +swc_ecma_transforms_typescript = { version = "=0.198.1", default-features = false } +swc_ecma_usage_analyzer = { version = "=0.30.3", default-features = false } +swc_ecma_utils = { version = "=0.134.2", default-features = false } +swc_ecma_visit = { version = "=0.104.8", default-features = false } +swc_eq_ignore_macros = { version = "=0.1.4", default-features = false } +swc_error_reporters = { version = "=0.21.0", default-features = false } +swc_fast_graph = { version = "=0.25.0", default-features = false } +swc_macros_common = { version = "=0.3.13", default-features = false } +swc_node_comments = { version = "=0.24.0", default-features = false } +swc_timer = { version = "=0.25.0", default-features = false } +swc_trace_macro = { version = "=0.1.3", default-features = false } +swc_transform_common = { version = "=0.1.1", default-features = false } +swc_typescript = { version = "=0.5.0", default-features = false } +swc_visit = { version = "=0.6.2", default-features = false } tracing-subscriber = { version = "0.3.18", features = ["std", "env-filter", "json"] } console-subscriber = { version = "0.3.0", optional = true } diff --git a/packages/cli/src/assets/css.rs b/packages/cli/src/assets/css.rs new file mode 100644 index 0000000000..f0e3dd1558 --- /dev/null +++ b/packages/cli/src/assets/css.rs @@ -0,0 +1,42 @@ +use std::path::Path; + +use anyhow::Context; +use lightningcss::{ + printer::PrinterOptions, + stylesheet::{MinifyOptions, ParserOptions, StyleSheet}, +}; +use manganis_core::CssAssetOptions; + +pub(crate) fn process_css( + css_options: &CssAssetOptions, + source: &Path, + output_path: &Path, +) -> anyhow::Result<()> { + let css = std::fs::read_to_string(source)?; + + let css = if css_options.minified() { + minify_css(&css) + } else { + css + }; + + std::fs::write(output_path, css).with_context(|| { + format!( + "Failed to write css to output location: {}", + output_path.display() + ) + })?; + + Ok(()) +} + +pub(crate) fn minify_css(css: &str) -> String { + let mut stylesheet = StyleSheet::parse(css, ParserOptions::default()).unwrap(); + stylesheet.minify(MinifyOptions::default()).unwrap(); + let printer = PrinterOptions { + minify: true, + ..Default::default() + }; + let res = stylesheet.to_css(printer).unwrap(); + res.code +} diff --git a/packages/cli/src/assets/file.rs b/packages/cli/src/assets/file.rs new file mode 100644 index 0000000000..1fc6fa0c54 --- /dev/null +++ b/packages/cli/src/assets/file.rs @@ -0,0 +1,75 @@ +use anyhow::Context; +use manganis_core::{AssetOptions, CssAssetOptions, ImageAssetOptions, JsAssetOptions}; +use std::path::Path; + +use super::{ + css::process_css, folder::process_folder, image::process_image, js::process_js, + json::process_json, +}; + +/// Process a specific file asset with the given options reading from the source and writing to the output path +pub(crate) fn process_file_to( + options: &AssetOptions, + source: &Path, + output_path: &Path, +) -> anyhow::Result<()> { + // If the file already exists, then we must have a file with the same hash + // already. The hash has the file contents and options, so if we find a file + // with the same hash, we probably already created this file in the past + if output_path.exists() { + return Ok(()); + } + if let Some(parent) = output_path.parent() { + if !parent.exists() { + std::fs::create_dir_all(parent)?; + } + } + match options { + AssetOptions::Unknown => match source.extension().map(|e| e.to_string_lossy()).as_deref() { + Some("css") => { + process_css(&CssAssetOptions::new(), source, output_path)?; + } + Some("js") => { + process_js(&JsAssetOptions::new(), source, output_path)?; + } + Some("json") => { + process_json(source, output_path)?; + } + Some("jpg" | "jpeg" | "png" | "webp" | "avif") => { + process_image(&ImageAssetOptions::new(), source, output_path)?; + } + None if source.is_dir() => { + process_folder(source, output_path)?; + } + Some(_) | None => { + let source_file = std::fs::File::open(source)?; + let mut reader = std::io::BufReader::new(source_file); + let output_file = std::fs::File::create(output_path)?; + let mut writer = std::io::BufWriter::new(output_file); + std::io::copy(&mut reader, &mut writer).with_context(|| { + format!( + "Failed to write file to output location: {}", + output_path.display() + ) + })?; + } + }, + AssetOptions::Css(options) => { + process_css(options, source, output_path)?; + } + AssetOptions::Js(options) => { + process_js(options, source, output_path)?; + } + AssetOptions::Image(options) => { + process_image(options, source, output_path)?; + } + AssetOptions::Folder(_) => { + process_folder(source, output_path)?; + } + _ => { + tracing::warn!("Unknown asset options: {:?}", options); + } + } + + Ok(()) +} diff --git a/packages/cli/src/assets/folder.rs b/packages/cli/src/assets/folder.rs new file mode 100644 index 0000000000..699f6d64d8 --- /dev/null +++ b/packages/cli/src/assets/folder.rs @@ -0,0 +1,41 @@ +use std::path::Path; + +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; + +use super::file::process_file_to; + +/// Process a folder, optimizing and copying all assets into the output folder +pub fn process_folder(source: &Path, output_folder: &Path) -> anyhow::Result<()> { + // Create the folder + std::fs::create_dir_all(output_folder)?; + + // Then optimize children + let files: Vec<_> = std::fs::read_dir(source) + .into_iter() + .flatten() + .flatten() + .collect(); + + files.par_iter().try_for_each(|file| { + let file = file.path(); + let metadata = file.metadata()?; + let output_path = output_folder.join(file.strip_prefix(source)?); + if metadata.is_dir() { + process_folder(&file, &output_path) + } else { + process_file_minimal(&file, &output_path) + } + })?; + + Ok(()) +} + +/// Optimize a file without changing any of its contents significantly (e.g. by changing the extension) +fn process_file_minimal(input_path: &Path, output_path: &Path) -> anyhow::Result<()> { + process_file_to( + &manganis_core::AssetOptions::Unknown, + input_path, + output_path, + )?; + Ok(()) +} diff --git a/packages/cli/src/assets/image/jpg.rs b/packages/cli/src/assets/image/jpg.rs new file mode 100644 index 0000000000..abe0c6bcfd --- /dev/null +++ b/packages/cli/src/assets/image/jpg.rs @@ -0,0 +1,23 @@ +use image::{DynamicImage, EncodableLayout}; +use std::{ + io::{BufWriter, Write}, + path::Path, +}; + +pub(crate) fn compress_jpg(image: DynamicImage, output_location: &Path) -> anyhow::Result<()> { + let mut comp = mozjpeg::Compress::new(mozjpeg::ColorSpace::JCS_EXT_RGBX); + let width = image.width() as usize; + let height = image.height() as usize; + + comp.set_size(width, height); + let mut comp = comp.start_compress(Vec::new())?; // any io::Write will work + + comp.write_scanlines(image.to_rgba8().as_bytes())?; + + let jpeg_bytes = comp.finish()?; + + let file = std::fs::File::create(output_location)?; + let w = &mut BufWriter::new(file); + w.write_all(&jpeg_bytes)?; + Ok(()) +} diff --git a/packages/cli/src/assets/image/mod.rs b/packages/cli/src/assets/image/mod.rs new file mode 100644 index 0000000000..4ab0087ae5 --- /dev/null +++ b/packages/cli/src/assets/image/mod.rs @@ -0,0 +1,62 @@ +use std::path::Path; + +use anyhow::Context; +use jpg::compress_jpg; +use manganis_core::{ImageAssetOptions, ImageFormat, ImageSize}; +use png::compress_png; + +mod jpg; +mod png; + +pub(crate) fn process_image( + image_options: &ImageAssetOptions, + source: &Path, + output_path: &Path, +) -> anyhow::Result<()> { + let mut image = image::ImageReader::new(std::io::Cursor::new(&*std::fs::read(source)?)) + .with_guessed_format()? + .decode(); + + if let Ok(image) = &mut image { + if let ImageSize::Manual { width, height } = image_options.size() { + *image = image.resize_exact(width, height, image::imageops::FilterType::Lanczos3); + } + } + + match (image, image_options.format()) { + (image, ImageFormat::Png) => { + compress_png(image?, output_path); + } + (image, ImageFormat::Jpg) => { + compress_jpg(image?, output_path)?; + } + (Ok(image), ImageFormat::Avif) => { + if let Err(error) = image.save(output_path) { + tracing::error!("Failed to save avif image: {} with path {}. You must have the avif feature enabled to use avif assets", error, output_path.display()); + } + } + (Ok(image), ImageFormat::Webp) => { + if let Err(err) = image.save(output_path) { + tracing::error!("Failed to save webp image: {}. You must have the avif feature enabled to use webp assets", err); + } + } + (Ok(image), _) => { + image.save(output_path)?; + } + // If we can't decode the image or it is of an unknown type, we just copy the file + _ => { + let source_file = std::fs::File::open(source)?; + let mut reader = std::io::BufReader::new(source_file); + let output_file = std::fs::File::create(output_path)?; + let mut writer = std::io::BufWriter::new(output_file); + std::io::copy(&mut reader, &mut writer).with_context(|| { + format!( + "Failed to write image to output location: {}", + output_path.display() + ) + })?; + } + } + + Ok(()) +} diff --git a/packages/cli/src/assets/image/png.rs b/packages/cli/src/assets/image/png.rs new file mode 100644 index 0000000000..039e64cdcf --- /dev/null +++ b/packages/cli/src/assets/image/png.rs @@ -0,0 +1,52 @@ +use std::{io::BufWriter, path::Path}; + +use image::DynamicImage; + +pub(crate) fn compress_png(image: DynamicImage, output_location: &Path) { + // Image loading/saving is outside scope of this library + let width = image.width() as usize; + let height = image.height() as usize; + let bitmap: Vec<_> = image + .into_rgba8() + .pixels() + .map(|px| imagequant::RGBA::new(px[0], px[1], px[2], px[3])) + .collect(); + + // Configure the library + let mut liq = imagequant::new(); + liq.set_speed(5).unwrap(); + liq.set_quality(0, 99).unwrap(); + + // Describe the bitmap + let mut img = liq.new_image(&bitmap[..], width, height, 0.0).unwrap(); + + // The magic happens in quantize() + let mut res = match liq.quantize(&mut img) { + Ok(res) => res, + Err(err) => panic!("Quantization failed, because: {err:?}"), + }; + + let (palette, pixels) = res.remapped(&mut img).unwrap(); + + let file = std::fs::File::create(output_location).unwrap(); + let w = &mut BufWriter::new(file); + + let mut encoder = png::Encoder::new(w, width as u32, height as u32); + encoder.set_color(png::ColorType::Rgba); + let mut flattened_palette = Vec::new(); + let mut alpha_palette = Vec::new(); + for px in palette { + flattened_palette.push(px.r); + flattened_palette.push(px.g); + flattened_palette.push(px.b); + alpha_palette.push(px.a); + } + encoder.set_palette(flattened_palette); + encoder.set_trns(alpha_palette); + encoder.set_depth(png::BitDepth::Eight); + encoder.set_color(png::ColorType::Indexed); + encoder.set_compression(png::Compression::Best); + let mut writer = encoder.write_header().unwrap(); + writer.write_image_data(&pixels).unwrap(); + writer.finish().unwrap(); +} diff --git a/packages/cli/src/assets/js.rs b/packages/cli/src/assets/js.rs new file mode 100644 index 0000000000..0dc2b86d06 --- /dev/null +++ b/packages/cli/src/assets/js.rs @@ -0,0 +1,69 @@ +use std::io::Read; +use std::path::Path; +use std::sync::Arc; + +use anyhow::Context; +use manganis_core::JsAssetOptions; +use swc::{config::JsMinifyOptions, try_with_handler, BoolOrDataConfig}; +use swc_common::{sync::Lrc, FileName}; +use swc_common::{SourceMap, GLOBALS}; + +pub(crate) fn minify_js(source: &Path) -> anyhow::Result { + let mut source_file = std::fs::File::open(source)?; + let cm = Arc::new(SourceMap::default()); + + let mut js = String::new(); + source_file.read_to_string(&mut js)?; + let c = swc::Compiler::new(cm.clone()); + let output = GLOBALS + .set(&Default::default(), || { + try_with_handler(cm.clone(), Default::default(), |handler| { + let filename = Lrc::new(FileName::Real(source.to_path_buf())); + let fm = cm.new_source_file(filename, js.to_string()); + + c.minify( + fm, + handler, + &JsMinifyOptions { + compress: BoolOrDataConfig::from_bool(true), + mangle: BoolOrDataConfig::from_bool(true), + ..Default::default() + }, + ) + .context("failed to minify javascript") + }) + }) + .map(|output| output.code); + + match output { + Ok(output) => Ok(output), + Err(err) => { + tracing::error!("Failed to minify javascript: {}", err); + Ok(js) + } + } +} + +pub(crate) fn process_js( + js_options: &JsAssetOptions, + source: &Path, + output_path: &Path, +) -> anyhow::Result<()> { + let js = if js_options.minified() { + minify_js(source)? + } else { + let mut source_file = std::fs::File::open(source)?; + let mut source = String::new(); + source_file.read_to_string(&mut source)?; + source + }; + + std::fs::write(output_path, js).with_context(|| { + format!( + "Failed to write js to output location: {}", + output_path.display() + ) + })?; + + Ok(()) +} diff --git a/packages/cli/src/assets/json.rs b/packages/cli/src/assets/json.rs new file mode 100644 index 0000000000..40400e6a9e --- /dev/null +++ b/packages/cli/src/assets/json.rs @@ -0,0 +1,33 @@ +use std::{io::Read, path::Path}; + +use anyhow::Context; + +pub(crate) fn minify_json(source: &str) -> anyhow::Result { + // First try to parse the json + let json: serde_json::Value = serde_json::from_str(source)?; + // Then print it in a minified format + let json = serde_json::to_string(&json)?; + Ok(json) +} + +pub(crate) fn process_json(source: &Path, output_path: &Path) -> anyhow::Result<()> { + let mut source_file = std::fs::File::open(source)?; + let mut source = String::new(); + source_file.read_to_string(&mut source)?; + let json = match minify_json(&source) { + Ok(json) => json, + Err(err) => { + tracing::error!("Failed to minify json: {}", err); + source + } + }; + + std::fs::write(output_path, json).with_context(|| { + format!( + "Failed to write json to output location: {}", + output_path.display() + ) + })?; + + Ok(()) +} diff --git a/packages/cli/src/assets.rs b/packages/cli/src/assets/mod.rs similarity index 79% rename from packages/cli/src/assets.rs rename to packages/cli/src/assets/mod.rs index 3384058343..4a0f2ff652 100644 --- a/packages/cli/src/assets.rs +++ b/packages/cli/src/assets/mod.rs @@ -1,24 +1,34 @@ use anyhow::Context; -use manganis_core::{LinkSection, ResourceAsset}; +use manganis_core::linker::LinkSection; +use manganis_core::BundledAsset; use object::{read::archive::ArchiveFile, File as ObjectFile, Object, ObjectSection}; use serde::{Deserialize, Serialize}; use std::path::Path; use std::{collections::HashMap, path::PathBuf}; +mod css; +mod file; +mod folder; +mod image; +mod js; +mod json; + +pub(crate) use file::process_file_to; + /// A manifest of all assets collected from dependencies /// /// This will be filled in primarily by incremental compilation artifacts. #[derive(Debug, PartialEq, Default, Clone, Serialize, Deserialize)] pub(crate) struct AssetManifest { /// Map of bundled asset name to the asset itself - pub(crate) assets: HashMap, + pub(crate) assets: HashMap, } impl AssetManifest { #[allow(dead_code)] pub(crate) fn load_from_file(path: &Path) -> anyhow::Result { - let src = std::fs::read_to_string(path) - .context("Failed to read asset manifest from filesystem")?; + let src = std::fs::read_to_string(path)?; + serde_json::from_str(&src) .with_context(|| format!("Failed to parse asset manifest from {path:?}\n{src}")) } @@ -44,7 +54,7 @@ impl AssetManifest { Ok(()) } - /// Fill this manifest from an rlib / ar file that contains many object files and their entryies + /// Fill this manifest from an rlib / ar file that contains many object files and their entries fn add_from_archive_file(&mut self, archive: &ArchiveFile, data: &[u8]) -> object::Result<()> { // Look through each archive member for object files. // Read the archive member's binary data (we know it's an object file) @@ -84,17 +94,13 @@ impl AssetManifest { .uncompressed_data() .context("Could not read uncompressed data from object file")?; - let as_str = std::str::from_utf8(&bytes) - .context("object file contained non utf8 encoding")? - .chars() - .filter(|c| !c.is_control()) - .collect::(); - - let assets = serde_json::Deserializer::from_str(&as_str).into_iter::(); - for as_resource in assets.flatten() { - // Some platforms (e.g. macOS) start the manganis section with a null byte, we need to filter that out before we deserialize the JSON + let mut buffer = const_serialize::ConstReadBuffer::new(&bytes); + while let Some((remaining_buffer, asset)) = + const_serialize::deserialize_const!(BundledAsset, buffer) + { self.assets - .insert(as_resource.absolute.clone(), as_resource); + .insert(asset.absolute_source_path().into(), asset); + buffer = remaining_buffer; } } diff --git a/packages/cli/src/build/bundle.rs b/packages/cli/src/build/bundle.rs index e4c6fde53f..cacd6f6af6 100644 --- a/packages/cli/src/build/bundle.rs +++ b/packages/cli/src/build/bundle.rs @@ -1,12 +1,14 @@ +use crate::assets::process_file_to; use crate::Result; use crate::{assets::AssetManifest, TraceSrc}; use crate::{BuildRequest, Platform}; use anyhow::Context; +use manganis_core::AssetOptions; use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; -use std::{ - fs::create_dir_all, - path::{Path, PathBuf}, -}; +use std::collections::HashSet; +use std::future::Future; +use std::path::{Path, PathBuf}; +use std::pin::Pin; use std::{sync::atomic::AtomicUsize, time::Duration}; use tokio::process::Command; use wasm_bindgen_cli_support::Bindgen; @@ -377,21 +379,54 @@ impl AppBundle { let asset_dir = self.build.asset_dir(); - // First, clear the asset dir - // todo(jon): cache the asset dir, removing old files and only copying new ones that changed since the last build - _ = std::fs::remove_dir_all(&asset_dir); - _ = create_dir_all(&asset_dir); + // First, clear the asset dir of any files that don't exist in the new manifest + _ = tokio::fs::create_dir_all(&asset_dir).await; + // Create a set of all the paths that new files will be bundled to + let bundled_output_paths: HashSet<_> = self + .app + .assets + .assets + .values() + .map(|a| asset_dir.join(a.bundled_path())) + .collect(); + // one possible implementation of walking a directory only visiting files + fn remove_old_assets<'a>( + path: &'a Path, + bundled_output_paths: &'a HashSet, + ) -> Pin> + Send + 'a>> { + Box::pin(async move { + // If this asset is in the manifest, we don't need to remove it + if bundled_output_paths.contains(path.canonicalize()?.as_path()) { + return Ok(()); + } + // Otherwise, if it is a directory, we need to walk it and remove child files + if path.is_dir() { + for entry in std::fs::read_dir(path)?.flatten() { + let path = entry.path(); + remove_old_assets(&path, bundled_output_paths).await?; + } + if path.read_dir()?.next().is_none() { + // If the directory is empty, remove it + tokio::fs::remove_dir(path).await?; + } + } else { + // If it is a file, remove it + tokio::fs::remove_file(path).await?; + } + Ok(()) + }) + } + remove_old_assets(&asset_dir, &bundled_output_paths).await?; // todo(jon): we also want to eventually include options for each asset's optimization and compression, which we currently aren't let mut assets_to_transfer = vec![]; // Queue the bundled assets - for asset in self.app.assets.assets.keys() { - let bundled = self.app.assets.assets.get(asset).unwrap(); - let from = bundled.absolute.clone(); - let to = asset_dir.join(&bundled.bundled); + for (asset, bundled) in &self.app.assets.assets { + let from = asset.clone(); + let to = asset_dir.join(bundled.bundled_path()); tracing::debug!("Copying asset {from:?} to {to:?}"); - assets_to_transfer.push((from, to)); + assets_to_transfer.push((from, to, *bundled.options())); } // And then queue the legacy assets @@ -399,35 +434,42 @@ impl AppBundle { for from in self.build.krate.legacy_asset_dir_files() { let to = asset_dir.join(from.file_name().unwrap()); tracing::debug!("Copying legacy asset {from:?} to {to:?}"); - assets_to_transfer.push((from, to)); + assets_to_transfer.push((from, to, AssetOptions::Unknown)); } let asset_count = assets_to_transfer.len(); let assets_finished = AtomicUsize::new(0); // Parallel Copy over the assets and keep track of progress with an atomic counter - // todo: we want to use the fastfs variant that knows how to parallelize folders, too - assets_to_transfer.par_iter().try_for_each(|(from, to)| { - tracing::trace!( - "Starting asset copy {current}/{asset_count} from {from:?}", - current = assets_finished.fetch_add(0, std::sync::atomic::Ordering::SeqCst), - ); - - // todo(jon): implement optimize + pre_compress on the asset type - let res = crate::fastfs::copy_asset(from, to); - - if let Err(err) = res.as_ref() { - tracing::error!("Failed to copy asset {from:?}: {err}"); - } - - self.build.status_copied_asset( - assets_finished.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1, - asset_count, - from.clone(), - ); - - res.map(|_| ()) - })?; + let progress = self.build.progress.clone(); + // Optimizing assets is expensive and blocking, so we do it in a tokio spawn blocking task + tokio::task::spawn_blocking(move || { + assets_to_transfer + .par_iter() + .try_for_each(|(from, to, options)| { + tracing::trace!( + "Starting asset copy {current}/{asset_count} from {from:?}", + current = assets_finished.fetch_add(0, std::sync::atomic::Ordering::SeqCst), + ); + + let res = process_file_to(options, from, to); + + if let Err(err) = res.as_ref() { + tracing::error!("Failed to copy asset {from:?}: {err}"); + } + + BuildRequest::status_copied_asset( + &progress, + assets_finished.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1, + asset_count, + from.to_path_buf(), + ); + + res.map(|_| ()) + }) + }) + .await + .map_err(|e| anyhow::anyhow!("A task failed while trying to copy assets: {e}"))??; Ok(()) } diff --git a/packages/cli/src/build/progress.rs b/packages/cli/src/build/progress.rs index 66a0430e18..f07b22c5b6 100644 --- a/packages/cli/src/build/progress.rs +++ b/packages/cli/src/build/progress.rs @@ -59,8 +59,13 @@ impl BuildRequest { }); } - pub(crate) fn status_copied_asset(&self, current: usize, total: usize, path: PathBuf) { - _ = self.progress.unbounded_send(BuildUpdate::Progress { + pub(crate) fn status_copied_asset( + progress: &UnboundedSender, + current: usize, + total: usize, + path: PathBuf, + ) { + _ = progress.unbounded_send(BuildUpdate::Progress { stage: BuildStage::CopyingAssets { current, total, diff --git a/packages/cli/src/build/request.rs b/packages/cli/src/build/request.rs index 35b7f24719..368d2d21fc 100644 --- a/packages/cli/src/build/request.rs +++ b/packages/cli/src/build/request.rs @@ -558,7 +558,7 @@ impl BuildRequest { static INITIALIZED: OnceCell> = OnceCell::new(); let success = INITIALIZED.get_or_init(|| { - _ = remove_dir_all(self.root_dir()); + _ = remove_dir_all(self.exe_dir()); create_dir_all(self.root_dir())?; create_dir_all(self.exe_dir())?; diff --git a/packages/cli/src/serve/handle.rs b/packages/cli/src/serve/handle.rs index 3a4f4eaa9b..7f9683c346 100644 --- a/packages/cli/src/serve/handle.rs +++ b/packages/cli/src/serve/handle.rs @@ -181,8 +181,8 @@ impl AppHandle { // The asset might've been renamed thanks to the manifest, let's attempt to reload that too if let Some(resource) = self.app.app.assets.assets.get(changed_file).as_ref() { - let res = std::fs::copy(changed_file, asset_dir.join(&resource.bundled)); - bundled_name = Some(PathBuf::from(&resource.bundled)); + let res = std::fs::copy(changed_file, asset_dir.join(resource.bundled_path())); + bundled_name = Some(PathBuf::from(resource.bundled_path())); if let Err(e) = res { tracing::debug!("Failed to hotreload asset {e}"); } diff --git a/packages/const-serialize-macro/Cargo.toml b/packages/const-serialize-macro/Cargo.toml new file mode 100644 index 0000000000..9440d8c67f --- /dev/null +++ b/packages/const-serialize-macro/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "const-serialize-macro" +version = { workspace = true } +authors = ["Evan Almloff"] +edition = "2021" +description = "A macro to derive const serialize" +license = "MIT OR Apache-2.0" +repository = "https://github.com/dioxuslabs/dioxus" +homepage = "https://dioxuslabs.com/learn/0.5/getting_started" +keywords = ["const", "serialize"] + +[dependencies] +syn = "2.0" +quote = "1.0" +proc-macro2 = "1.0.86" + +[lib] +proc-macro = true diff --git a/packages/const-serialize-macro/src/lib.rs b/packages/const-serialize-macro/src/lib.rs new file mode 100644 index 0000000000..b8015bdbcc --- /dev/null +++ b/packages/const-serialize-macro/src/lib.rs @@ -0,0 +1,207 @@ +use proc_macro::TokenStream; +use quote::{quote, ToTokens}; +use syn::{parse_macro_input, DeriveInput, LitInt}; +use syn::{parse_quote, Generics, WhereClause, WherePredicate}; + +fn add_bounds(where_clause: &mut Option, generics: &Generics) { + let bounds = generics.params.iter().filter_map(|param| match param { + syn::GenericParam::Type(ty) => { + Some::(parse_quote! { #ty: const_serialize::SerializeConst, }) + } + syn::GenericParam::Lifetime(_) => None, + syn::GenericParam::Const(_) => None, + }); + if let Some(clause) = where_clause { + clause.predicates.extend(bounds); + } else { + *where_clause = Some(parse_quote! { where #(#bounds)* }); + } +} + +/// Derive the const serialize trait for a struct +#[proc_macro_derive(SerializeConst)] +pub fn derive_parse(input: TokenStream) -> TokenStream { + // Parse the input tokens into a syntax tree + let input = parse_macro_input!(input as DeriveInput); + + match input.data { + syn::Data::Struct(data) => match data.fields { + syn::Fields::Unnamed(_) | syn::Fields::Named(_) => { + let ty = &input.ident; + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + let mut where_clause = where_clause.cloned(); + add_bounds(&mut where_clause, &input.generics); + let field_names = data.fields.iter().enumerate().map(|(i, field)| { + field + .ident + .as_ref() + .map(|ident| ident.to_token_stream()) + .unwrap_or_else(|| { + LitInt::new(&i.to_string(), proc_macro2::Span::call_site()) + .into_token_stream() + }) + }); + let field_types = data.fields.iter().map(|field| &field.ty); + quote! { + unsafe impl #impl_generics const_serialize::SerializeConst for #ty #ty_generics #where_clause { + const MEMORY_LAYOUT: const_serialize::Layout = const_serialize::Layout::Struct(const_serialize::StructLayout::new( + std::mem::size_of::(), + &[#( + const_serialize::StructFieldLayout::new( + std::mem::offset_of!(#ty, #field_names), + <#field_types as const_serialize::SerializeConst>::MEMORY_LAYOUT, + ), + )*], + )); + } + }.into() + } + syn::Fields::Unit => { + let ty = &input.ident; + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + let mut where_clause = where_clause.cloned(); + add_bounds(&mut where_clause, &input.generics); + quote! { + unsafe impl #impl_generics const_serialize::SerializeConst for #ty #ty_generics #where_clause { + const MEMORY_LAYOUT: const_serialize::Layout = const_serialize::Layout::Struct(const_serialize::StructLayout::new( + std::mem::size_of::(), + &[], + )); + } + }.into() + } + }, + syn::Data::Enum(data) => match data.variants.len() { + 0 => syn::Error::new(input.ident.span(), "Enums must have at least one variant") + .to_compile_error() + .into(), + 1.. => { + let mut repr_c = false; + let mut discriminant_size = None; + for attr in &input.attrs { + if attr.path().is_ident("repr") { + if let Err(err) = attr.parse_nested_meta(|meta| { + // #[repr(C)] + if meta.path.is_ident("C") { + repr_c = true; + return Ok(()); + } + + // #[repr(u8)] + if meta.path.is_ident("u8") { + discriminant_size = Some(1); + return Ok(()); + } + + // #[repr(u16)] + if meta.path.is_ident("u16") { + discriminant_size = Some(2); + return Ok(()); + } + + // #[repr(u32)] + if meta.path.is_ident("u32") { + discriminant_size = Some(3); + return Ok(()); + } + + // #[repr(u64)] + if meta.path.is_ident("u64") { + discriminant_size = Some(4); + return Ok(()); + } + + Err(meta.error("unrecognized repr")) + }) { + return err.to_compile_error().into(); + } + } + } + + let variants_have_fields = data + .variants + .iter() + .any(|variant| !variant.fields.is_empty()); + if !repr_c && variants_have_fields { + return syn::Error::new(input.ident.span(), "Enums must be repr(C, u*)") + .to_compile_error() + .into(); + } + + if discriminant_size.is_none() { + return syn::Error::new(input.ident.span(), "Enums must be repr(u*)") + .to_compile_error() + .into(); + } + + let ty = &input.ident; + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + let mut where_clause = where_clause.cloned(); + add_bounds(&mut where_clause, &input.generics); + let mut last_discriminant = None; + let variants = data.variants.iter().map(|variant| { + let discriminant = variant + .discriminant + .as_ref() + .map(|(_, discriminant)| discriminant.to_token_stream()) + .unwrap_or_else(|| match &last_discriminant { + Some(discriminant) => quote! { #discriminant + 1 }, + None => { + quote! { 0 } + } + }); + last_discriminant = Some(discriminant.clone()); + let field_names = variant.fields.iter().enumerate().map(|(i, field)| { + field + .ident + .clone() + .unwrap_or_else(|| quote::format_ident!("__field_{}", i)) + }); + let field_types = variant.fields.iter().map(|field| &field.ty); + let generics = &input.generics; + quote! { + { + #[allow(unused)] + #[derive(const_serialize::SerializeConst)] + #[repr(C)] + struct VariantStruct #generics { + #( + #field_names: #field_types, + )* + } + const_serialize::EnumVariant::new( + #discriminant as u32, + match VariantStruct::MEMORY_LAYOUT { + const_serialize::Layout::Struct(layout) => layout, + _ => panic!("VariantStruct::MEMORY_LAYOUT must be a struct"), + }, + std::mem::align_of::(), + ) + } + } + }); + quote! { + unsafe impl #impl_generics const_serialize::SerializeConst for #ty #ty_generics #where_clause { + const MEMORY_LAYOUT: const_serialize::Layout = const_serialize::Layout::Enum(const_serialize::EnumLayout::new( + std::mem::size_of::(), + const_serialize::PrimitiveLayout::new( + #discriminant_size as usize, + ), + { + const DATA: &'static [const_serialize::EnumVariant] = &[ + #( + #variants, + )* + ]; + DATA + }, + )); + } + }.into() + } + }, + _ => syn::Error::new(input.ident.span(), "Only structs and enums are supported") + .to_compile_error() + .into(), + } +} diff --git a/packages/const-serialize/.gitignore b/packages/const-serialize/.gitignore new file mode 100644 index 0000000000..1de565933b --- /dev/null +++ b/packages/const-serialize/.gitignore @@ -0,0 +1 @@ +target \ No newline at end of file diff --git a/packages/const-serialize/Cargo.toml b/packages/const-serialize/Cargo.toml new file mode 100644 index 0000000000..2133f802ff --- /dev/null +++ b/packages/const-serialize/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "const-serialize" +version = { workspace = true } +authors = ["Evan Almloff"] +edition = "2021" +description = "A serialization framework that works in const contexts" +license = "MIT OR Apache-2.0" +repository = "https://github.com/dioxuslabs/dioxus" +homepage = "https://dioxuslabs.com/learn/0.5/getting_started" +keywords = ["const", "serialize"] + +[dependencies] +const-serialize-macro = { workspace = true } +serde = { workspace = true, features = ["derive"], optional = true } + +[dev-dependencies] +const-serialize = { path = "." } +rand.workspace = true + +[features] +test-big-endian = [] +serde = ["dep:serde"] diff --git a/packages/const-serialize/README.md b/packages/const-serialize/README.md new file mode 100644 index 0000000000..c1291ae4d2 --- /dev/null +++ b/packages/const-serialize/README.md @@ -0,0 +1,57 @@ +A rust serialization library that works in const with complex(ish) types like enums, nested structs and arrays. Const rust does not have an allocator, so this library cannot work in a cross architecture environment with Vecs, slices or strings. + +```rust +use const_serialize::{deserialize_const, serialize_const, serialize_eq, ConstVec, SerializeConst}; +#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] +struct Struct { + a: u32, + b: u8, + c: u32, + d: Enum, +} + +#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] +#[repr(C, u8)] +enum Enum { + A { one: u32, two: u16 }, + B { one: u8, two: u16 } = 15, +} + +const { + let data = [Struct { + a: 0x11111111, + b: 0x22, + c: 0x33333333, + d: Enum::A { + one: 0x44444444, + two: 0x5555, + }, + }; 3]; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + let buf = buf.read(); + let (buf, deserialized) = match deserialize_const!([Struct; 3], buf) { + Some(data) => data, + None => panic!("data mismatch"), + }; + if !serialize_eq(&data, &deserialized) { + panic!("data mismatch"); + } +} +``` + +## How it works + +`const-serialize` relies heavily on well defined layouts for the types you want to serialize. The serialization format is the linear sequence of unaligned bytes stored in the order of the fields, items or variants of the type. Numbers are stored in little endian order. + +In order to support complex nested types, serialization is done using a trait. Since functions in traits cannot be const, `const-serialize` uses a macro to generate constant associated items that describe the memory layout of the type. That layout is then used to read all of the bytes in the type into the serialized buffer. + +The deserialization is done in a similar way, but the layout is used to write the bytes from the serialized buffer into the type. + +The rust [nomicon](https://doc.rust-lang.org/nomicon/data.html) defines the memory layout of different types. It is used as a reference for the layout of the types implemented in `const-serialize`. + +## Limitations + +- Only constant sized types are supported. This means that you can't serialize a type like `Vec`. These types are difficult to create in const contexts in general +- Only types with a well defined memory layout are supported (see https://github.com/rust-lang/rfcs/pull/3727 and https://onevariable.com/blog/pods-from-scratch). `repr(Rust)` enums don't have a well defined layout, so they are not supported. `repr(C, u8)` enums can be used instead +- Const rust does not support mutable references or points, so this crate leans heavily on function data structures for data processing. diff --git a/packages/const-serialize/src/const_buffers.rs b/packages/const-serialize/src/const_buffers.rs new file mode 100644 index 0000000000..c230a6d8aa --- /dev/null +++ b/packages/const-serialize/src/const_buffers.rs @@ -0,0 +1,33 @@ +/// A buffer that can be read from at compile time. This is very similar to [Cursor](std::io::Cursor) but is +/// designed to be used in const contexts. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct ConstReadBuffer<'a> { + location: usize, + memory: &'a [u8], +} + +impl<'a> ConstReadBuffer<'a> { + /// Create a new buffer from a byte slice + pub const fn new(memory: &'a [u8]) -> Self { + Self { + location: 0, + memory, + } + } + + /// Get the next byte from the buffer. Returns `None` if the buffer is empty. + /// This will return the new version of the buffer with the first byte removed. + pub const fn get(mut self) -> Option<(Self, u8)> { + if self.location >= self.memory.len() { + return None; + } + let value = self.memory[self.location]; + self.location += 1; + Some((self, value)) + } + + /// Get a reference to the underlying byte slice + pub const fn as_ref(&self) -> &[u8] { + self.memory + } +} diff --git a/packages/const-serialize/src/const_vec.rs b/packages/const-serialize/src/const_vec.rs new file mode 100644 index 0000000000..5535b31a1d --- /dev/null +++ b/packages/const-serialize/src/const_vec.rs @@ -0,0 +1,422 @@ +#![allow(dead_code)] +use std::{fmt::Debug, hash::Hash, mem::MaybeUninit}; + +use crate::ConstReadBuffer; + +const DEFAULT_MAX_SIZE: usize = 2usize.pow(10); + +/// [`ConstVec`] is a version of [`Vec`] that is usable in const contexts. It has +/// a fixed maximum size, but it can can grow and shrink within that size limit +/// as needed. +/// +/// # Example +/// ```rust +/// # use const_serialize::ConstVec; +/// const EMPTY: ConstVec = ConstVec::new(); +/// // Methods that mutate the vector will return a new vector +/// const ONE: ConstVec = EMPTY.push(1); +/// const TWO: ConstVec = ONE.push(2); +/// const THREE: ConstVec = TWO.push(3); +/// const FOUR: ConstVec = THREE.push(4); +/// // If a value is also returned, that will be placed in a tuple in the return value +/// // along with the new vector +/// const POPPED: (ConstVec, Option) = FOUR.pop(); +/// assert_eq!(POPPED.0, THREE); +/// assert_eq!(POPPED.1.unwrap(), 4); +/// ``` +pub struct ConstVec { + memory: [MaybeUninit; MAX_SIZE], + len: u32, +} + +impl Clone for ConstVec { + fn clone(&self) -> Self { + let mut cloned = Self::new_with_max_size(); + for i in 0..self.len as usize { + cloned = cloned.push(self.get(i).unwrap().clone()); + } + cloned + } +} + +impl Copy for ConstVec {} + +impl PartialEq for ConstVec { + fn eq(&self, other: &Self) -> bool { + self.as_ref() == other.as_ref() + } +} + +impl Hash for ConstVec { + fn hash(&self, state: &mut H) { + self.as_ref().hash(state) + } +} + +impl Default for ConstVec { + fn default() -> Self { + Self::new_with_max_size() + } +} + +impl Debug for ConstVec { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ConstVec") + .field("len", &self.len) + .field("memory", &self.as_ref()) + .finish() + } +} + +impl ConstVec { + /// Create a new empty [`ConstVec`] + pub const fn new() -> Self { + Self::new_with_max_size() + } +} + +impl ConstVec { + /// Create a new empty [`ConstVec`] with a custom maximum size + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new_with_max_size(); + /// ``` + pub const fn new_with_max_size() -> Self { + Self { + memory: [const { MaybeUninit::uninit() }; MAX_SIZE], + len: 0, + } + } + + /// Push a value onto the end of the [`ConstVec`] + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// assert_eq!(ONE.as_ref(), &[1]); + /// ``` + pub const fn push(mut self, value: T) -> Self { + self.memory[self.len as usize] = MaybeUninit::new(value); + self.len += 1; + self + } + + /// Extend the [`ConstVec`] with the contents of a slice + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.extend(&[1, 2, 3]); + /// assert_eq!(ONE.as_ref(), &[1, 2, 3]); + /// ``` + pub const fn extend(mut self, other: &[T]) -> Self + where + T: Copy, + { + let mut i = 0; + while i < other.len() { + self = self.push(other[i]); + i += 1; + } + self + } + + /// Get a reference to the value at the given index + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// assert_eq!(ONE.get(0), Some(&1)); + /// ``` + pub const fn get(&self, index: usize) -> Option<&T> { + if index < self.len as usize { + Some(unsafe { &*self.memory[index].as_ptr() }) + } else { + None + } + } + + /// Get the length of the [`ConstVec`] + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// assert_eq!(ONE.len(), 1); + /// ``` + pub const fn len(&self) -> usize { + self.len as usize + } + + /// Check if the [`ConstVec`] is empty + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// assert!(EMPTY.is_empty()); + /// const ONE: ConstVec = EMPTY.push(1); + /// assert!(!ONE.is_empty()); + /// ``` + pub const fn is_empty(&self) -> bool { + self.len == 0 + } + + /// Get a reference to the underlying slice + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// assert_eq!(ONE.as_ref(), &[1]); + /// ``` + pub const fn as_ref(&self) -> &[T] { + unsafe { + &*(self.memory.split_at(self.len as usize).0 as *const [MaybeUninit] as *const [T]) + } + } + + /// Swap the values at the given indices + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// const TWO: ConstVec = ONE.push(2); + /// const THREE: ConstVec = TWO.swap(0, 1); + /// assert_eq!(THREE.as_ref(), &[2, 1]); + /// ``` + pub const fn swap(mut self, first: usize, second: usize) -> Self + where + T: Copy, + { + assert!(first < self.len as usize); + assert!(second < self.len as usize); + let temp = self.memory[first]; + self.memory[first] = self.memory[second]; + self.memory[second] = temp; + self + } + + /// Pop a value off the end of the [`ConstVec`] + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// const TWO: ConstVec = ONE.push(2); + /// const THREE: ConstVec = TWO.push(3); + /// const POPPED: (ConstVec, Option) = THREE.pop(); + /// assert_eq!(POPPED.0, TWO); + /// assert_eq!(POPPED.1.unwrap(), 3); + /// ``` + pub const fn pop(mut self) -> (Self, Option) + where + T: Copy, + { + let value = if self.len > 0 { + self.len -= 1; + let last = self.len as usize; + let last_value = unsafe { self.memory[last].assume_init() }; + Some(last_value) + } else { + None + }; + (self, value) + } + + /// Remove the value at the given index + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// const TWO: ConstVec = ONE.push(2); + /// const THREE: ConstVec = TWO.push(3); + /// const REMOVED: (ConstVec, Option) = THREE.remove(1); + /// assert_eq!(REMOVED.0.as_ref(), &[1, 3]); + /// assert_eq!(REMOVED.1.unwrap(), 2); + /// ``` + pub const fn remove(mut self, index: usize) -> (Self, Option) + where + T: Copy, + { + let value = if index < self.len as usize { + let value = unsafe { self.memory[index].assume_init() }; + let mut swap_index = index; + while swap_index + 1 < self.len as usize { + self.memory[swap_index] = self.memory[swap_index + 1]; + swap_index += 1; + } + self.len -= 1; + Some(value) + } else { + None + }; + + (self, value) + } + + /// Set the value at the given index + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// const TWO: ConstVec = ONE.set(0, 2); + /// assert_eq!(TWO.as_ref(), &[2]); + /// ``` + pub const fn set(mut self, index: usize, value: T) -> Self { + if index >= self.len as usize { + panic!("Out of bounds") + } + self.memory[index] = MaybeUninit::new(value); + self + } + + pub(crate) const fn into_parts(self) -> ([MaybeUninit; MAX_SIZE], usize) { + (self.memory, self.len as usize) + } + + /// Split the [`ConstVec`] into two at the given index + /// + /// # Example + /// ```rust + /// # use const_serialize::ConstVec; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// const TWO: ConstVec = ONE.push(2); + /// const THREE: ConstVec = TWO.push(3); + /// const SPLIT: (ConstVec, ConstVec) = THREE.split_at(1); + /// assert_eq!(SPLIT.0.as_ref(), &[1]); + /// assert_eq!(SPLIT.1.as_ref(), &[2, 3]); + /// ``` + pub const fn split_at(&self, index: usize) -> (Self, Self) + where + T: Copy, + { + assert!(index <= self.len as usize); + let slice = self.as_ref(); + let (left, right) = slice.split_at(index); + let mut left_vec = Self::new_with_max_size(); + let mut i = 0; + while i < left.len() { + left_vec = left_vec.push(left[i]); + i += 1; + } + let mut right_vec = Self::new_with_max_size(); + i = 0; + while i < right.len() { + right_vec = right_vec.push(right[i]); + i += 1; + } + (left_vec, right_vec) + } +} + +impl ConstVec { + /// Convert the [`ConstVec`] into a [`ConstReadBuffer`](crate::ConstReadBuffer) + /// + /// # Example + /// ```rust + /// # use const_serialize::{ConstVec, ConstReadBuffer}; + /// const EMPTY: ConstVec = ConstVec::new(); + /// const ONE: ConstVec = EMPTY.push(1); + /// const TWO: ConstVec = ONE.push(2); + /// const READ: ConstReadBuffer = TWO.read(); + /// ``` + pub const fn read(&self) -> ConstReadBuffer<'_> { + ConstReadBuffer::new(self.as_ref()) + } +} + +#[test] +fn test_const_vec() { + const VEC: ConstVec = { + let mut vec = ConstVec::new(); + vec = vec.push(1234); + vec = vec.push(5678); + vec + }; + assert_eq!(VEC.as_ref(), &[1234, 5678]); + let vec = VEC; + let (vec, value) = vec.pop(); + assert_eq!(value, Some(5678)); + let (vec, value) = vec.pop(); + assert_eq!(value, Some(1234)); + let (vec, value) = vec.pop(); + assert_eq!(value, None); + assert_eq!(vec.as_ref(), &[]); +} + +#[test] +fn test_const_vec_len() { + const VEC: ConstVec = { + let mut vec = ConstVec::new(); + vec = vec.push(1234); + vec = vec.push(5678); + vec + }; + assert_eq!(VEC.len(), 2); +} + +#[test] +fn test_const_vec_get() { + const VEC: ConstVec = { + let mut vec = ConstVec::new(); + vec = vec.push(1234); + vec = vec.push(5678); + vec + }; + assert_eq!(VEC.get(0), Some(&1234)); + assert_eq!(VEC.get(1), Some(&5678)); + assert_eq!(VEC.get(2), None); +} + +#[test] +fn test_const_vec_swap() { + const VEC: ConstVec = { + let mut vec = ConstVec::new(); + vec = vec.push(1234); + vec = vec.push(5678); + vec + }; + let mut vec = VEC; + assert_eq!(vec.as_ref(), &[1234, 5678]); + vec = vec.swap(0, 1); + assert_eq!(vec.as_ref(), &[5678, 1234]); + vec = vec.swap(0, 1); + assert_eq!(vec.as_ref(), &[1234, 5678]); +} + +#[test] +fn test_const_vec_remove() { + const VEC: ConstVec = { + let mut vec = ConstVec::new(); + vec = vec.push(1234); + vec = vec.push(5678); + vec + }; + let vec = VEC; + println!("{:?}", vec); + assert_eq!(vec.as_ref(), &[1234, 5678]); + let (vec, value) = vec.remove(0); + assert_eq!(value, Some(1234)); + assert_eq!(vec.as_ref(), &[5678]); + let (vec, value) = vec.remove(0); + assert_eq!(value, Some(5678)); + assert_eq!(vec.as_ref(), &[]); +} diff --git a/packages/const-serialize/src/lib.rs b/packages/const-serialize/src/lib.rs new file mode 100644 index 0000000000..5fba91db4f --- /dev/null +++ b/packages/const-serialize/src/lib.rs @@ -0,0 +1,924 @@ +#![doc = include_str!("../README.md")] +#![warn(missing_docs)] + +use std::{char, mem::MaybeUninit}; + +mod const_buffers; +mod const_vec; + +pub use const_buffers::ConstReadBuffer; +pub use const_serialize_macro::SerializeConst; +pub use const_vec::ConstVec; + +/// Plain old data for a field. Stores the offset of the field in the struct and the layout of the field. +#[derive(Debug, Copy, Clone)] +pub struct StructFieldLayout { + offset: usize, + layout: Layout, +} + +impl StructFieldLayout { + /// Create a new struct field layout + pub const fn new(offset: usize, layout: Layout) -> Self { + Self { offset, layout } + } +} + +/// Layout for a struct. The struct layout is just a list of fields with offsets +#[derive(Debug, Copy, Clone)] +pub struct StructLayout { + size: usize, + data: &'static [StructFieldLayout], +} + +impl StructLayout { + /// Create a new struct layout + pub const fn new(size: usize, data: &'static [StructFieldLayout]) -> Self { + Self { size, data } + } +} + +/// The layout for an enum. The enum layout is just a discriminate size and a tag layout. +#[derive(Debug, Copy, Clone)] +pub struct EnumLayout { + size: usize, + discriminant: PrimitiveLayout, + variants_offset: usize, + variants: &'static [EnumVariant], +} + +impl EnumLayout { + /// Create a new enum layout + pub const fn new( + size: usize, + discriminant: PrimitiveLayout, + variants: &'static [EnumVariant], + ) -> Self { + let mut max_align = 1; + let mut i = 0; + while i < variants.len() { + let EnumVariant { align, .. } = &variants[i]; + if *align > max_align { + max_align = *align; + } + i += 1; + } + + let variants_offset = (discriminant.size / max_align) + max_align; + + Self { + size, + discriminant, + variants_offset, + variants, + } + } +} + +/// The layout for an enum variant. The enum variant layout is just a struct layout with a tag and alignment. +#[derive(Debug, Copy, Clone)] +pub struct EnumVariant { + // Note: tags may not be sequential + tag: u32, + data: StructLayout, + align: usize, +} + +impl EnumVariant { + /// Create a new enum variant layout + pub const fn new(tag: u32, data: StructLayout, align: usize) -> Self { + Self { tag, data, align } + } +} + +/// The layout for a constant sized array. The array layout is just a length and an item layout. +#[derive(Debug, Copy, Clone)] +pub struct ListLayout { + len: usize, + item_layout: &'static Layout, +} + +impl ListLayout { + /// Create a new list layout + pub const fn new(len: usize, item_layout: &'static Layout) -> Self { + Self { len, item_layout } + } +} + +/// The layout for a primitive type. The bytes will be reversed if the target is big endian. +#[derive(Debug, Copy, Clone)] +pub struct PrimitiveLayout { + size: usize, +} + +impl PrimitiveLayout { + /// Create a new primitive layout + pub const fn new(size: usize) -> Self { + Self { size } + } +} + +/// The layout for a type. This layout defines a sequence of locations and reversed or not bytes. These bytes will be copied from during serialization and copied into during deserialization. +#[derive(Debug, Copy, Clone)] +pub enum Layout { + /// An enum layout + Enum(EnumLayout), + /// A struct layout + Struct(StructLayout), + /// A list layout + List(ListLayout), + /// A primitive layout + Primitive(PrimitiveLayout), +} + +impl Layout { + /// The size of the type in bytes. + const fn size(&self) -> usize { + match self { + Layout::Enum(layout) => layout.size, + Layout::Struct(layout) => layout.size, + Layout::List(layout) => layout.len * layout.item_layout.size(), + Layout::Primitive(layout) => layout.size, + } + } +} + +/// A trait for types that can be serialized and deserialized in const. +/// +/// # Safety +/// The layout must accurately describe the memory layout of the type +pub unsafe trait SerializeConst: Sized { + /// The memory layout of the type. This type must have plain old data; no pointers or references. + const MEMORY_LAYOUT: Layout; + /// Assert that the memory layout of the type is the same as the size of the type + const _ASSERT: () = assert!(Self::MEMORY_LAYOUT.size() == std::mem::size_of::()); +} + +macro_rules! impl_serialize_const { + ($type:ty) => { + unsafe impl SerializeConst for $type { + const MEMORY_LAYOUT: Layout = Layout::Primitive(PrimitiveLayout { + size: std::mem::size_of::<$type>(), + }); + } + }; +} + +impl_serialize_const!(u8); +impl_serialize_const!(u16); +impl_serialize_const!(u32); +impl_serialize_const!(u64); +impl_serialize_const!(i8); +impl_serialize_const!(i16); +impl_serialize_const!(i32); +impl_serialize_const!(i64); +impl_serialize_const!(bool); +impl_serialize_const!(f32); +impl_serialize_const!(f64); + +unsafe impl SerializeConst for [T; N] { + const MEMORY_LAYOUT: Layout = Layout::List(ListLayout { + len: N, + item_layout: &T::MEMORY_LAYOUT, + }); +} + +macro_rules! impl_serialize_const_tuple { + ($($generic:ident: $generic_number:expr),*) => { + impl_serialize_const_tuple!(@impl ($($generic,)*) = $($generic: $generic_number),*); + }; + (@impl $inner:ty = $($generic:ident: $generic_number:expr),*) => { + unsafe impl<$($generic: SerializeConst),*> SerializeConst for ($($generic,)*) { + const MEMORY_LAYOUT: Layout = { + Layout::Struct(StructLayout { + size: std::mem::size_of::<($($generic,)*)>(), + data: &[ + $( + StructFieldLayout::new(std::mem::offset_of!($inner, $generic_number), $generic::MEMORY_LAYOUT), + )* + ], + }) + }; + } + }; +} + +impl_serialize_const_tuple!(T1: 0); +impl_serialize_const_tuple!(T1: 0, T2: 1); +impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2); +impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3); +impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4); +impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5); +impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6); +impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6, T8: 7); +impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6, T8: 7, T9: 8); +impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6, T8: 7, T9: 8, T10: 9); + +const MAX_STR_SIZE: usize = 256; + +/// A string that is stored in a constant sized buffer that can be serialized and deserialized at compile time +#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct ConstStr { + #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))] + bytes: [u8; MAX_STR_SIZE], + len: u32, +} + +#[cfg(feature = "serde")] +mod serde_bytes { + use serde::{Deserialize, Serializer}; + + pub fn serialize(bytes: &[u8], serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_bytes(bytes) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; super::MAX_STR_SIZE], D::Error> + where + D: serde::Deserializer<'de>, + { + let bytes = Vec::::deserialize(deserializer)?; + bytes + .try_into() + .map_err(|_| serde::de::Error::custom("Failed to convert bytes to a fixed size array")) + } +} + +unsafe impl SerializeConst for ConstStr { + const MEMORY_LAYOUT: Layout = Layout::Struct(StructLayout { + size: std::mem::size_of::(), + data: &[ + StructFieldLayout::new( + std::mem::offset_of!(Self, bytes), + Layout::List(ListLayout { + len: MAX_STR_SIZE, + item_layout: &Layout::Primitive(PrimitiveLayout { + size: std::mem::size_of::(), + }), + }), + ), + StructFieldLayout::new( + std::mem::offset_of!(Self, len), + Layout::Primitive(PrimitiveLayout { + size: std::mem::size_of::(), + }), + ), + ], + }); +} + +impl ConstStr { + /// Create a new constant string + pub const fn new(s: &str) -> Self { + let str_bytes = s.as_bytes(); + let mut bytes = [0; MAX_STR_SIZE]; + let mut i = 0; + while i < str_bytes.len() { + bytes[i] = str_bytes[i]; + i += 1; + } + Self { + bytes, + len: str_bytes.len() as u32, + } + } + + /// Get a reference to the string + pub const fn as_str(&self) -> &str { + let str_bytes = self.bytes.split_at(self.len as usize).0; + match std::str::from_utf8(str_bytes) { + Ok(s) => s, + Err(_) => panic!( + "Invalid utf8; ConstStr should only ever be constructed from valid utf8 strings" + ), + } + } + + /// Get the length of the string + pub const fn len(&self) -> usize { + self.len as usize + } + + /// Check if the string is empty + pub const fn is_empty(&self) -> bool { + self.len == 0 + } + + /// Push a character onto the string + pub const fn push(self, byte: char) -> Self { + assert!(byte.is_ascii(), "Only ASCII bytes are supported"); + let (bytes, len) = char_to_bytes(byte); + let (str, _) = bytes.split_at(len); + let Ok(str) = std::str::from_utf8(str) else { + panic!("Invalid utf8; char_to_bytes should always return valid utf8 bytes") + }; + self.push_str(str) + } + + /// Push a str onto the string + pub const fn push_str(self, str: &str) -> Self { + let Self { mut bytes, len } = self; + assert!( + str.len() + len as usize <= MAX_STR_SIZE, + "String is too long" + ); + let str_bytes = str.as_bytes(); + let new_len = len as usize + str_bytes.len(); + let mut i = 0; + while i < str_bytes.len() { + bytes[len as usize + i] = str_bytes[i]; + i += 1; + } + Self { + bytes, + len: new_len as u32, + } + } + + /// Split the string at a byte index. The byte index must be a char boundary + pub const fn split_at(self, index: usize) -> (Self, Self) { + let (left, right) = self.bytes.split_at(index); + let left = match std::str::from_utf8(left) { + Ok(s) => s, + Err(_) => { + panic!("Invalid utf8; you cannot split at a byte that is not a char boundary") + } + }; + let right = match std::str::from_utf8(right) { + Ok(s) => s, + Err(_) => { + panic!("Invalid utf8; you cannot split at a byte that is not a char boundary") + } + }; + (Self::new(left), Self::new(right)) + } + + /// Split the string at the last occurrence of a character + pub const fn rsplit_once(&self, char: char) -> Option<(Self, Self)> { + let str = self.as_str(); + let mut index = str.len() - 1; + // First find the bytes we are searching for + let (char_bytes, len) = char_to_bytes(char); + let (char_bytes, _) = char_bytes.split_at(len); + let bytes = str.as_bytes(); + + // Then walk backwards from the end of the string + loop { + let byte = bytes[index]; + // Look for char boundaries in the string and check if the bytes match + if let Some(char_boundary_len) = utf8_char_boundary_to_char_len(byte) { + // Split up the string into three sections: [before_char, in_char, after_char] + let (before_char, after_index) = bytes.split_at(index); + let (in_char, after_char) = after_index.split_at(char_boundary_len as usize); + if in_char.len() != char_boundary_len as usize { + panic!("in_char.len() should always be equal to char_boundary_len as usize") + } + // Check if the bytes for the current char and the target char match + let mut in_char_eq = true; + let mut i = 0; + let min_len = if in_char.len() < char_bytes.len() { + in_char.len() + } else { + char_bytes.len() + }; + while i < min_len { + in_char_eq &= in_char[i] == char_bytes[i]; + i += 1; + } + // If they do, convert the bytes to strings and return the split strings + if in_char_eq { + let Ok(before_char_str) = std::str::from_utf8(before_char) else { + panic!("Invalid utf8; utf8_char_boundary_to_char_len should only return Some when the byte is a character boundary") + }; + let Ok(after_char_str) = std::str::from_utf8(after_char) else { + panic!("Invalid utf8; utf8_char_boundary_to_char_len should only return Some when the byte is a character boundary") + }; + return Some((Self::new(before_char_str), Self::new(after_char_str))); + } + } + match index.checked_sub(1) { + Some(new_index) => index = new_index, + None => return None, + } + } + } + + /// Split the string at the first occurrence of a character + pub const fn split_once(&self, char: char) -> Option<(Self, Self)> { + let str = self.as_str(); + let mut index = 0; + // First find the bytes we are searching for + let (char_bytes, len) = char_to_bytes(char); + let (char_bytes, _) = char_bytes.split_at(len); + let bytes = str.as_bytes(); + + // Then walk forwards from the start of the string + while index < bytes.len() { + let byte = bytes[index]; + // Look for char boundaries in the string and check if the bytes match + if let Some(char_boundary_len) = utf8_char_boundary_to_char_len(byte) { + // Split up the string into three sections: [before_char, in_char, after_char] + let (before_char, after_index) = bytes.split_at(index); + let (in_char, after_char) = after_index.split_at(char_boundary_len as usize); + if in_char.len() != char_boundary_len as usize { + panic!("in_char.len() should always be equal to char_boundary_len as usize") + } + // Check if the bytes for the current char and the target char match + let mut in_char_eq = true; + let mut i = 0; + let min_len = if in_char.len() < char_bytes.len() { + in_char.len() + } else { + char_bytes.len() + }; + while i < min_len { + in_char_eq &= in_char[i] == char_bytes[i]; + i += 1; + } + // If they do, convert the bytes to strings and return the split strings + if in_char_eq { + let Ok(before_char_str) = std::str::from_utf8(before_char) else { + panic!("Invalid utf8; utf8_char_boundary_to_char_len should only return Some when the byte is a character boundary") + }; + let Ok(after_char_str) = std::str::from_utf8(after_char) else { + panic!("Invalid utf8; utf8_char_boundary_to_char_len should only return Some when the byte is a character boundary") + }; + return Some((Self::new(before_char_str), Self::new(after_char_str))); + } + } + index += 1 + } + None + } +} + +#[test] +fn test_rsplit_once() { + let str = ConstStr::new("hello world"); + assert_eq!( + str.rsplit_once(' '), + Some((ConstStr::new("hello"), ConstStr::new("world"))) + ); + + let unicode_str = ConstStr::new("hi😀hello😀world😀world"); + assert_eq!( + unicode_str.rsplit_once('😀'), + Some((ConstStr::new("hi😀hello😀world"), ConstStr::new("world"))) + ); + assert_eq!(unicode_str.rsplit_once('❌'), None); + + for _ in 0..100 { + let random_str: String = (0..rand::random::() % 50) + .map(|_| rand::random::()) + .collect(); + let konst = ConstStr::new(&random_str); + let mut seen_chars = std::collections::HashSet::new(); + for char in random_str.chars().rev() { + let (char_bytes, len) = char_to_bytes(char); + let char_bytes = &char_bytes[..len]; + assert_eq!(char_bytes, char.to_string().as_bytes()); + if seen_chars.contains(&char) { + continue; + } + seen_chars.insert(char); + let (correct_left, correct_right) = random_str.rsplit_once(char).unwrap(); + let (left, right) = konst.rsplit_once(char).unwrap(); + println!("splitting {random_str:?} at {char:?}"); + assert_eq!(left.as_str(), correct_left); + assert_eq!(right.as_str(), correct_right); + } + } +} + +const CONTINUED_CHAR_MASK: u8 = 0b10000000; +const BYTE_CHAR_BOUNDARIES: [u8; 4] = [0b00000000, 0b11000000, 0b11100000, 0b11110000]; + +// Const version of https://doc.rust-lang.org/src/core/char/methods.rs.html#1765-1797 +const fn char_to_bytes(char: char) -> ([u8; 4], usize) { + let code = char as u32; + let len = char.len_utf8(); + let mut bytes = [0; 4]; + match len { + 1 => { + bytes[0] = code as u8; + } + 2 => { + bytes[0] = (code >> 6 & 0x1F) as u8 | BYTE_CHAR_BOUNDARIES[1]; + bytes[1] = (code & 0x3F) as u8 | CONTINUED_CHAR_MASK; + } + 3 => { + bytes[0] = (code >> 12 & 0x0F) as u8 | BYTE_CHAR_BOUNDARIES[2]; + bytes[1] = (code >> 6 & 0x3F) as u8 | CONTINUED_CHAR_MASK; + bytes[2] = (code & 0x3F) as u8 | CONTINUED_CHAR_MASK; + } + 4 => { + bytes[0] = (code >> 18 & 0x07) as u8 | BYTE_CHAR_BOUNDARIES[3]; + bytes[1] = (code >> 12 & 0x3F) as u8 | CONTINUED_CHAR_MASK; + bytes[2] = (code >> 6 & 0x3F) as u8 | CONTINUED_CHAR_MASK; + bytes[3] = (code & 0x3F) as u8 | CONTINUED_CHAR_MASK; + } + _ => panic!( + "encode_utf8: need more than 4 bytes to encode the unicode character, but the buffer has 4 bytes" + ), + }; + (bytes, len) +} + +#[test] +fn fuzz_char_to_bytes() { + use std::char; + for _ in 0..100 { + let char = rand::random::(); + let (bytes, len) = char_to_bytes(char); + let str = std::str::from_utf8(&bytes[..len]).unwrap(); + assert_eq!(char.to_string(), str); + } +} + +const fn utf8_char_boundary_to_char_len(byte: u8) -> Option { + match byte { + 0b00000000..=0b01111111 => Some(1), + 0b11000000..=0b11011111 => Some(2), + 0b11100000..=0b11101111 => Some(3), + 0b11110000..=0b11111111 => Some(4), + _ => None, + } +} + +#[test] +fn fuzz_utf8_byte_to_char_len() { + for _ in 0..100 { + let random_string: String = (0..rand::random::()) + .map(|_| rand::random::()) + .collect(); + let bytes = random_string.as_bytes(); + let chars: std::collections::HashMap<_, _> = random_string.char_indices().collect(); + for (i, byte) in bytes.iter().enumerate() { + match utf8_char_boundary_to_char_len(*byte) { + Some(char_len) => { + let char = chars + .get(&i) + .unwrap_or_else(|| panic!("{byte:b} is not a character boundary")); + assert_eq!(char.len_utf8(), char_len as usize); + } + None => { + assert!(!chars.contains_key(&i), "{byte:b} is a character boundary"); + } + } + } + } +} + +/// Serialize a struct that is stored at the pointer passed in +const fn serialize_const_struct( + ptr: *const (), + mut to: ConstVec, + layout: &StructLayout, +) -> ConstVec { + let mut i = 0; + while i < layout.data.len() { + // Serialize the field at the offset pointer in the struct + let StructFieldLayout { offset, layout } = &layout.data[i]; + let field = unsafe { ptr.byte_add(*offset) }; + to = serialize_const_ptr(field, to, layout); + i += 1; + } + to +} + +/// Serialize an enum that is stored at the pointer passed in +const fn serialize_const_enum( + ptr: *const (), + mut to: ConstVec, + layout: &EnumLayout, +) -> ConstVec { + let mut discriminant = 0; + + let byte_ptr = ptr as *const u8; + let mut offset = 0; + while offset < layout.discriminant.size { + // If the bytes are reversed, walk backwards from the end of the number when pushing bytes + let byte = if cfg!(target_endian = "big") { + unsafe { + byte_ptr + .byte_add(layout.discriminant.size - offset - 1) + .read() + } + } else { + unsafe { byte_ptr.byte_add(offset).read() } + }; + to = to.push(byte); + discriminant |= (byte as u32) << (offset * 8); + offset += 1; + } + + let mut i = 0; + while i < layout.variants.len() { + // If the variant is the discriminated one, serialize it + let EnumVariant { tag, data, .. } = &layout.variants[i]; + if discriminant == *tag { + let data_ptr = unsafe { ptr.byte_add(layout.variants_offset) }; + to = serialize_const_struct(data_ptr, to, data); + break; + } + i += 1; + } + to +} + +/// Serialize a primitive type that is stored at the pointer passed in +const fn serialize_const_primitive( + ptr: *const (), + mut to: ConstVec, + layout: &PrimitiveLayout, +) -> ConstVec { + let ptr = ptr as *const u8; + let mut offset = 0; + while offset < layout.size { + // If the bytes are reversed, walk backwards from the end of the number when pushing bytes + if cfg!(any(target_endian = "big", feature = "test-big-endian")) { + to = to.push(unsafe { ptr.byte_add(layout.size - offset - 1).read() }); + } else { + to = to.push(unsafe { ptr.byte_add(offset).read() }); + } + offset += 1; + } + to +} + +/// Serialize a constant sized array that is stored at the pointer passed in +const fn serialize_const_list( + ptr: *const (), + mut to: ConstVec, + layout: &ListLayout, +) -> ConstVec { + let len = layout.len; + let mut i = 0; + while i < len { + let field = unsafe { ptr.byte_add(i * layout.item_layout.size()) }; + to = serialize_const_ptr(field, to, layout.item_layout); + i += 1; + } + to +} + +/// Serialize a pointer to a type that is stored at the pointer passed in +const fn serialize_const_ptr(ptr: *const (), to: ConstVec, layout: &Layout) -> ConstVec { + match layout { + Layout::Enum(layout) => serialize_const_enum(ptr, to, layout), + Layout::Struct(layout) => serialize_const_struct(ptr, to, layout), + Layout::List(layout) => serialize_const_list(ptr, to, layout), + Layout::Primitive(layout) => serialize_const_primitive(ptr, to, layout), + } +} + +/// Serialize a type into a buffer +/// +/// # Example +/// +/// ```rust +/// use const_serialize::{ConstVec, SerializeConst, serialize_const}; +/// +/// #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] +/// struct Struct { +/// a: u32, +/// b: u8, +/// c: u32, +/// } +/// +/// let mut buffer = ConstVec::new(); +/// buffer = serialize_const(&Struct { +/// a: 0x11111111, +/// b: 0x22, +/// c: 0x33333333, +/// }, buffer); +/// let buf = buffer.read(); +/// assert_eq!(buf.as_ref(), &[0x11, 0x11, 0x11, 0x11, 0x22, 0x33, 0x33, 0x33, 0x33]); +/// ``` +#[must_use = "The data is serialized into the returned buffer"] +pub const fn serialize_const(data: &T, to: ConstVec) -> ConstVec { + let ptr = data as *const T as *const (); + serialize_const_ptr(ptr, to, &T::MEMORY_LAYOUT) +} + +/// Deserialize a primitive type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added. +const fn deserialize_const_primitive<'a, const N: usize>( + mut from: ConstReadBuffer<'a>, + layout: &PrimitiveLayout, + out: (usize, [MaybeUninit; N]), +) -> Option<(ConstReadBuffer<'a>, [MaybeUninit; N])> { + let (start, mut out) = out; + let mut offset = 0; + while offset < layout.size { + // If the bytes are reversed, walk backwards from the end of the number when filling in bytes + let (from_new, value) = match from.get() { + Some(data) => data, + None => return None, + }; + from = from_new; + if cfg!(any(target_endian = "big", feature = "test-big-endian")) { + out[start + layout.size - offset - 1] = MaybeUninit::new(value); + } else { + out[start + offset] = MaybeUninit::new(value); + } + offset += 1; + } + Some((from, out)) +} + +/// Deserialize a struct type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added. +const fn deserialize_const_struct<'a, const N: usize>( + mut from: ConstReadBuffer<'a>, + layout: &StructLayout, + out: (usize, [MaybeUninit; N]), +) -> Option<(ConstReadBuffer<'a>, [MaybeUninit; N])> { + let (start, mut out) = out; + let mut i = 0; + while i < layout.data.len() { + // Deserialize the field at the offset pointer in the struct + let StructFieldLayout { offset, layout } = &layout.data[i]; + let (new_from, new_out) = match deserialize_const_ptr(from, layout, (start + *offset, out)) + { + Some(data) => data, + None => return None, + }; + from = new_from; + out = new_out; + i += 1; + } + Some((from, out)) +} + +/// Deserialize an enum type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added. +const fn deserialize_const_enum<'a, const N: usize>( + mut from: ConstReadBuffer<'a>, + layout: &EnumLayout, + out: (usize, [MaybeUninit; N]), +) -> Option<(ConstReadBuffer<'a>, [MaybeUninit; N])> { + let (start, mut out) = out; + let mut discriminant = 0; + + // First, deserialize the discriminant + let mut offset = 0; + while offset < layout.discriminant.size { + // If the bytes are reversed, walk backwards from the end of the number when filling in bytes + let (from_new, value) = match from.get() { + Some(data) => data, + None => return None, + }; + from = from_new; + if cfg!(target_endian = "big") { + out[start + layout.size - offset - 1] = MaybeUninit::new(value); + discriminant |= (value as u32) << ((layout.discriminant.size - offset - 1) * 8); + } else { + out[start + offset] = MaybeUninit::new(value); + discriminant |= (value as u32) << (offset * 8); + } + offset += 1; + } + + // Then, deserialize the variant + let mut i = 0; + let mut matched_variant = false; + while i < layout.variants.len() { + // If the variant is the discriminated one, deserialize it + let EnumVariant { tag, data, .. } = &layout.variants[i]; + if discriminant == *tag { + let offset = layout.variants_offset; + let (new_from, new_out) = + match deserialize_const_struct(from, data, (start + offset, out)) { + Some(data) => data, + None => return None, + }; + from = new_from; + out = new_out; + matched_variant = true; + break; + } + i += 1; + } + if !matched_variant { + return None; + } + + Some((from, out)) +} + +/// Deserialize a list type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added. +const fn deserialize_const_list<'a, const N: usize>( + mut from: ConstReadBuffer<'a>, + layout: &ListLayout, + out: (usize, [MaybeUninit; N]), +) -> Option<(ConstReadBuffer<'a>, [MaybeUninit; N])> { + let (start, mut out) = out; + let len = layout.len; + let item_layout = layout.item_layout; + let mut i = 0; + while i < len { + let (new_from, new_out) = + match deserialize_const_ptr(from, item_layout, (start + i * item_layout.size(), out)) { + Some(data) => data, + None => return None, + }; + from = new_from; + out = new_out; + i += 1; + } + Some((from, out)) +} + +/// Deserialize a type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added. +const fn deserialize_const_ptr<'a, const N: usize>( + from: ConstReadBuffer<'a>, + layout: &Layout, + out: (usize, [MaybeUninit; N]), +) -> Option<(ConstReadBuffer<'a>, [MaybeUninit; N])> { + match layout { + Layout::Enum(layout) => deserialize_const_enum(from, layout, out), + Layout::Struct(layout) => deserialize_const_struct(from, layout, out), + Layout::List(layout) => deserialize_const_list(from, layout, out), + Layout::Primitive(layout) => deserialize_const_primitive(from, layout, out), + } +} + +/// Deserialize a type into the output buffer. Accepts (Type, ConstVec) as input and returns Option<(ConstReadBuffer, Instance of type)> +/// +/// # Example +/// ```rust +/// # use const_serialize::{deserialize_const, serialize_const, ConstVec, SerializeConst}; +/// #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] +/// struct Struct { +/// a: u32, +/// b: u8, +/// c: u32, +/// d: u32, +/// } +/// +/// let mut buffer = ConstVec::new(); +/// buffer = serialize_const(&Struct { +/// a: 0x11111111, +/// b: 0x22, +/// c: 0x33333333, +/// d: 0x44444444, +/// }, buffer); +/// let buf = buffer.read(); +/// assert_eq!(deserialize_const!(Struct, buf).unwrap().1, Struct { +/// a: 0x11111111, +/// b: 0x22, +/// c: 0x33333333, +/// d: 0x44444444, +/// }); +/// ``` +#[macro_export] +macro_rules! deserialize_const { + ($type:ty, $buffer:expr) => { + unsafe { + const __SIZE: usize = std::mem::size_of::<$type>(); + $crate::deserialize_const_raw::<__SIZE, $type>($buffer) + } + }; +} + +/// Deserialize a buffer into a type. This will return None if the buffer doesn't have enough data to fill the type. +/// # Safety +/// N must be `std::mem::size_of::()` +#[must_use = "The data is deserialized from the input buffer"] +pub const unsafe fn deserialize_const_raw( + from: ConstReadBuffer, +) -> Option<(ConstReadBuffer, T)> { + // Create uninitized memory with the size of the type + let out = [MaybeUninit::uninit(); N]; + // Fill in the bytes into the buffer for the type + let (from, out) = match deserialize_const_ptr(from, &T::MEMORY_LAYOUT, (0, out)) { + Some(data) => data, + None => return None, + }; + // Now that the memory is filled in, transmute it into the type + Some((from, unsafe { + std::mem::transmute_copy::<[MaybeUninit; N], T>(&out) + })) +} + +/// Check if the serialized representation of two items are the same +pub const fn serialize_eq(first: &T, second: &T) -> bool { + let first_serialized = ConstVec::::new(); + let first_serialized = serialize_const(first, first_serialized); + let second_serialized = ConstVec::::new(); + let second_serialized = serialize_const(second, second_serialized); + let first_buf = first_serialized.as_ref(); + let second_buf = second_serialized.as_ref(); + if first_buf.len() != second_buf.len() { + return false; + } + let mut i = 0; + while i < first_buf.len() { + if first_buf[i] != second_buf[i] { + return false; + } + i += 1; + } + true +} diff --git a/packages/const-serialize/tests/enum.rs b/packages/const-serialize/tests/enum.rs new file mode 100644 index 0000000000..130a5dbaa0 --- /dev/null +++ b/packages/const-serialize/tests/enum.rs @@ -0,0 +1,206 @@ +use const_serialize::{deserialize_const, serialize_const, ConstVec, SerializeConst}; +use std::mem::MaybeUninit; + +#[test] +fn test_transmute_bytes_to_enum() { + #[derive(Clone, Copy, Debug, PartialEq)] + #[repr(C, u8)] + enum Enum { + A { one: u32, two: u16 }, + B { one: u8, two: T } = 15, + } + + #[repr(C)] + #[derive(Debug, PartialEq)] + struct A { + one: u32, + two: u16, + } + + #[repr(C)] + #[derive(Debug, PartialEq)] + struct B { + one: u8, + two: T, + } + + const SIZE: usize = std::mem::size_of::>(); + let mut out = [MaybeUninit::uninit(); SIZE]; + let discriminate_size = std::mem::size_of::(); + let tag_align = 0; + let union_alignment = std::mem::align_of::().max(std::mem::align_of::>()); + let data_align = (discriminate_size / union_alignment) + union_alignment; + let a_one_align = std::mem::offset_of!(A, one); + let a_two_align = std::mem::offset_of!(A, two); + let b_one_align = std::mem::offset_of!(B, one); + let b_two_align = std::mem::offset_of!(B, two); + + let one = 1234u32; + let two = 5678u16; + let first = Enum::A { one, two }; + for (i, byte) in one.to_le_bytes().iter().enumerate() { + out[data_align + i + a_one_align] = MaybeUninit::new(*byte); + } + for (i, byte) in two.to_le_bytes().iter().enumerate() { + out[data_align + i + a_two_align] = MaybeUninit::new(*byte); + } + out[tag_align] = MaybeUninit::new(0); + let out = unsafe { std::mem::transmute_copy::<[MaybeUninit; SIZE], Enum>(&out) }; + assert_eq!(out, first); + + let mut out = [MaybeUninit::uninit(); SIZE]; + let one = 123u8; + let two = 58u16; + let second = Enum::B { one, two }; + for (i, byte) in one.to_le_bytes().iter().enumerate() { + out[data_align + i + b_one_align] = MaybeUninit::new(*byte); + } + for (i, byte) in two.to_le_bytes().iter().enumerate() { + out[data_align + i + b_two_align] = MaybeUninit::new(*byte); + } + out[tag_align] = MaybeUninit::new(15); + let out = unsafe { std::mem::transmute_copy::<[MaybeUninit; SIZE], Enum>(&out) }; + assert_eq!(out, second); +} + +#[test] +fn test_serialize_enum() { + #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] + #[repr(C, u8)] + enum Enum { + A { one: u32, two: u16 }, + B { one: u8, two: u16 } = 15, + } + + println!("{:#?}", Enum::MEMORY_LAYOUT); + + let data = Enum::A { + one: 0x11111111, + two: 0x22, + }; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data); + + let data = Enum::B { + one: 0x11, + two: 0x2233, + }; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data); +} + +#[test] +fn test_serialize_u8_enum() { + #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] + #[repr(u8)] + enum Enum { + A, + B, + } + + println!("{:#?}", Enum::MEMORY_LAYOUT); + + let data = Enum::A; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data); + + let data = Enum::B; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data); +} + +#[test] +fn test_serialize_corrupted_enum() { + #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] + #[repr(C, u8)] + enum Enum { + A { one: u32, two: u16 }, + } + + let data = Enum::A { + one: 0x11111111, + two: 0x22, + }; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + buf = buf.set(0, 2); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!(Enum, buf), None); +} + +#[test] +fn test_serialize_nested_enum() { + #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] + #[repr(C, u8)] + enum Enum { + A { one: u32, two: u16 }, + B { one: u8, two: InnerEnum } = 15, + } + + #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] + #[repr(C, u16)] + enum InnerEnum { + A(u8), + B { one: u64, two: f64 } = 1000, + C { one: u32, two: u16 }, + } + + let data = Enum::A { + one: 0x11111111, + two: 0x22, + }; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data); + + let data = Enum::B { + one: 0x11, + two: InnerEnum::A(0x22), + }; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data); + + let data = Enum::B { + one: 0x11, + two: InnerEnum::B { + one: 0x2233, + two: 0.123456789, + }, + }; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data); + + let data = Enum::B { + one: 0x11, + two: InnerEnum::C { + one: 0x2233, + two: 56789, + }, + }; + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data); +} diff --git a/packages/const-serialize/tests/eq.rs b/packages/const-serialize/tests/eq.rs new file mode 100644 index 0000000000..a4edf67b58 --- /dev/null +++ b/packages/const-serialize/tests/eq.rs @@ -0,0 +1,59 @@ +use const_serialize::{serialize_eq, SerializeConst}; + +#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] +struct Struct { + a: u32, + b: u8, + c: u32, + d: Enum, +} + +#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] +#[repr(C, u8)] +enum Enum { + A { one: u32, two: u16 }, + B { one: u8, two: u16 } = 15, +} + +#[test] +fn const_eq() { + const { + let data = [ + Struct { + a: 0x11111111, + b: 0x22, + c: 0x33333333, + d: Enum::A { + one: 0x44444444, + two: 0x5555, + }, + }, + Struct { + a: 123, + b: 9, + c: 38, + d: Enum::B { + one: 0x44, + two: 0x555, + }, + }, + Struct { + a: 9, + b: 123, + c: 39, + d: Enum::B { + one: 0x46, + two: 0x555, + }, + }, + ]; + let mut other = data; + other[2].a += 1; + if serialize_eq(&data, &other) { + panic!("data should be different"); + } + if !serialize_eq(&data, &data) { + panic!("data should be the same"); + } + } +} diff --git a/packages/const-serialize/tests/lists.rs b/packages/const-serialize/tests/lists.rs new file mode 100644 index 0000000000..84f9fe11b2 --- /dev/null +++ b/packages/const-serialize/tests/lists.rs @@ -0,0 +1,34 @@ +use const_serialize::{deserialize_const, serialize_const, ConstVec}; + +#[test] +fn test_serialize_const_layout_list() { + let mut buf = ConstVec::new(); + buf = serialize_const(&[1u8, 2, 3] as &[u8; 3], buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!(deserialize_const!([u8; 3], buf).unwrap().1, [1, 2, 3]) +} + +#[test] +fn test_serialize_const_layout_nested_lists() { + let mut buf = ConstVec::new(); + buf = serialize_const( + &[[1u8, 2, 3], [4u8, 5, 6], [7u8, 8, 9]] as &[[u8; 3]; 3], + buf, + ); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + + assert_eq!( + deserialize_const!([[u8; 3]; 3], buf).unwrap().1, + [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + ); +} + +#[test] +fn test_serialize_list_too_little_data() { + let mut buf = ConstVec::new(); + buf = buf.push(1); + let buf = buf.read(); + assert_eq!(deserialize_const!([u64; 10], buf), None); +} diff --git a/packages/const-serialize/tests/primitive.rs b/packages/const-serialize/tests/primitive.rs new file mode 100644 index 0000000000..a5e3e803ff --- /dev/null +++ b/packages/const-serialize/tests/primitive.rs @@ -0,0 +1,71 @@ +use const_serialize::{deserialize_const, serialize_const, ConstVec}; + +#[test] +fn test_serialize_const_layout_primitive() { + let mut buf = ConstVec::new(); + buf = serialize_const(&1234u32, buf); + if cfg!(feature = "test-big-endian") { + assert_eq!(buf.as_ref(), 1234u32.to_be_bytes()); + } else { + assert_eq!(buf.as_ref(), 1234u32.to_le_bytes()); + } + let buf = buf.read(); + assert_eq!(deserialize_const!(u32, buf).unwrap().1, 1234u32); + + let mut buf = ConstVec::new(); + buf = serialize_const(&1234u64, buf); + if cfg!(feature = "test-big-endian") { + assert_eq!(buf.as_ref(), 1234u64.to_be_bytes()); + } else { + assert_eq!(buf.as_ref(), 1234u64.to_le_bytes()); + } + let buf = buf.read(); + assert_eq!(deserialize_const!(u64, buf).unwrap().1, 1234u64); + + let mut buf = ConstVec::new(); + buf = serialize_const(&1234i32, buf); + if cfg!(feature = "test-big-endian") { + assert_eq!(buf.as_ref(), 1234i32.to_be_bytes()); + } else { + assert_eq!(buf.as_ref(), 1234i32.to_le_bytes()); + } + let buf = buf.read(); + assert_eq!(deserialize_const!(i32, buf).unwrap().1, 1234i32); + + let mut buf = ConstVec::new(); + buf = serialize_const(&1234i64, buf); + if cfg!(feature = "test-big-endian") { + assert_eq!(buf.as_ref(), 1234i64.to_be_bytes()); + } else { + assert_eq!(buf.as_ref(), 1234i64.to_le_bytes()); + } + let buf = buf.read(); + assert_eq!(deserialize_const!(i64, buf).unwrap().1, 1234i64); + + let mut buf = ConstVec::new(); + buf = serialize_const(&true, buf); + assert_eq!(buf.as_ref(), [1u8]); + let buf = buf.read(); + assert!(deserialize_const!(bool, buf).unwrap().1); + + let mut buf = ConstVec::new(); + buf = serialize_const(&0.631f32, buf); + if cfg!(feature = "test-big-endian") { + assert_eq!(buf.as_ref(), 0.631f32.to_be_bytes()); + } else { + assert_eq!(buf.as_ref(), 0.631f32.to_le_bytes()); + } + let buf = buf.read(); + assert_eq!(deserialize_const!(f32, buf).unwrap().1, 0.631); +} + +#[test] + +fn test_serialize_primitive_too_little_data() { + let mut buf = ConstVec::new(); + buf = buf.push(1); + buf = buf.push(1); + buf = buf.push(1); + let buf = buf.read(); + assert_eq!(deserialize_const!(u64, buf), None); +} diff --git a/packages/const-serialize/tests/str.rs b/packages/const-serialize/tests/str.rs new file mode 100644 index 0000000000..45371741d5 --- /dev/null +++ b/packages/const-serialize/tests/str.rs @@ -0,0 +1,40 @@ +use const_serialize::{deserialize_const, serialize_const, ConstStr, ConstVec}; + +#[test] +fn test_serialize_const_layout_str() { + let mut buf = ConstVec::new(); + let str = ConstStr::new("hello"); + buf = serialize_const(&str, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + assert_eq!( + deserialize_const!(ConstStr, buf).unwrap().1.as_str(), + "hello" + ); +} + +#[test] +fn test_serialize_const_layout_nested_str() { + let mut buf = ConstVec::new(); + let str = ConstStr::new("hello"); + buf = serialize_const(&[str, str, str] as &[ConstStr; 3], buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + + assert_eq!( + deserialize_const!([ConstStr; 3], buf).unwrap().1, + [ + ConstStr::new("hello"), + ConstStr::new("hello"), + ConstStr::new("hello") + ] + ); +} + +#[test] +fn test_serialize_str_too_little_data() { + let mut buf = ConstVec::new(); + buf = buf.push(1); + let buf = buf.read(); + assert_eq!(deserialize_const!(ConstStr, buf), None); +} diff --git a/packages/const-serialize/tests/structs.rs b/packages/const-serialize/tests/structs.rs new file mode 100644 index 0000000000..68ce249381 --- /dev/null +++ b/packages/const-serialize/tests/structs.rs @@ -0,0 +1,164 @@ +use const_serialize::{deserialize_const, serialize_const, ConstVec, SerializeConst}; +use std::mem::MaybeUninit; + +#[test] +fn test_transmute_bytes_to_struct() { + struct MyStruct { + a: u32, + b: u8, + c: u32, + d: u32, + } + const SIZE: usize = std::mem::size_of::(); + let mut out = [MaybeUninit::uninit(); SIZE]; + let first_align = std::mem::offset_of!(MyStruct, a); + let second_align = std::mem::offset_of!(MyStruct, b); + let third_align = std::mem::offset_of!(MyStruct, c); + let fourth_align = std::mem::offset_of!(MyStruct, d); + for (i, byte) in 1234u32.to_le_bytes().iter().enumerate() { + out[i + first_align] = MaybeUninit::new(*byte); + } + for (i, byte) in 12u8.to_le_bytes().iter().enumerate() { + out[i + second_align] = MaybeUninit::new(*byte); + } + for (i, byte) in 13u32.to_le_bytes().iter().enumerate() { + out[i + third_align] = MaybeUninit::new(*byte); + } + for (i, byte) in 14u32.to_le_bytes().iter().enumerate() { + out[i + fourth_align] = MaybeUninit::new(*byte); + } + let out = unsafe { std::mem::transmute_copy::<[MaybeUninit; SIZE], MyStruct>(&out) }; + assert_eq!(out.a, 1234); + assert_eq!(out.b, 12); + assert_eq!(out.c, 13); + assert_eq!(out.d, 14); +} + +#[test] +fn test_serialize_const_layout_struct_list() { + #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] + struct Struct { + a: u32, + b: u8, + c: u32, + d: u32, + } + + impl Struct { + #[allow(dead_code)] + const fn equal(&self, other: &Struct) -> bool { + self.a == other.a && self.b == other.b && self.c == other.c && self.d == other.d + } + } + + #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)] + struct OtherStruct { + a: u32, + b: u8, + c: Struct, + d: u32, + } + + impl OtherStruct { + #[allow(dead_code)] + const fn equal(&self, other: &OtherStruct) -> bool { + self.a == other.a && self.b == other.b && self.c.equal(&other.c) && self.d == other.d + } + } + + const INNER_DATA: Struct = Struct { + a: 0x11111111, + b: 0x22, + c: 0x33333333, + d: 0x44444444, + }; + const DATA: [OtherStruct; 3] = [ + OtherStruct { + a: 0x11111111, + b: 0x22, + c: INNER_DATA, + d: 0x44444444, + }, + OtherStruct { + a: 0x111111, + b: 0x23, + c: INNER_DATA, + d: 0x44444444, + }, + OtherStruct { + a: 0x11111111, + b: 0x11, + c: INNER_DATA, + d: 0x44441144, + }, + ]; + + const _ASSERT: () = { + let mut buf = ConstVec::new(); + buf = serialize_const(&DATA, buf); + let buf = buf.read(); + let [first, second, third] = match deserialize_const!([OtherStruct; 3], buf) { + Some((_, data)) => data, + None => panic!("data mismatch"), + }; + if !(first.equal(&DATA[0]) && second.equal(&DATA[1]) && third.equal(&DATA[2])) { + panic!("data mismatch"); + } + }; + const _ASSERT_2: () = { + let mut buf = ConstVec::new(); + const DATA_AGAIN: [[OtherStruct; 3]; 3] = [DATA, DATA, DATA]; + buf = serialize_const(&DATA_AGAIN, buf); + let buf = buf.read(); + let [first, second, third] = match deserialize_const!([[OtherStruct; 3]; 3], buf) { + Some((_, data)) => data, + None => panic!("data mismatch"), + }; + if !(first[0].equal(&DATA[0]) && first[1].equal(&DATA[1]) && first[2].equal(&DATA[2])) { + panic!("data mismatch"); + } + if !(second[0].equal(&DATA[0]) && second[1].equal(&DATA[1]) && second[2].equal(&DATA[2])) { + panic!("data mismatch"); + } + if !(third[0].equal(&DATA[0]) && third[1].equal(&DATA[1]) && third[2].equal(&DATA[2])) { + panic!("data mismatch"); + } + }; + + let mut buf = ConstVec::new(); + buf = serialize_const(&DATA, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + let (_, data2) = deserialize_const!([OtherStruct; 3], buf).unwrap(); + assert_eq!(DATA, data2); +} + +#[test] +fn test_serialize_const_layout_struct() { + #[derive(Debug, PartialEq, SerializeConst)] + struct Struct { + a: u32, + b: u8, + c: u32, + d: u32, + } + + #[derive(Debug, PartialEq, SerializeConst)] + struct OtherStruct(u32, u8, Struct, u32); + + println!("{:?}", OtherStruct::MEMORY_LAYOUT); + + let data = Struct { + a: 0x11111111, + b: 0x22, + c: 0x33333333, + d: 0x44444444, + }; + let data = OtherStruct(0x11111111, 0x22, data, 0x44444444); + let mut buf = ConstVec::new(); + buf = serialize_const(&data, buf); + println!("{:?}", buf.as_ref()); + let buf = buf.read(); + let (_, data2) = deserialize_const!(OtherStruct, buf).unwrap(); + assert_eq!(data, data2); +} diff --git a/packages/const-serialize/tests/tuples.rs b/packages/const-serialize/tests/tuples.rs new file mode 100644 index 0000000000..43a036c413 --- /dev/null +++ b/packages/const-serialize/tests/tuples.rs @@ -0,0 +1,28 @@ +use const_serialize::{deserialize_const, serialize_const, ConstVec}; + +#[test] +fn test_serialize_const_layout_tuple() { + let mut buf = ConstVec::new(); + buf = serialize_const(&(1234u32, 5678u16), buf); + let buf = buf.read(); + assert_eq!( + deserialize_const!((u32, u16), buf).unwrap().1, + (1234u32, 5678u16) + ); + + let mut buf = ConstVec::new(); + buf = serialize_const(&(1234f64, 5678u16, 90u8), buf); + let buf = buf.read(); + assert_eq!( + deserialize_const!((f64, u16, u8), buf).unwrap().1, + (1234f64, 5678u16, 90u8) + ); + + let mut buf = ConstVec::new(); + buf = serialize_const(&(1234u32, 5678u16, 90u8, 1000000f64), buf); + let buf = buf.read(); + assert_eq!( + deserialize_const!((u32, u16, u8, f64), buf).unwrap().1, + (1234u32, 5678u16, 90u8, 1000000f64) + ); +} diff --git a/packages/dioxus/Cargo.toml b/packages/dioxus/Cargo.toml index 2806ae114f..b66949abfc 100644 --- a/packages/dioxus/Cargo.toml +++ b/packages/dioxus/Cargo.toml @@ -26,7 +26,7 @@ dioxus-desktop = { workspace = true, default-features = true, optional = true } dioxus-fullstack = { workspace = true, default-features = true, optional = true } dioxus-liveview = { workspace = true, optional = true } dioxus-ssr = { workspace = true, optional = true } -manganis = { workspace = true, optional = true } +manganis = { workspace = true, features = ["dioxus"], optional = true } serde = { version = "1.0.136", optional = true } diff --git a/packages/dioxus/src/lib.rs b/packages/dioxus/src/lib.rs index 35d99c49bc..9c60ac6276 100644 --- a/packages/dioxus/src/lib.rs +++ b/packages/dioxus/src/lib.rs @@ -128,7 +128,7 @@ pub mod prelude { #[cfg(feature = "asset")] #[cfg_attr(docsrs, doc(cfg(feature = "asset")))] - pub use manganis::{self, asset, Asset, ImageAsset, ImageType}; + pub use manganis::{self, *}; } #[cfg(feature = "web")] diff --git a/packages/manganis/manganis-core/Cargo.toml b/packages/manganis/manganis-core/Cargo.toml index 6af15c06de..f09f174d9f 100644 --- a/packages/manganis/manganis-core/Cargo.toml +++ b/packages/manganis/manganis-core/Cargo.toml @@ -13,5 +13,13 @@ keywords = ["dom", "ui", "gui", "assets"] # be careful with dependencies you add here - these need to get compiled for the proc macro and therefore # prevent the main code from compiling! [dependencies] -serde_json = "1.0" serde = { workspace = true, features = ["derive"] } +const-serialize = { workspace = true, features = ["serde"] } +dioxus-core-types = { workspace = true, optional = true } + +[dev-dependencies] +manganis = { workspace = true } +dioxus = { workspace = true } + +[features] +dioxus = ["dep:dioxus-core-types"] diff --git a/packages/manganis/manganis-core/assets/image.png b/packages/manganis/manganis-core/assets/image.png new file mode 100644 index 0000000000..4d57790481 Binary files /dev/null and b/packages/manganis/manganis-core/assets/image.png differ diff --git a/packages/manganis/manganis-core/assets/script.js b/packages/manganis/manganis-core/assets/script.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/manganis/manganis-core/assets/style.css b/packages/manganis/manganis-core/assets/style.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/manganis/manganis-core/src/asset.rs b/packages/manganis/manganis-core/src/asset.rs index cf4e83c5f7..e37dea1fe9 100644 --- a/packages/manganis/manganis-core/src/asset.rs +++ b/packages/manganis/manganis-core/src/asset.rs @@ -1,118 +1,156 @@ -use serde::{Deserialize, Serialize}; -use std::{ - hash::{Hash, Hasher}, - path::PathBuf, - time::SystemTime, -}; - -/// The location we'll write to the link section - needs to be serializable -/// -/// This basically is 1:1 with `manganis/Asset` but with more metadata to be useful to the macro and cli -#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] -pub struct ResourceAsset { - /// The input path `/assets/blah.css` - pub input: PathBuf, +use crate::AssetOptions; +use const_serialize::{ConstStr, SerializeConst}; +use std::path::PathBuf; + +/// An asset that should be copied by the bundler with some options. This type will be +/// serialized into the binary and added to the link section [`LinkSection::CURRENT`](crate::linker::LinkSection::CURRENT). +/// CLIs that support manganis, should pull out the assets from the link section, optimize, +/// and write them to the filesystem at [`BundledAsset::bundled_path`] for the application +/// to use. +#[derive( + Debug, + PartialEq, + PartialOrd, + Clone, + Copy, + Hash, + SerializeConst, + serde::Serialize, + serde::Deserialize, +)] +pub struct BundledAsset { + /// The absolute path of the asset + absolute_source_path: ConstStr, + /// The bundled path of the asset + bundled_path: ConstStr, + /// The options for the asset + options: AssetOptions, +} - /// The canonicalized asset - /// - /// `Users/dioxus/dev/app/assets/blah.css` - pub absolute: PathBuf, +impl BundledAsset { + #[doc(hidden)] + /// This should only be called from the macro + /// Create a new asset + pub const fn new( + absolute_source_path: &'static str, + bundled_path: &'static str, + options: AssetOptions, + ) -> Self { + Self { + absolute_source_path: ConstStr::new(absolute_source_path), + bundled_path: ConstStr::new(bundled_path), + options, + } + } - /// The post-bundle name of the asset - do we include the `assets` name? + #[doc(hidden)] + /// This should only be called from the macro + /// Create a new asset but with a relative path /// - /// `blahcss123.css` - pub bundled: String, -} - -impl ResourceAsset { - pub fn parse_any(raw: &str) -> Result { - // get the location where the asset is absolute, relative to - // - // IE - // /users/dioxus/dev/app/ - // is the root of - // /users/dioxus/dev/app/assets/blah.css - let manifest_dir = std::env::var("CARGO_MANIFEST_DIR") - .map(PathBuf::from) - .unwrap(); + /// This method is deprecated and will be removed in a future release. + #[deprecated( + note = "Relative asset!() paths are not supported. Use a path like `/assets/myfile.png` instead of `./assets/myfile.png`" + )] + pub const fn new_relative( + absolute_source_path: &'static str, + bundled_path: &'static str, + options: AssetOptions, + ) -> Self { + Self::new(absolute_source_path, bundled_path, options) + } - // 1. the input file should be a pathbuf - let input = PathBuf::from(raw); + /// Get the bundled name of the asset. This identifier cannot be used to read the asset directly + pub fn bundled_path(&self) -> &str { + self.bundled_path.as_str() + } - // 2. absolute path to the asset - let absolute = manifest_dir.join(raw.trim_start_matches('/')); - let absolute = - absolute - .canonicalize() - .map_err(|err| AssetParseError::AssetDoesntExist { - err, - path: absolute, - })?; + /// Get the absolute path of the asset source. This path will not be available when the asset is bundled + pub fn absolute_source_path(&self) -> &str { + self.absolute_source_path.as_str() + } + /// Get the options for the asset + pub const fn options(&self) -> &AssetOptions { + &self.options + } +} - // 3. the bundled path is the unique name of the asset - let bundled = Self::make_unique_name(absolute.clone())?; +/// A bundled asset with some options. The asset can be used in rsx! to reference the asset. +/// It should not be read directly with [`std::fs::read`] because the path needs to be resolved +/// relative to the bundle +/// +/// ```rust +/// # use manganis::{asset, Asset}; +/// # use dioxus::prelude::*; +/// const ASSET: Asset = asset!("/assets/image.png"); +/// rsx! { +/// img { src: ASSET } +/// }; +/// ``` +#[derive(Debug, PartialEq, Clone, Copy)] +pub struct Asset { + /// The bundled asset + bundled: BundledAsset, + /// The link section for the asset + keep_link_section: fn() -> u8, +} - Ok(Self { - input, - absolute, +impl Asset { + #[doc(hidden)] + /// This should only be called from the macro + /// Create a new asset from the bundled form of the asset and the link section + pub const fn new(bundled: BundledAsset, keep_link_section: fn() -> u8) -> Self { + Self { bundled, - }) + keep_link_section, + } } - fn make_unique_name(file_path: PathBuf) -> Result { - // Create a hasher - let mut hash = std::collections::hash_map::DefaultHasher::new(); - - // Open the file to get its options - let file = std::fs::File::open(&file_path).map_err(AssetParseError::FailedToReadAsset)?; - let modified = file - .metadata() - .and_then(|metadata| metadata.modified()) - .unwrap_or(SystemTime::UNIX_EPOCH); - - // Hash a bunch of metadata - // name, options, modified time, and maybe the version of our crate - // Hash the last time the file was updated and the file source. If either of these change, we need to regenerate the unique name - modified.hash(&mut hash); - file_path.hash(&mut hash); - - let uuid = hash.finish(); - let file_name = file_path - .file_stem() - .expect("file_path should have a file_stem") - .to_string_lossy(); + /// Get the bundled asset + pub const fn bundled(&self) -> &BundledAsset { + &self.bundled + } - let extension = file_path - .extension() - .map(|f| f.to_string_lossy()) - .unwrap_or_default(); + /// Return a canonicalized path to the asset + /// + /// Attempts to resolve it against an `assets` folder in the current directory. + /// If that doesn't exist, it will resolve against the cargo manifest dir + pub fn resolve(&self) -> PathBuf { + // Force a volatile read of the asset link section to ensure the symbol makes it into the binary + (self.keep_link_section)(); + + #[cfg(feature = "dioxus")] + // If the asset is relative, we resolve the asset at the current directory + if !dioxus_core_types::is_bundled_app() { + return PathBuf::from(self.bundled.absolute_source_path.as_str()); + } - Ok(format!("{file_name}-{uuid:x}.{extension}")) + // Otherwise presumably we're bundled and we can use the bundled path + PathBuf::from("/assets/").join(PathBuf::from( + self.bundled.bundled_path.as_str().trim_start_matches('/'), + )) } } -#[derive(Debug)] -pub enum AssetParseError { - ParseError(String), - AssetDoesntExist { - err: std::io::Error, - path: std::path::PathBuf, - }, - FailedToReadAsset(std::io::Error), - FailedToReadMetadata(std::io::Error), +impl From for String { + fn from(value: Asset) -> Self { + value.to_string() + } +} +impl From for Option { + fn from(value: Asset) -> Self { + Some(value.to_string()) + } } -impl std::fmt::Display for AssetParseError { +impl std::fmt::Display for Asset { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - AssetParseError::ParseError(err) => write!(f, "Failed to parse asset: {}", err), - AssetParseError::AssetDoesntExist { err, path } => { - write!(f, "Asset at {} doesn't exist: {}", path.display(), err) - } - AssetParseError::FailedToReadAsset(err) => write!(f, "Failed to read asset: {}", err), - AssetParseError::FailedToReadMetadata(err) => { - write!(f, "Failed to read asset metadata: {}", err) - } - } + write!(f, "{}", self.resolve().display()) + } +} + +#[cfg(feature = "dioxus")] +impl dioxus_core_types::DioxusFormattable for Asset { + fn format(&self) -> std::borrow::Cow<'static, str> { + std::borrow::Cow::Owned(self.to_string()) } } diff --git a/packages/manganis/manganis-core/src/css.rs b/packages/manganis/manganis-core/src/css.rs new file mode 100644 index 0000000000..974defd44c --- /dev/null +++ b/packages/manganis/manganis-core/src/css.rs @@ -0,0 +1,77 @@ +use const_serialize::SerializeConst; + +use crate::AssetOptions; + +/// Options for a css asset +#[derive( + Debug, + PartialEq, + PartialOrd, + Clone, + Copy, + Hash, + SerializeConst, + serde::Serialize, + serde::Deserialize, +)] +pub struct CssAssetOptions { + minify: bool, + preload: bool, +} + +impl Default for CssAssetOptions { + fn default() -> Self { + Self::new() + } +} + +impl CssAssetOptions { + /// Create a new css asset using the builder + pub const fn new() -> Self { + Self { + preload: false, + minify: true, + } + } + + /// Sets whether the css should be minified (default: true) + /// + /// Minifying the css can make your site load faster by loading less data + /// + /// ```rust + /// # use manganis::{asset, Asset, CssAssetOptions}; + /// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_minify(false)); + /// ``` + #[allow(unused)] + pub const fn with_minify(self, minify: bool) -> Self { + Self { minify, ..self } + } + + /// Check if the asset is minified + pub const fn minified(&self) -> bool { + self.minify + } + + /// Make the asset preloaded + /// + /// Preloading css will make the image start to load as soon as possible. This is useful for css that is used soon after the page loads or css that may not be used immediately, but should start loading sooner + /// + /// ```rust + /// # use manganis::{asset, Asset, CssAssetOptions}; + /// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_preload(true)); + /// ``` + #[allow(unused)] + pub const fn with_preload(self, preload: bool) -> Self { + Self { preload, ..self } + } + + /// Check if the asset is preloaded + pub const fn preloaded(&self) -> bool { + self.preload + } + + /// Convert the options into options for a generic asset + pub const fn into_asset_options(self) -> AssetOptions { + AssetOptions::Css(self) + } +} diff --git a/packages/manganis/manganis-core/src/folder.rs b/packages/manganis/manganis-core/src/folder.rs new file mode 100644 index 0000000000..06127cf093 --- /dev/null +++ b/packages/manganis/manganis-core/src/folder.rs @@ -0,0 +1,35 @@ +use const_serialize::SerializeConst; + +use crate::AssetOptions; + +/// The builder for [`FolderAsset`] +#[derive( + Debug, + PartialEq, + PartialOrd, + Clone, + Copy, + Hash, + SerializeConst, + serde::Serialize, + serde::Deserialize, +)] +pub struct FolderAssetOptions {} + +impl Default for FolderAssetOptions { + fn default() -> Self { + Self::new() + } +} + +impl FolderAssetOptions { + /// Create a new folder asset using the builder + pub const fn new() -> Self { + Self {} + } + + /// Convert the options into options for a generic asset + pub const fn into_asset_options(self) -> AssetOptions { + AssetOptions::Folder(self) + } +} diff --git a/packages/manganis/manganis-core/src/images.rs b/packages/manganis/manganis-core/src/images.rs new file mode 100644 index 0000000000..825dffb7a1 --- /dev/null +++ b/packages/manganis/manganis-core/src/images.rs @@ -0,0 +1,225 @@ +use const_serialize::SerializeConst; + +use crate::AssetOptions; + +/// The type of an image. You can read more about the tradeoffs between image formats [here](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types) +#[derive( + Debug, + PartialEq, + PartialOrd, + Clone, + Copy, + Hash, + SerializeConst, + serde::Serialize, + serde::Deserialize, +)] +#[repr(u8)] +pub enum ImageFormat { + /// A png image. Png images cannot contain transparency and tend to compress worse than other formats + Png, + /// A jpg image. Jpg images can contain transparency and tend to compress better than png images + Jpg, + /// A webp image. Webp images can contain transparency and tend to compress better than jpg images + Webp, + /// An avif image. Avif images can compress slightly better than webp images but are not supported by all browsers + Avif, + /// An unknown image type + Unknown, +} + +/// The size of an image asset +#[derive( + Debug, + PartialEq, + PartialOrd, + Clone, + Copy, + Hash, + SerializeConst, + serde::Serialize, + serde::Deserialize, +)] +#[repr(C, u8)] +pub enum ImageSize { + /// A manual size in pixels + Manual { + /// The width of the image in pixels + width: u32, + /// The height of the image in pixels + height: u32, + }, + /// The size will be automatically determined from the image source + Automatic, +} + +/// Options for an image asset +#[derive( + Debug, + PartialEq, + PartialOrd, + Clone, + Copy, + Hash, + SerializeConst, + serde::Serialize, + serde::Deserialize, +)] +pub struct ImageAssetOptions { + ty: ImageFormat, + low_quality_preview: bool, + size: ImageSize, + preload: bool, +} + +impl Default for ImageAssetOptions { + fn default() -> Self { + Self::new() + } +} + +impl ImageAssetOptions { + /// Create a new image asset options + pub const fn new() -> Self { + Self { + ty: ImageFormat::Unknown, + low_quality_preview: false, + size: ImageSize::Automatic, + preload: false, + } + } + + /// Make the asset preloaded + /// + /// Preloading an image will make the image start to load as soon as possible. This is useful for images that will be displayed soon after the page loads or images that may not be visible immediately, but should start loading sooner + /// + /// ```rust + /// # use manganis::{asset, Asset, ImageAssetOptions}; + /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_preload(true)); + /// ``` + pub const fn with_preload(self, preload: bool) -> Self { + Self { preload, ..self } + } + + /// Check if the asset is preloaded + pub const fn preloaded(&self) -> bool { + self.preload + } + + /// Sets the format of the image + /// + /// Choosing the right format can make your site load much faster. Webp and avif images tend to be a good default for most images + /// + /// ```rust + /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat}; + /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Webp)); + /// ``` + pub const fn with_format(self, format: ImageFormat) -> Self { + Self { ty: format, ..self } + } + + /// Sets the format of the image to [`ImageFormat::Avif`] + /// + /// Avif images tend to be a good default for most images rendered in browser because + /// they compress images well + /// + /// ```rust + /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat}; + /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_avif()); + /// ``` + pub const fn with_avif(self) -> Self { + self.with_format(ImageFormat::Avif) + } + + /// Sets the format of the image to [`ImageFormat::Webp`] + /// + /// Webp images tend to be a good default for most images rendered in browser because + /// they compress images well + /// + /// ```rust + /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat}; + /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_webp()); + /// ``` + pub const fn with_webp(self) -> Self { + self.with_format(ImageFormat::Webp) + } + + /// Sets the format of the image to [`ImageFormat::Jpg`] + /// + /// Jpeg images compress much better than [`ImageFormat::Png`], but worse than [`ImageFormat::Webp`] or [`ImageFormat::Avif`] + /// + /// ```rust + /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat}; + /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_jpg()); + /// ``` + pub const fn with_jpg(self) -> Self { + self.with_format(ImageFormat::Jpg) + } + + /// Sets the format of the image to [`ImageFormat::Png`] + /// + /// Png images don't compress very well, so they are not recommended for large images + /// + /// ```rust + /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat}; + /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_png()); + /// ``` + pub const fn with_png(self) -> Self { + self.with_format(ImageFormat::Png) + } + + /// Get the format of the image + pub const fn format(&self) -> ImageFormat { + self.ty + } + + /// Sets the size of the image + /// + /// If you only use the image in one place, you can set the size of the image to the size it will be displayed at. This will make the image load faster + /// + /// ```rust + /// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize}; + /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 512, height: 512 })); + /// ``` + + pub const fn with_size(self, size: ImageSize) -> Self { + Self { size, ..self } + } + + /// Get the size of the image + pub const fn size(&self) -> ImageSize { + self.size + } + + // LQIP is currently disabled until we have the CLI set up to inject the low quality image preview after the crate is built through the linker + // /// Make the image use a low quality preview + // /// + // /// A low quality preview is a small version of the image that will load faster. This is useful for large images on mobile devices that may take longer to load + // /// + // /// ```rust + // /// # use manganis::{asset, Asset, ImageAssetOptions}; + // /// const _: Asset = manganis::asset!("/assets/image.png", ImageAssetOptions::new().with_low_quality_image_preview()); + // /// ``` + // + // pub const fn with_low_quality_image_preview(self, low_quality_preview: bool) -> Self { + // Self { + // low_quality_preview, + // ..self + // } + // } + + /// Convert the options into options for a generic asset + pub const fn into_asset_options(self) -> AssetOptions { + AssetOptions::Image(self) + } + + pub(crate) const fn extension(&self) -> Option<&'static str> { + match self.ty { + ImageFormat::Png => Some("png"), + ImageFormat::Jpg => Some("jpg"), + ImageFormat::Webp => Some("webp"), + ImageFormat::Avif => Some("avif"), + ImageFormat::Unknown => None, + } + } +} diff --git a/packages/manganis/manganis-core/src/js.rs b/packages/manganis/manganis-core/src/js.rs new file mode 100644 index 0000000000..8c284ba448 --- /dev/null +++ b/packages/manganis/manganis-core/src/js.rs @@ -0,0 +1,77 @@ +use const_serialize::SerializeConst; + +use crate::AssetOptions; + +/// Options for a javascript asset +#[derive( + Debug, + PartialEq, + PartialOrd, + Clone, + Copy, + Hash, + SerializeConst, + serde::Serialize, + serde::Deserialize, +)] +pub struct JsAssetOptions { + minify: bool, + preload: bool, +} + +impl Default for JsAssetOptions { + fn default() -> Self { + Self::new() + } +} + +impl JsAssetOptions { + /// Create a new js asset builder + pub const fn new() -> Self { + Self { + minify: true, + preload: false, + } + } + + /// Sets whether the js should be minified (default: true) + /// + /// Minifying the js can make your site load faster by loading less data + /// + /// ```rust + /// # use manganis::{asset, Asset, JsAssetOptions}; + /// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_minify(false)); + /// ``` + #[allow(unused)] + pub const fn with_minify(self, minify: bool) -> Self { + Self { minify, ..self } + } + + /// Check if the asset is minified + pub const fn minified(&self) -> bool { + self.minify + } + + /// Make the asset preloaded + /// + /// Preloading the javascript will make the javascript start to load as soon as possible. This is useful for javascript that will be used soon after the page loads or javascript that may not be used immediately, but should start loading sooner + /// + /// ```rust + /// # use manganis::{asset, Asset, JsAssetOptions}; + /// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_preload(true)); + /// ``` + #[allow(unused)] + pub const fn with_preload(self, preload: bool) -> Self { + Self { preload, ..self } + } + + /// Check if the asset is preloaded + pub const fn preloaded(&self) -> bool { + self.preload + } + + /// Convert the options into options for a generic asset + pub const fn into_asset_options(self) -> AssetOptions { + AssetOptions::Js(self) + } +} diff --git a/packages/manganis/manganis-core/src/lib.rs b/packages/manganis/manganis-core/src/lib.rs index 4f5c4d7527..b3dddf836b 100644 --- a/packages/manganis/manganis-core/src/lib.rs +++ b/packages/manganis/manganis-core/src/lib.rs @@ -1,5 +1,19 @@ -mod asset; -mod linker; +mod folder; +pub use folder::*; + +mod images; +pub use images::*; + +mod options; +pub use options::*; + +mod css; +pub use css::*; +mod js; +pub use js::*; + +mod asset; pub use asset::*; -pub use linker::*; + +pub mod linker; diff --git a/packages/manganis/manganis-core/src/linker.rs b/packages/manganis/manganis-core/src/linker.rs index e86444cf42..1393b17735 100644 --- a/packages/manganis/manganis-core/src/linker.rs +++ b/packages/manganis/manganis-core/src/linker.rs @@ -1,3 +1,5 @@ +//! Utilities for working with Manganis assets in the linker. This module defines [`LinkSection`] which has information about what section manganis assets are stored in on each platform. + /// Information about the manganis link section for a given platform #[derive(Debug, Clone, Copy)] pub struct LinkSection { diff --git a/packages/manganis/manganis-core/src/options.rs b/packages/manganis/manganis-core/src/options.rs new file mode 100644 index 0000000000..714c1f9007 --- /dev/null +++ b/packages/manganis/manganis-core/src/options.rs @@ -0,0 +1,48 @@ +use const_serialize::SerializeConst; + +use crate::{CssAssetOptions, FolderAssetOptions, ImageAssetOptions, JsAssetOptions}; + +/// Settings for a generic asset +#[derive( + Debug, + PartialEq, + PartialOrd, + Clone, + Copy, + Hash, + SerializeConst, + serde::Serialize, + serde::Deserialize, +)] +#[repr(C, u8)] +#[non_exhaustive] +pub enum AssetOptions { + /// An image asset + Image(ImageAssetOptions), + /// A folder asset + Folder(FolderAssetOptions), + /// A css asset + Css(CssAssetOptions), + /// A javascript asset + Js(JsAssetOptions), + /// An unknown asset + Unknown, +} + +impl AssetOptions { + /// Try to get the extension for the asset. If the asset options don't define an extension, this will return None + pub const fn extension(&self) -> Option<&'static str> { + match self { + AssetOptions::Image(image) => image.extension(), + AssetOptions::Css(_) => Some("css"), + AssetOptions::Js(_) => Some("js"), + AssetOptions::Folder(_) => None, + AssetOptions::Unknown => None, + } + } + + /// Convert the options into options for a generic asset + pub const fn into_asset_options(self) -> Self { + self + } +} diff --git a/packages/manganis/manganis-macro/Cargo.toml b/packages/manganis/manganis-macro/Cargo.toml index 7a5f7ffd33..e1015b4ccc 100644 --- a/packages/manganis/manganis-macro/Cargo.toml +++ b/packages/manganis/manganis-macro/Cargo.toml @@ -18,9 +18,10 @@ proc-macro = true proc-macro2 = { version = "1.0" } quote = "1.0" syn = { version = "2.0", features = ["full", "extra-traits"] } -serde_json = "1.0" -serde = { workspace = true, features = ["derive"] } manganis-core = { workspace = true } [features] default = [] + +[dev-dependencies] +manganis.workspace = true diff --git a/packages/manganis/manganis-macro/assets/asset.txt b/packages/manganis/manganis-macro/assets/asset.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/manganis/manganis-macro/assets/image.png b/packages/manganis/manganis-macro/assets/image.png new file mode 100644 index 0000000000..4d57790481 Binary files /dev/null and b/packages/manganis/manganis-macro/assets/image.png differ diff --git a/packages/manganis/manganis-macro/assets/script.js b/packages/manganis/manganis-macro/assets/script.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/manganis/manganis-macro/assets/style.css b/packages/manganis/manganis-macro/assets/style.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/manganis/manganis-macro/src/asset.rs b/packages/manganis/manganis-macro/src/asset.rs index 7d2b995be5..07c2123221 100644 --- a/packages/manganis/manganis-macro/src/asset.rs +++ b/packages/manganis/manganis-macro/src/asset.rs @@ -1,14 +1,107 @@ -use manganis_core::{AssetParseError, ResourceAsset}; use proc_macro2::TokenStream as TokenStream2; use quote::{quote, ToTokens, TokenStreamExt}; +use std::{ + hash::Hasher, + io::Read, + path::{Path, PathBuf}, +}; use syn::{ parse::{Parse, ParseStream}, - LitStr, + LitStr, Token, }; +fn resolve_path(raw: &str) -> Result { + // Get the location of the root of the crate which is where all assets are relative to + // + // IE + // /users/dioxus/dev/app/ + // is the root of + // /users/dioxus/dev/app/assets/blah.css + let manifest_dir = std::env::var("CARGO_MANIFEST_DIR") + .map(PathBuf::from) + .unwrap(); + + // 1. the input file should be a pathbuf + let input = PathBuf::from(raw); + + // 2. absolute path to the asset + manifest_dir + .join(raw.trim_start_matches('/')) + .canonicalize() + .map_err(|err| AssetParseError::AssetDoesntExist { + err, + path: input.clone(), + }) +} + +fn hash_file_contents(file_path: &Path) -> Result { + // Create a hasher + let mut hash = std::collections::hash_map::DefaultHasher::new(); + + // If this is a folder, hash the folder contents + if file_path.is_dir() { + let files = + std::fs::read_dir(file_path).map_err(|err| AssetParseError::AssetDoesntExist { + err, + path: file_path.to_path_buf(), + })?; + for file in files.flatten() { + let path = file.path(); + hash_file_contents(&path)?; + } + return Ok(hash.finish()); + } + + // Otherwise, open the file to get its contents + let mut file = + std::fs::File::open(file_path).map_err(|err| AssetParseError::AssetDoesntExist { + err, + path: file_path.to_path_buf(), + })?; + + // We add a hash to the end of the file so it is invalidated when the bundled version of the file changes + // The hash includes the file contents, the options, and the version of manganis. From the macro, we just + // know the file contents, so we only include that hash + let mut buffer = [0; 8192]; + loop { + let read = file + .read(&mut buffer) + .map_err(AssetParseError::FailedToReadAsset)?; + if read == 0 { + break; + } + hash.write(&buffer[..read]); + } + + Ok(hash.finish()) +} + +#[derive(Debug)] +pub(crate) enum AssetParseError { + AssetDoesntExist { + err: std::io::Error, + path: std::path::PathBuf, + }, + FailedToReadAsset(std::io::Error), +} + +impl std::fmt::Display for AssetParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AssetParseError::AssetDoesntExist { err, path } => { + write!(f, "Asset at {} doesn't exist: {}", path.display(), err) + } + AssetParseError::FailedToReadAsset(err) => write!(f, "Failed to read asset: {}", err), + } + } +} + pub struct AssetParser { + /// The span of the source string + path_span: proc_macro2::Span, + /// The asset itself - asset: Result, + asset: Result, /// The source of the trailing options options: TokenStream2, @@ -26,8 +119,8 @@ impl Parse for AssetParser { // ``` // asset!( // "/assets/myfile.png", - // asset::image() - // .format(ImageType::Jpg) + // ImageAssetOptions::new() + // .format(ImageFormat::Jpg) // .size(512, 512) // ) // ``` @@ -36,29 +129,27 @@ impl Parse for AssetParser { fn parse(input: ParseStream) -> syn::Result { // And then parse the options let src = input.parse::()?; - let asset = ResourceAsset::parse_any(&src.value()); + let path_span = src.span(); + let asset = resolve_path(&src.value()); + let _comma = input.parse::(); let options = input.parse()?; - Ok(Self { asset, options }) + Ok(Self { + path_span, + asset, + options, + }) } } impl ToTokens for AssetParser { - // Need to generate: - // - // - 1. absolute file path on the user's system: `/users/dioxus/dev/project/assets/blah.css` - // - 2. original input in case that's useful: `../blah.css` - // - 3. path relative to the CARGO_MANIFEST_DIR - and then we'll add a `/`: `/assets/blah.css - // - 4. file from which this macro was called: `/users/dioxus/dev/project/src/lib.rs` - // - 5: The link section containing all this data - // - 6: the input tokens such that the builder gets validated by the const code - // - 7: the bundled name `/blahcss123.css` - // - // Not that we'll use everything, but at least we have this metadata for more post-processing. - // - // For now, `2` and `3` will be the same since we don't support relative paths... a bit of - // a limitation from rust itself. We technically could support them but not without some hoops - // to jump through + // The manganis macro outputs info to two different places: + // 1) The crate the macro was invoked in + // - It needs the hashed contents of the file, the file path, and the file options + // - Most of this is just forwarding the input, the only thing that the macro needs to do is hash the file contents + // 2) A bundler that supports manganis (currently just dioxus-cli) + // - The macro needs to output the absolute path to the asset for the bundler to find later + // - It also needs to serialize the bundled asset along with the asset options for the bundler to use later fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { let asset = match self.asset.as_ref() { Ok(asset) => asset, @@ -68,47 +159,60 @@ impl ToTokens for AssetParser { return; } }; + let asset_str = asset.display().to_string(); + let mut asset_str = proc_macro2::Literal::string(&asset_str); + asset_str.set_span(self.path_span); - // 1. the link section itself - let link_section = crate::generate_link_section(&asset); - - // 2. original - let input = asset.input.display().to_string(); - - // 3. resolved on the user's system - let local = asset.absolute.display().to_string(); - - // 4. bundled - let bundled = asset.bundled.to_string(); + let hash = match hash_file_contents(asset) { + Ok(hash) => hash, + Err(err) => { + let err = err.to_string(); + tokens.append_all(quote! { compile_error!(#err) }); + return; + } + }; - // 5. source tokens - let option_source = &self.options; + // Generate the link section for the asset + // The link section includes the source path and the output path of the asset + let link_section = crate::generate_link_section(quote!(__ASSET)); // generate the asset::new method to deprecate the `./assets/blah.css` syntax - let method = if !asset.input.starts_with("/") { + let constructor = if asset.is_relative() { quote::quote! { new_relative } } else { quote::quote! { new } }; + let options = if self.options.is_empty() { + quote! { manganis::AssetOptions::Unknown } + } else { + self.options.clone() + }; + tokens.extend(quote! { - Asset::#method( - { - #link_section - manganis::Asset { - // "/assets/blah.css" - input: #input, - - // "/users/dioxus/dev/app/assets/blah.css" - local: #local, - - // "/blahcss123.css" - bundled: #bundled, - - metadata: __volatile_reader as fn() -> u8, - } - } - ) #option_source + { + // We keep a hash of the contents of the asset for cache busting + const __ASSET_HASH: u64 = #hash; + // The source is used by the CLI to copy the asset + const __ASSET_SOURCE_PATH: &'static str = #asset_str; + // The options give the CLI info about how to process the asset + // Note: into_asset_options is not a trait, so we cannot accept the options directly + // in the constructor. Stable rust doesn't have support for constant functions in traits + const __ASSET_OPTIONS: manganis::AssetOptions = #options.into_asset_options(); + // We calculate the bundled path from the hash and any transformations done by the options + // This is the final path that the asset will be written to + const __ASSET_BUNDLED_PATH: manganis::macro_helpers::const_serialize::ConstStr = manganis::macro_helpers::generate_unique_path(__ASSET_SOURCE_PATH, __ASSET_HASH, &__ASSET_OPTIONS); + // Get the reference to the string that was generated. We cannot return &'static str from + // generate_unique_path because it would return a reference to data generated in the function + const __ASSET_BUNDLED_PATH_STR: &'static str = __ASSET_BUNDLED_PATH.as_str(); + // Create the asset that the crate will use. This is used both in the return value and + // added to the linker for the bundler to copy later + const __ASSET: manganis::BundledAsset = manganis::BundledAsset::#constructor(__ASSET_SOURCE_PATH, __ASSET_BUNDLED_PATH_STR, __ASSET_OPTIONS); + + #link_section + + manganis::Asset::new(__ASSET, __keep_link_section) + } }) } } diff --git a/packages/manganis/manganis-macro/src/lib.rs b/packages/manganis/manganis-macro/src/lib.rs index 05d9d9850d..078b137ca2 100644 --- a/packages/manganis/manganis-macro/src/lib.rs +++ b/packages/manganis/manganis-macro/src/lib.rs @@ -11,6 +11,42 @@ pub(crate) mod linker; use linker::generate_link_section; /// The asset macro collects assets that will be included in the final binary +/// +/// # Files +/// +/// The file builder collects an arbitrary file. Relative paths are resolved relative to the package root +/// ```rust +/// # use manganis::{asset, Asset}; +/// const _: Asset = asset!("/assets/asset.txt"); +/// ``` +/// Or you can use URLs to read the asset at build time from a remote location +/// ```rust +/// # use manganis::{asset, Asset}; +/// const _: Asset = asset!("/assets/image.png"); +/// ``` +/// +/// # Images +/// +/// You can collect images which will be automatically optimized with the image builder: +/// ```rust +/// # use manganis::{asset, Asset}; +/// const _: Asset = asset!("/assets/image.png"); +/// ``` +/// Resize the image at compile time to make the assets file size smaller: +/// ```rust +/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize}; +/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 52, height: 52 })); +/// ``` +/// Or convert the image at compile time to a web friendly format: +/// ```rust +/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize, ImageFormat}; +/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Avif)); +/// ``` +/// You can mark images as preloaded to make them load faster in your app +/// ```rust +/// # use manganis::{asset, Asset, ImageAssetOptions}; +/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_preload(true)); +/// ``` #[proc_macro] pub fn asset(input: TokenStream) -> TokenStream { let asset = parse_macro_input!(input as asset::AssetParser); diff --git a/packages/manganis/manganis-macro/src/linker.rs b/packages/manganis/manganis-macro/src/linker.rs index 745c472e59..42fad42011 100644 --- a/packages/manganis/manganis-macro/src/linker.rs +++ b/packages/manganis/manganis-macro/src/linker.rs @@ -1,25 +1,33 @@ use proc_macro2::TokenStream as TokenStream2; -use serde::Serialize; +use quote::ToTokens; -/// this new approach will store the assets descriptions *inside the executable*. -/// The trick is to use the `link_section` attribute. -/// We force rust to store a json representation of the asset description +/// We store description of the assets an application uses in the executable. +/// We use the `link_section` attribute embed an extra section in the executable. +/// We force rust to store a serialized representation of the asset description /// inside a particular region of the binary, with the label "manganis". -/// After linking, the "manganis" sections of the different executables will be merged. -pub fn generate_link_section(asset: &impl Serialize) -> TokenStream2 { +/// After linking, the "manganis" sections of the different object files will be merged. +pub fn generate_link_section(asset: impl ToTokens) -> TokenStream2 { let position = proc_macro2::Span::call_site(); - let asset_description = serde_json::to_string(asset).unwrap(); - let len = asset_description.as_bytes().len(); - let asset_bytes = syn::LitByteStr::new(asset_description.as_bytes(), position); - let section_name = syn::LitStr::new(manganis_core::LinkSection::CURRENT.link_section, position); + let section_name = syn::LitStr::new( + manganis_core::linker::LinkSection::CURRENT.link_section, + position, + ); quote::quote! { + // First serialize the asset into a constant sized buffer + const __BUFFER: manganis::macro_helpers::const_serialize::ConstVec = manganis::macro_helpers::serialize_asset(&#asset); + // Then pull out the byte slice + const __BYTES: &[u8] = __BUFFER.as_ref(); + // And the length of the byte slice + const __LEN: usize = __BYTES.len(); + + // Now that we have the size of the asset, copy the bytes into a static array #[link_section = #section_name] #[used] - static ASSET: [u8; #len] = * #asset_bytes; + static __LINK_SECTION: [u8; __LEN] = manganis::macro_helpers::copy_bytes(__BYTES); - fn __volatile_reader() -> u8 { - unsafe { std::ptr::read_volatile(ASSET.as_ptr()) } + fn __keep_link_section() -> u8 { + unsafe { std::ptr::read_volatile(__LINK_SECTION.as_ptr()) } } } } diff --git a/packages/manganis/manganis/Cargo.toml b/packages/manganis/manganis/Cargo.toml index 82d74cc674..306831d05d 100644 --- a/packages/manganis/manganis/Cargo.toml +++ b/packages/manganis/manganis/Cargo.toml @@ -13,29 +13,10 @@ keywords = ["assets"] [lib] [dependencies] -manganis-macro = { workspace = true, optional = true } -dioxus-core-types = { workspace = true } - -# dunce = "1.0.2" -# [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] -# core-foundation = "0.10.0" -# [target.'cfg(target_os = "macos")'.dependencies] -# core-foundation = "0.9.3" -# [target.'cfg(target_os ="macos")'.dependencies] -# infer = { workspace = true } -# dirs = "5.0.1" -# infer = { workspace = true } -# manganis-common = { workspace = true } - -once_cell = "1.19.0" -dunce = "1.0.2" -serde = { version = "1.0.183", features = ["derive"] } -anyhow = "1" -base64 = { workspace = true } - +const-serialize = { workspace = true } +manganis-core = { workspace = true } +manganis-macro = { workspace = true } [features] -default = ["macro"] -html = [] -# url-encoding = ["manganis-macro/url-encoding"] -macro = ["dep:manganis-macro"] +default = [] +dioxus = ["manganis-core/dioxus"] diff --git a/packages/manganis/manganis/README.md b/packages/manganis/manganis/README.md new file mode 100644 index 0000000000..51917bd120 --- /dev/null +++ b/packages/manganis/manganis/README.md @@ -0,0 +1,31 @@ +# Manganis + +The Manganis allows you to submit assets to any build tool that supports collecting assets. It makes it easy to self-host assets that are distributed throughout your libraries. Manganis also handles optimizing, converting, and fetching assets. + +If you defined this in a component library: + +```rust +use manganis::{Asset, asset}; +const AVIF_ASSET: Asset = manganis::asset!("/assets/image.png"); +``` + +AVIF_ASSET will be set to a new file name that will be served by some CLI. That file can be collected by any package that depends on the component library. + +```rust +use manganis::{ImageFormat, ImageAssetOptions, Asset, asset, ImageSize}; +// You can collect arbitrary files. Absolute paths are resolved relative to the package root +const _: Asset = asset!("/assets/script.js"); + +// You can collect images which will be automatically optimized +pub const PNG_ASSET: Asset = + asset!("/assets/image.png"); +// Resize the image at compile time to make the assets smaller +pub const RESIZED_PNG_ASSET: Asset = + asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 52, height: 52 })); +// Or convert the image at compile time to a web friendly format +pub const AVIF_ASSET: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Avif)); +``` + +## Adding Support to Your CLI + +To add support for your CLI, you need to integrate with the [manganis_cli_support](https://github.com/DioxusLabs/manganis/tree/main/cli-support) crate. This crate provides utilities to collect assets that integrate with the Manganis macro. It makes it easy to integrate an asset collection and optimization system into a build tool. diff --git a/packages/manganis/manganis/assets/image.png b/packages/manganis/manganis/assets/image.png new file mode 100644 index 0000000000..4d57790481 Binary files /dev/null and b/packages/manganis/manganis/assets/image.png differ diff --git a/packages/manganis/manganis/assets/script.js b/packages/manganis/manganis/assets/script.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/manganis/manganis/assets/style.css b/packages/manganis/manganis/assets/style.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/manganis/manganis/src/builder.rs b/packages/manganis/manganis/src/builder.rs deleted file mode 100644 index 91d6ff0e47..0000000000 --- a/packages/manganis/manganis/src/builder.rs +++ /dev/null @@ -1,87 +0,0 @@ -use dioxus_core_types::DioxusFormattable; -use std::path::PathBuf; - -/// Asset -#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Hash)] -pub struct Asset { - /// The input URI given to the macro - pub input: &'static str, - - /// The absolute path to the asset on the filesystem - pub local: &'static str, - - /// The asset location after its been bundled - /// - /// `blah-123.css`` - pub bundled: &'static str, - - /// The metadata for this asset plumbed by the compiler - pub metadata: fn() -> u8, -} - -impl Asset { - /// Create a new asset - pub const fn new(self) -> Self { - self - } - - /// Create a new asset but with a relative path - /// - /// This method is deprecated and will be removed in a future release. - #[deprecated( - note = "Relative asset!() paths are not supported. Use a path like `/assets/myfile.png` instead of `./assets/myfile.png`" - )] - pub const fn new_relative(self) -> Self { - self - } - - /// Get the path to the asset - pub fn path(&self) -> PathBuf { - PathBuf::from(self.input.to_string()) - } - - /// Get the path to the asset - pub fn relative_path(&self) -> PathBuf { - PathBuf::from(self.input.trim_start_matches('/').to_string()) - } - - /// Return a canonicalized path to the asset - /// - /// Attempts to resolve it against an `assets` folder in the current directory. - /// If that doesn't exist, it will resolve against the cargo manifest dir - pub fn resolve(&self) -> PathBuf { - // Force a volatile read of the metadata to ensure the symbol makes it into the binary - (self.metadata)(); - - // If the asset is relative, we resolve the asset at the current directory - if !dioxus_core_types::is_bundled_app() { - return PathBuf::from(self.local); - } - - // Otherwise presumably we're bundled and we can use the bundled path - PathBuf::from("/assets/").join(PathBuf::from(self.bundled.trim_start_matches('/'))) - } -} - -impl From for String { - fn from(value: Asset) -> Self { - value.to_string() - } -} -impl From for Option { - fn from(value: Asset) -> Self { - Some(value.to_string()) - } -} - -impl std::fmt::Display for Asset { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.resolve().display()) - } -} - -impl DioxusFormattable for Asset { - fn format(&self) -> std::borrow::Cow<'static, str> { - std::borrow::Cow::Owned(self.to_string()) - } -} diff --git a/packages/manganis/manganis/src/folder.rs b/packages/manganis/manganis/src/folder.rs deleted file mode 100644 index 971b6927aa..0000000000 --- a/packages/manganis/manganis/src/folder.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::Asset; - -/// This is basically a compile-time version of ResourceAsset -/// A struct that contains the relative and absolute paths of an asset -#[derive(Debug, PartialEq, PartialOrd, Clone, Hash)] -pub struct FolderAsset { - src: Asset, -} - -impl Asset { - /// Create a new folder using `Asset` as the base type - pub const fn folder(self) -> FolderAsset { - FolderAsset::new(self) - } -} - -impl FolderAsset { - /// Create a new folder asset from an `Asset` - pub const fn new(src: Asset) -> Self { - Self { src } - } -} - -/// The builder for [`FolderAsset`] -pub struct FolderAssetBuilder; - -/// Create an folder asset from the local path -#[allow(unused)] -pub const fn folder(src: &'static str) -> FolderAssetBuilder { - FolderAssetBuilder {} -} diff --git a/packages/manganis/manganis/src/hash.rs b/packages/manganis/manganis/src/hash.rs new file mode 100644 index 0000000000..98dbb9252e --- /dev/null +++ b/packages/manganis/manganis/src/hash.rs @@ -0,0 +1,39 @@ +use const_serialize::{serialize_const, ConstVec, SerializeConst}; + +// From rustchash - https://github.com/rust-lang/rustc-hash/blob/6745258da00b7251bed4a8461871522d0231a9c7/src/lib.rs#L98 +const K: u64 = 0xf1357aea2e62a9c5; + +pub(crate) struct ConstHasher { + hash: u64, +} + +impl ConstHasher { + pub const fn new() -> Self { + Self { hash: 0 } + } + + pub const fn finish(&self) -> u64 { + self.hash + } + + pub const fn write(mut self, bytes: &[u8]) -> Self { + let mut i = 0; + while i < bytes.len() { + self = self.write_byte(bytes[i]); + i += 1; + } + self + } + + pub const fn write_byte(mut self, byte: u8) -> Self { + self.hash = self.hash.wrapping_add(byte as u64).wrapping_mul(K); + self + } + + pub const fn hash_by_bytes(self, item: &T) -> Self { + let mut bytes = ConstVec::new(); + bytes = serialize_const(item, bytes); + let bytes = bytes.as_ref(); + self.write(bytes) + } +} diff --git a/packages/manganis/manganis/src/images.rs b/packages/manganis/manganis/src/images.rs deleted file mode 100644 index b5a83060e4..0000000000 --- a/packages/manganis/manganis/src/images.rs +++ /dev/null @@ -1,141 +0,0 @@ -use dioxus_core_types::DioxusFormattable; - -use crate::Asset; - -/// An image asset that is built by the [`asset!`] macro -#[derive(Debug, PartialEq, PartialOrd, Clone, Hash, Copy)] -pub struct ImageAsset { - /// The path to the image - asset: Asset, - /// A low quality preview of the image that is URL encoded - preview: Option<&'static str>, - /// A caption for the image - caption: Option<&'static str>, - /// The format of the image - format: Option, -} - -impl Asset { - /// Convert this asset into an image asset - pub const fn image(self) -> ImageAsset { - ImageAsset::new(self) - } -} - -impl ImageAsset { - /// Creates a new image asset - pub const fn new(path: Asset) -> Self { - Self { - asset: path, - preview: None, - caption: None, - format: None, - } - } - - /// Returns the path to the image - pub const fn path(&self) -> &'static str { - self.asset.bundled - } - - /// Returns the preview of the image - pub const fn preview(&self) -> Option<&'static str> { - self.preview - } - - /// Sets the preview of the image - pub const fn with_preview(self, preview: Option<&'static str>) -> Self { - Self { preview, ..self } - } - - /// Returns the caption of the image - pub const fn caption(&self) -> Option<&'static str> { - self.caption - } - - /// Sets the caption of the image - pub const fn with_caption(self, caption: Option<&'static str>) -> Self { - Self { caption, ..self } - } - - /// Sets the format of the image - pub const fn format(self, format: Option) -> Self { - Self { format, ..self } - } -} - -impl std::ops::Deref for ImageAsset { - type Target = Asset; - - fn deref(&self) -> &Self::Target { - &self.asset - } -} - -impl std::fmt::Display for ImageAsset { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.asset.fmt(f) - } -} - -impl DioxusFormattable for ImageAsset { - fn format(&self) -> std::borrow::Cow<'static, str> { - std::borrow::Cow::Owned(self.to_string()) - } -} - -/// The type of an image. You can read more about the tradeoffs between image formats [here](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types) -#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Hash)] -pub enum ImageType { - /// A png image. Png images cannot contain transparency and tend to compress worse than other formats - Png, - /// A jpg image. Jpg images can contain transparency and tend to compress better than png images - Jpg, - /// A webp image. Webp images can contain transparency and tend to compress better than jpg images - Webp, - /// An avif image. Avif images can compress slightly better than webp images but are not supported by all browsers - Avif, -} - -/// A builder for an image asset. This must be used in the [`asset!`] macro. -/// -/// > **Note**: This will do nothing outside of the `asset!` macro -pub struct ImageAssetBuilder; - -impl ImageAssetBuilder { - /// Sets the format of the image - #[allow(unused)] - pub const fn format(self, format: ImageType) -> Self { - Self - } - - /// Sets the size of the image - #[allow(unused)] - pub const fn size(self, x: u32, y: u32) -> Self { - Self - } - - /// Make the image use a low quality preview - #[allow(unused)] - pub const fn low_quality_preview(self) -> Self { - Self - } - - /// Make the image preloaded - #[allow(unused)] - pub const fn preload(self) -> Self { - Self - } - - /// Make the image URL encoded - #[allow(unused)] - pub const fn url_encoded(self) -> Self { - Self - } -} - -/// Create an image asset from the local path or url to the image -#[allow(unused)] -pub const fn image(path: &'static str) -> ImageAssetBuilder { - ImageAssetBuilder -} diff --git a/packages/manganis/manganis/src/lib.rs b/packages/manganis/manganis/src/lib.rs index 9302470f72..5e5b949bda 100644 --- a/packages/manganis/manganis/src/lib.rs +++ b/packages/manganis/manganis/src/lib.rs @@ -1,15 +1,12 @@ -//! Manganis is a Rust library for bundling assets into a final binary. - +#![doc = include_str!("../README.md")] #![deny(missing_docs)] -#[cfg(feature = "macro")] -pub use manganis_macro::*; - -mod folder; -pub use folder::*; - -mod images; -pub use images::*; +mod hash; +#[doc(hidden)] +pub mod macro_helpers; +pub use manganis_macro::asset; -mod builder; -pub use builder::*; +pub use manganis_core::{ + Asset, AssetOptions, BundledAsset, CssAssetOptions, FolderAssetOptions, ImageAssetOptions, + ImageFormat, ImageSize, JsAssetOptions, +}; diff --git a/packages/manganis/manganis/src/macro_helpers.rs b/packages/manganis/manganis/src/macro_helpers.rs new file mode 100644 index 0000000000..e84ff29f90 --- /dev/null +++ b/packages/manganis/manganis/src/macro_helpers.rs @@ -0,0 +1,144 @@ +pub use const_serialize; +use const_serialize::{serialize_const, ConstStr, ConstVec}; +use manganis_core::{AssetOptions, BundledAsset}; + +use crate::hash::ConstHasher; + +/// Format the input path with a hash to create an unique output path for the macro in the form `{input_path}-{hash}.{extension}` +pub const fn generate_unique_path( + input_path: &str, + content_hash: u64, + asset_config: &AssetOptions, +) -> ConstStr { + // Format the unique path with the format `{input_path}-{hash}.{extension}` + // Start with the input path + let mut input_path = ConstStr::new(input_path); + // Then strip the prefix from the input path. The path comes from the build platform, but + // in wasm, we don't know what the path separator is from the build platform. We need to + // split by both unix and windows paths and take the smallest one + let mut extension = None; + match (input_path.rsplit_once('/'), input_path.rsplit_once('\\')) { + (Some((_, unix_new_input_path)), Some((_, windows_new_input_path))) => { + input_path = if unix_new_input_path.len() < windows_new_input_path.len() { + unix_new_input_path + } else { + windows_new_input_path + }; + } + (Some((_, unix_new_input_path)), _) => { + input_path = unix_new_input_path; + } + (_, Some((_, windows_new_input_path))) => { + input_path = windows_new_input_path; + } + _ => {} + } + if let Some((new_input_path, new_extension)) = input_path.rsplit_once('.') { + extension = Some(new_extension); + input_path = new_input_path; + } + // Then add a dash + let mut macro_output_path = input_path.push_str("-"); + + // Hash the contents along with the asset config to create a unique hash for the asset + // When this hash changes, the client needs to re-fetch the asset + let mut hasher = ConstHasher::new(); + hasher = hasher.write(&content_hash.to_le_bytes()); + hasher = hasher.hash_by_bytes(asset_config); + let hash = hasher.finish(); + + // Then add the hash in hex form + let hash_bytes = hash.to_le_bytes(); + let mut i = 0; + while i < hash_bytes.len() { + let byte = hash_bytes[i]; + let first = byte >> 4; + let second = byte & 0x0f; + const fn byte_to_char(byte: u8) -> char { + match char::from_digit(byte as u32, 16) { + Some(c) => c, + None => panic!("byte must be a valid digit"), + } + } + macro_output_path = macro_output_path.push(byte_to_char(first)); + macro_output_path = macro_output_path.push(byte_to_char(second)); + i += 1; + } + + // Finally add the extension + match asset_config.extension() { + Some(extension) => { + macro_output_path = macro_output_path.push('.'); + macro_output_path = macro_output_path.push_str(extension) + } + None => { + if let Some(extension) = extension { + macro_output_path = macro_output_path.push('.'); + macro_output_path = macro_output_path.push_str(extension.as_str()) + } + } + } + + macro_output_path +} + +#[test] +fn test_unique_path() { + use manganis_core::{ImageAssetOptions, ImageFormat}; + use std::path::PathBuf; + let mut input_path = PathBuf::from("some"); + input_path.push("prefix"); + input_path.push("test.png"); + let content_hash = 123456789; + let asset_config = AssetOptions::Image(ImageAssetOptions::new().with_format(ImageFormat::Avif)); + let output_path = + generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config); + assert_eq!(output_path.as_str(), "test-603a88fe296462a3.avif"); + + // Changing the path without changing the contents shouldn't change the hash + let mut input_path = PathBuf::from("some"); + input_path.push("prefix"); + input_path.push("prefix"); + input_path.push("test.png"); + let content_hash = 123456789; + let asset_config = AssetOptions::Image(ImageAssetOptions::new().with_format(ImageFormat::Avif)); + let output_path = + generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config); + assert_eq!(output_path.as_str(), "test-603a88fe296462a3.avif"); + + let mut input_path = PathBuf::from("test"); + input_path.push("ing"); + input_path.push("test"); + let content_hash = 123456789; + let asset_config = AssetOptions::Unknown; + let output_path = + generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config); + assert_eq!(output_path.as_str(), "test-c8c4cfad21cac262"); + + // Just changing the content hash should change the total hash + let mut input_path = PathBuf::from("test"); + input_path.push("ing"); + input_path.push("test"); + let content_hash = 123456780; + let asset_config = AssetOptions::Unknown; + let output_path = + generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config); + assert_eq!(output_path.as_str(), "test-7bced03789ff865c"); +} + +/// Serialize an asset to a const buffer +pub const fn serialize_asset(asset: &BundledAsset) -> ConstVec { + let write = ConstVec::new(); + serialize_const(asset, write) +} + +/// Copy a slice into a constant sized buffer at compile time +pub const fn copy_bytes(bytes: &[u8]) -> [u8; N] { + let mut out = [0; N]; + let mut i = 0; + while i < N { + out[i] = bytes[i]; + i += 1; + } + out +}