From 573929beee44ac30f0692e74d76eebd809f93861 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Sun, 6 Oct 2024 21:29:58 +0300 Subject: [PATCH] fix: daletpack optimizing --- Cargo.lock | 289 +++++++++++------------------------ Cargo.toml | 1 - examples/bench.rs | 18 --- src/daletl.rs | 8 +- src/daletpack/encode.rs | 118 ++++---------- src/daletpack/types.rs | 49 +++--- src/traits/from_daletl.rs | 36 ++--- src/traits/is_null_daletl.rs | 4 +- src/traits/to_daletl.rs | 18 +-- 9 files changed, 166 insertions(+), 375 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e4b617..5dbbe69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,10 +3,10 @@ version = 3 [[package]] -name = "adler" -version = "1.0.2" +name = "adler2" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" [[package]] name = "ahash" @@ -71,7 +71,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -81,7 +81,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -96,20 +96,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" - -[[package]] -name = "bigdecimal" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6773ddc0eafc0e509fb60e48dff7f450f8e674a0686ae8605e8d9901bd5eefa" -dependencies = [ - "num-bigint", - "num-integer", - "num-traits", -] +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "bincode" @@ -128,12 +117,13 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cc" -version = "1.1.7" +version = "1.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26a5c3fd7bfa1ce3897a3a3501d362b2d87b7f2583ebcb4a949ec25911025cbc" +checksum = "2e80e3b6a3ab07840e1cae9b0666a63970dc28e8ed5ffbcdacbfc760c281bfc1" dependencies = [ "jobserver", "libc", + "shlex", ] [[package]] @@ -148,7 +138,7 @@ version = "1.0.0-alpha.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7b80276986f86789dc56ca6542d53bba9cda3c66091ebbe7bd96fc1bdf20f1f" dependencies = [ - "hashbrown", + "hashbrown 0.14.5", "regex-automata", "serde", "stacker", @@ -157,9 +147,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.13" +version = "4.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fbb260a053428790f3de475e304ff84cdbc4face759ea7a3e64c1edd938a7fc" +checksum = "7be5744db7978a28d9df86a214130d106a89ce49644cbc4e3f0c22c3fba30615" dependencies = [ "clap_builder", "clap_derive", @@ -167,9 +157,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.13" +version = "4.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64b17d7ea74e9f833c7dbf2cbe4fb12ff26783eda4782a8975b72f895c9b4d99" +checksum = "a5fbc17d3ef8278f55b282b2a2e75ae6f6c7d4bb70ed3d0382375104bfafdb4b" dependencies = [ "anstream", "anstyle", @@ -179,14 +169,14 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.13" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.72", + "syn", ] [[package]] @@ -218,7 +208,6 @@ dependencies = [ "bincode", "chumsky", "clap", - "dlhn", "enum-procs", "flate2", "num_enum", @@ -228,41 +217,6 @@ dependencies = [ "zstd", ] -[[package]] -name = "deranged" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" -dependencies = [ - "powerfmt", -] - -[[package]] -name = "dlhn" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56f8c0fbf92cd49450565bc2231273fabc48bbbf0d152b5b32ee4ce13eed469e" -dependencies = [ - "bigdecimal", - "dlhn_derive", - "num-bigint", - "num-traits", - "serde", - "serde_bytes", - "time", -] - -[[package]] -name = "dlhn_derive" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e579511d5845df93b92409782ce0e5cdbc10dd239f5aa4c9e53f229d6ac620c0" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "enum-procs" version = "0.3.0" @@ -270,7 +224,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e188616f4527e2cf6128e58622bb2fd16ebcfa37a200b2d16739c3ca227bf649" dependencies = [ "quote", - "syn 2.0.72", + "syn", ] [[package]] @@ -281,9 +235,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "flate2" -version = "1.0.30" +version = "1.0.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae" +checksum = "a1b589b4dc103969ad3cf85c950899926ec64300a1a46d76c03a6072957036f0" dependencies = [ "crc32fast", "miniz_oxide", @@ -299,6 +253,12 @@ dependencies = [ "allocator-api2", ] +[[package]] +name = "hashbrown" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" + [[package]] name = "heck" version = "0.5.0" @@ -307,12 +267,12 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "indexmap" -version = "2.2.6" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.15.0", ] [[package]] @@ -332,9 +292,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.155" +version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" [[package]] name = "memchr" @@ -344,36 +304,11 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "miniz_oxide" -version = "0.7.4" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" dependencies = [ - "adler", -] - -[[package]] -name = "num-bigint" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" -dependencies = [ - "num-integer", - "num-traits", -] - -[[package]] -name = "num-conv" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" - -[[package]] -name = "num-integer" -version = "0.1.46" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" -dependencies = [ - "num-traits", + "adler2", ] [[package]] @@ -403,14 +338,14 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.72", + "syn", ] [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" [[package]] name = "paste" @@ -420,21 +355,15 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pkg-config" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" - -[[package]] -name = "powerfmt" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "proc-macro-crate" -version = "3.1.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" dependencies = [ "toml_edit", ] @@ -450,18 +379,18 @@ dependencies = [ [[package]] name = "psm" -version = "0.1.21" +version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5787f7cda34e3033a72192c018bc5883100330f362ef279a8cbccfce8bb4e874" +checksum = "aa37f80ca58604976033fae9515a8a2989fc13797d953f7c04fb8fa36a11f205" dependencies = [ "cc", ] [[package]] name = "quote" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -507,31 +436,22 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.204" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] -[[package]] -name = "serde_bytes" -version = "0.11.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "387cc504cb06bb40a96c8e04e951fe01854cf6bc921053c954e4a606d9675c6a" -dependencies = [ - "serde", -] - [[package]] name = "serde_derive" -version = "1.0.204" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn", ] [[package]] @@ -542,20 +462,26 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn", ] [[package]] -name = "stacker" -version = "0.1.15" +name = "shlex" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[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", - "winapi", + "windows-sys 0.59.0", ] [[package]] @@ -566,45 +492,15 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "1.0.109" +version = "2.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] -[[package]] -name = "syn" -version = "2.0.72" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "time" -version = "0.3.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" -dependencies = [ - "deranged", - "num-conv", - "powerfmt", - "serde", - "time-core", -] - -[[package]] -name = "time-core" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" - [[package]] name = "toml_datetime" version = "0.6.8" @@ -613,9 +509,9 @@ checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" [[package]] name = "toml_edit" -version = "0.21.1" +version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ "indexmap", "toml_datetime", @@ -624,15 +520,15 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "unicode-width" -version = "0.1.13" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" [[package]] name = "utf8parse" @@ -646,28 +542,6 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - [[package]] name = "windows-sys" version = "0.52.0" @@ -677,6 +551,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.6" @@ -743,9 +626,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.5.40" +version = "0.6.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" dependencies = [ "memchr", ] @@ -773,7 +656,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn", ] [[package]] @@ -787,18 +670,18 @@ dependencies = [ [[package]] name = "zstd-safe" -version = "7.2.0" +version = "7.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa556e971e7b568dc775c136fc9de8c779b1c2fc3a63defaafadffdbd3181afa" +checksum = "54a3ab4db68cea366acc5c897c7b4d4d1b8994a9cd6e6f841f8964566a419059" dependencies = [ "zstd-sys", ] [[package]] name = "zstd-sys" -version = "2.0.12+zstd.1.5.6" +version = "2.0.13+zstd.1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a4e40c320c3cb459d9a9ff6de98cff88f4751ee9275d140e2be94a2b74e4c13" +checksum = "38ff0f21cfee8f97d94cef41359e0c89aa6113028ab0291aa8ca0038995a95aa" dependencies = [ "cc", "pkg-config", diff --git a/Cargo.toml b/Cargo.toml index 550d160..98ed793 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,6 @@ ariadne = { version = "0.4.1", optional = true } rmp-serde = "1.3.0" bincode = "1.3.3" flate2 = "1.0" -dlhn = "0.1" [features] default = ["types", "daletpack", "parsers", "daleth"] diff --git a/examples/bench.rs b/examples/bench.rs index 669be0c..2e97807 100644 --- a/examples/bench.rs +++ b/examples/bench.rs @@ -1,10 +1,8 @@ use dalet::{ - daletl::DlPage, daletpack::*, typed::{Hl, TNullArg, Tag::*}, }; use flate2::Compression; -use serde::{Deserialize, Serialize}; use std::io::Write; #[macro_export] @@ -51,21 +49,6 @@ fn compress_zlib(data: &[u8]) -> std::io::Result> { c.finish() } -fn dlhn_serialize(page: &DlPage) -> Vec { - let mut output = Vec::new(); - let mut serializer = dlhn::Serializer::new(&mut output); - page.serialize(&mut serializer).unwrap(); - - output -} - -fn dlhn_deserialize(output: &Vec) -> DlPage { - let mut reader = output.as_slice(); - let mut deserializer = dlhn::Deserializer::new(&mut reader); - - DlPage::deserialize(&mut deserializer).unwrap() -} - fn main() { let page = vec![ H("Heading 1".into(), Hl::One), @@ -117,7 +100,6 @@ fn main() { bench!("Markdown", include_str!("./bench.md").as_bytes().to_vec()); bench!("Daletpack", encode_no_compress(&dalet_page).unwrap()); - bench!("Dlhn", dlhn_serialize(&dalet_page)); bench!("Messagepack", rmp_serde::to_vec(&dalet_page).unwrap()); bench!("Bincode", bincode::serialize(&dalet_page).unwrap()); } diff --git a/src/daletl.rs b/src/daletl.rs index 726b6f2..43a4453 100644 --- a/src/daletl.rs +++ b/src/daletl.rs @@ -12,18 +12,18 @@ pub struct DlPage { pub struct DlTag { pub id: DlTid, pub body: DlBody, - pub argument: DlArgument, + pub argument: DlArg, } impl DlTag { #[inline] - pub fn new(id: DlTid, body: DlBody, argument: DlArgument) -> DlTag { + pub fn new(id: DlTid, body: DlBody, argument: DlArg) -> DlTag { DlTag { id, body, argument } } } #[inline] -pub fn dlt_new(id: DlTid, body: DlBody, argument: DlArgument) -> DlTag { +pub fn dlt_new(id: DlTid, body: DlBody, argument: DlArg) -> DlTag { DlTag::new(id, body, argument) } @@ -37,7 +37,7 @@ pub enum DlBody { #[derive(AutoFrom, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(untagged)] -pub enum DlArgument { +pub enum DlArg { Text(String), Number(u8), Null, diff --git a/src/daletpack/encode.rs b/src/daletpack/encode.rs index 6805a56..2120c7b 100644 --- a/src/daletpack/encode.rs +++ b/src/daletpack/encode.rs @@ -1,4 +1,4 @@ -use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid}; +use crate::daletl::{DlArg, DlBody, DlPage, DlTag}; use super::{utils, DaletPackError, TypeId}; @@ -28,7 +28,7 @@ fn write_str(bv: &mut Vec, string: &String) -> Result<(), DaletPackError> { } bv.extend_from_slice(string.as_bytes()); - bv.push(TypeId::TextEnd as u8); + bv.push(TypeId::EndOfBody as u8); Ok(()) } @@ -43,106 +43,46 @@ fn write_array(bv: &mut Vec, arr: &Vec) -> Result<(), DaletPackError> } if arr.len() != 1 { - bv.push(TypeId::TagsEnd as u8); + bv.push(TypeId::EndOfBody as u8); } Ok(()) } fn write_tag(bv: &mut Vec, tag: &DlTag) -> Result<(), DaletPackError> { - // TypeId and TagId if needed - match (&tag.body, &tag.argument) { - (DlBody::Text(_), DlArgument::Text(_)) => match &tag.id { - DlTid::Meta => bv.push(TypeId::Meta as u8), - _ => { - bv.push(TypeId::CompTextText as u8); - bv.push(tag.id as u8); - } - }, - (DlBody::Text(_), DlArgument::Number(_)) => { - bv.push(TypeId::CompTextNumber as u8); - bv.push(tag.id as u8); - } - (DlBody::Text(_), DlArgument::Null) => match &tag.id { - DlTid::El => bv.push(TypeId::ElText as u8), - DlTid::P => bv.push(TypeId::PText as u8), - DlTid::B => bv.push(TypeId::B as u8), - DlTid::I => bv.push(TypeId::I as u8), - DlTid::S => bv.push(TypeId::S as u8), - DlTid::Sup => bv.push(TypeId::Sup as u8), - DlTid::Sub => bv.push(TypeId::Sub as u8), - - _ => { - bv.push(TypeId::BodyText as u8); - bv.push(tag.id as u8); - } - }, - (DlBody::Tags(tags), DlArgument::Text(_)) => { - if tags.len() == 1 { - bv.push(TypeId::CompTagText as u8); + let type_id = match (&tag.body, &tag.argument) { + (DlBody::Text(_), DlArg::Text(_)) => TypeId::TextText, + (DlBody::Text(_), DlArg::Number(_)) => TypeId::TextNumber, + (DlBody::Text(_), DlArg::Null) => TypeId::TextBody, + (DlBody::Tags(vec), DlArg::Text(_)) => { + if vec.len() == 1 { + TypeId::TagText } else { - bv.push(TypeId::CompTagsText as u8); + TypeId::TagsText } - - bv.push(tag.id as u8); } - (DlBody::Tags(tags), DlArgument::Number(_)) => { - if tags.len() == 1 { - bv.push(TypeId::CompTagNumber as u8); + (DlBody::Tags(vec), DlArg::Number(_)) => { + if vec.len() == 1 { + TypeId::TagNumber } else { - bv.push(TypeId::CompTagsNumber as u8); + TypeId::TagsNumber } - - bv.push(tag.id as u8); } - (DlBody::Tags(tags), DlArgument::Null) => { - if tags.len() == 1 { - match &tag.id { - DlTid::El => bv.push(TypeId::ElTag as u8), - DlTid::P => bv.push(TypeId::PTag as u8), - _ => { - bv.push(TypeId::BodyTag as u8); - bv.push(tag.id as u8); - } - } + (DlBody::Tags(vec), DlArg::Null) => { + if vec.len() == 1 { + TypeId::TagBody } else { - match &tag.id { - DlTid::El => bv.push(TypeId::ElTags as u8), - DlTid::P => bv.push(TypeId::PTags as u8), - _ => { - bv.push(TypeId::BodyTags as u8); - bv.push(tag.id as u8); - } - } + TypeId::TagsBody } } - (DlBody::Null, DlArgument::Text(_)) => match &tag.id { - DlTid::Img => bv.push(TypeId::Img as u8), - DlTid::A => bv.push(TypeId::AText as u8), - - _ => { - bv.push(TypeId::ArgText as u8); - bv.push(tag.id as u8); - } - }, - (DlBody::Null, DlArgument::Number(_)) => match &tag.id { - DlTid::A => bv.push(TypeId::ANumber as u8), - _ => { - bv.push(TypeId::ArgNumber as u8); - bv.push(tag.id as u8); - } - }, - (DlBody::Null, DlArgument::Null) => match &tag.id { - DlTid::Br => bv.push(TypeId::Br as u8), - DlTid::Hr => bv.push(TypeId::Hr as u8), - - _ => { - bv.push(TypeId::Id as u8); - bv.push(tag.id as u8); - } - }, + (DlBody::Null, DlArg::Text(_)) => TypeId::TextArg, + (DlBody::Null, DlArg::Number(_)) => TypeId::NumberArg, + (DlBody::Null, DlArg::Null) => TypeId::JustId, }; + bv.push(type_id as u8); + bv.push(tag.id as u8); + write_tag_body(bv, &tag.body)?; write_tag_argument(bv, &tag.argument)?; @@ -159,11 +99,11 @@ fn write_tag_body(bv: &mut Vec, body: &DlBody) -> Result<(), DaletPackError> Ok(()) } -fn write_tag_argument(bv: &mut Vec, argument: &DlArgument) -> Result<(), DaletPackError> { +fn write_tag_argument(bv: &mut Vec, argument: &DlArg) -> Result<(), DaletPackError> { match argument { - DlArgument::Text(s) => write_str(bv, s)?, - DlArgument::Number(n) => bv.push(*n), - DlArgument::Null => {} + DlArg::Text(s) => write_str(bv, s)?, + DlArg::Number(n) => bv.push(*n), + DlArg::Null => {} }; Ok(()) diff --git a/src/daletpack/types.rs b/src/daletpack/types.rs index 74cefcf..34d4109 100644 --- a/src/daletpack/types.rs +++ b/src/daletpack/types.rs @@ -43,35 +43,22 @@ impl From> for DaletPackDecodeError { #[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive, Copy)] #[repr(u8)] pub enum TypeId { - TextEnd = 0x00, - TagsEnd, - BodyText = 0xa0, - BodyTag, - BodyTags, - ArgText = 0xb0, - ArgNumber, - CompTextText = 0xc0, - CompTagText, - CompTagsText, - CompTextNumber, - CompTagNumber, - CompTagsNumber, - Id = 0xd0, - ElText, - ElTag, - ElTags, - PText, - PTag, - PTags, - Br, - Hr, - Img, - B, - I, - ANumber, - AText, - S, - Sup, - Sub, - Meta, + EndOfBody, + + TextBody, + TagBody, + TagsBody, + + TextArg, + NumberArg, + + TextText, + TagText, + TagsText, + + TextNumber, + TagNumber, + TagsNumber, + + JustId, } diff --git a/src/traits/from_daletl.rs b/src/traits/from_daletl.rs index 5ede9fe..9ea606a 100644 --- a/src/traits/from_daletl.rs +++ b/src/traits/from_daletl.rs @@ -48,35 +48,35 @@ impl TryFrom for Tag { } } -impl TryFrom for Hl { +impl TryFrom for Hl { type Error = ConversionError; - fn try_from(value: DlArgument) -> Result { + fn try_from(value: DlArg) -> Result { match value { - DlArgument::Number(n) => n.try_into().map_err(|_| ConversionError), + DlArg::Number(n) => n.try_into().map_err(|_| ConversionError), _ => Err(ConversionError), } } } -impl TryFrom for AlignArg { +impl TryFrom for AlignArg { type Error = ConversionError; - fn try_from(value: DlArgument) -> Result { + fn try_from(value: DlArg) -> Result { match value { - DlArgument::Number(n) => n.try_into().map_err(|_| ConversionError), + DlArg::Number(n) => n.try_into().map_err(|_| ConversionError), _ => Err(ConversionError), } } } -impl TryFrom for TNullArg { +impl TryFrom for TNullArg { type Error = ConversionError; - fn try_from(value: DlArgument) -> Result { + fn try_from(value: DlArg) -> Result { match value { - DlArgument::Text(t) => Ok(TNullArg::Text(t)), - DlArgument::Null => Ok(TNullArg::Null), + DlArg::Text(t) => Ok(TNullArg::Text(t)), + DlArg::Null => Ok(TNullArg::Null), _ => Err(ConversionError), } } @@ -109,24 +109,24 @@ impl TryFrom for String { } } -impl TryFrom for NNArg { +impl TryFrom for NNArg { type Error = ConversionError; - fn try_from(value: DlArgument) -> Result { + fn try_from(value: DlArg) -> Result { match value { - DlArgument::Text(t) => Ok(t.into()), - DlArgument::Number(n) => Ok(n.into()), - DlArgument::Null => Err(ConversionError), + DlArg::Text(t) => Ok(t.into()), + DlArg::Number(n) => Ok(n.into()), + DlArg::Null => Err(ConversionError), } } } -impl TryFrom for String { +impl TryFrom for String { type Error = ConversionError; - fn try_from(value: DlArgument) -> Result { + fn try_from(value: DlArg) -> Result { match value { - DlArgument::Text(s) => Ok(s), + DlArg::Text(s) => Ok(s), _ => Err(ConversionError), } } diff --git a/src/traits/is_null_daletl.rs b/src/traits/is_null_daletl.rs index 5817afd..eab2797 100644 --- a/src/traits/is_null_daletl.rs +++ b/src/traits/is_null_daletl.rs @@ -1,4 +1,4 @@ -use crate::daletl::{DlArgument, DlBody, IsNull}; +use crate::daletl::{DlArg, DlBody, IsNull}; impl IsNull for DlBody { fn is_null(&self) -> bool { @@ -6,7 +6,7 @@ impl IsNull for DlBody { } } -impl IsNull for DlArgument { +impl IsNull for DlArg { fn is_null(&self) -> bool { matches!(self, Self::Null) } diff --git a/src/traits/to_daletl.rs b/src/traits/to_daletl.rs index 0733331..1d17365 100644 --- a/src/traits/to_daletl.rs +++ b/src/traits/to_daletl.rs @@ -1,7 +1,7 @@ use crate::{daletl::*, typed::*}; const NB: DlBody = DlBody::Null; -const NA: DlArgument = DlArgument::Null; +const NA: DlArg = DlArg::Null; impl From for DlTag { fn from(item: Tag) -> DlTag { @@ -41,8 +41,8 @@ impl From for DlTag { } } -impl From for DlArgument { - fn from(item: Hl) -> DlArgument { +impl From for DlArg { + fn from(item: Hl) -> DlArg { match item { Hl::One => NA, Hl::Two => 2u8.into(), @@ -54,8 +54,8 @@ impl From for DlArgument { } } -impl From for DlArgument { - fn from(item: AlignArg) -> DlArgument { +impl From for DlArg { + fn from(item: AlignArg) -> DlArg { match item { AlignArg::Start => NA, AlignArg::Center => 1u8.into(), @@ -64,8 +64,8 @@ impl From for DlArgument { } } -impl From for DlArgument { - fn from(item: TNullArg) -> DlArgument { +impl From for DlArg { + fn from(item: TNullArg) -> DlArg { match item { TNullArg::Text(s) => s.into(), TNullArg::Null => NA, @@ -83,8 +83,8 @@ impl From for DlBody { } } -impl From for DlArgument { - fn from(item: NNArg) -> DlArgument { +impl From for DlArg { + fn from(item: NNArg) -> DlArg { match item { NNArg::Number(v) => v.into(), NNArg::Text(v) => v.into(),