From 1611820661cb6222ba05451dbfe27bf2515c1ee7 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Thu, 1 Aug 2024 15:51:59 +0300 Subject: [PATCH] feat: rust create abstractions, tag enum, fix serde; update daletpack, daletl specs --- libs/rust/Cargo.lock | 141 +++++++++++++++++++++++++++++++++- libs/rust/Cargo.toml | 5 +- libs/rust/src/abstractions.rs | 106 +++++++++++++++++++++++++ libs/rust/src/daletl.rs | 68 ++++++++++++++++ libs/rust/src/lib.rs | 5 +- libs/rust/src/main.rs | 8 +- libs/rust/src/types.rs | 54 ------------- specification/daletpack.md | 25 +++--- specification/tags.md | 36 ++++----- 9 files changed, 355 insertions(+), 93 deletions(-) create mode 100644 libs/rust/src/abstractions.rs create mode 100644 libs/rust/src/daletl.rs delete mode 100644 libs/rust/src/types.rs diff --git a/libs/rust/Cargo.lock b/libs/rust/Cargo.lock index 11dd28d..fbf98dd 100644 --- a/libs/rust/Cargo.lock +++ b/libs/rust/Cargo.lock @@ -3,10 +3,89 @@ version = 3 [[package]] -name = "dalet" -version = "1.0.0-pre2" +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "dalet" +version = "1.0.0-pre4" +dependencies = [ + "bitvec", + "num_enum", "serde", + "serde_repr", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "num_enum" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e613fc340b2220f734a8595782c551f1250e969d87d3be1ae0579e8d4065179" +dependencies = [ + "num_enum_derive", +] + +[[package]] +name = "num_enum_derive" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro-crate" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +dependencies = [ + "toml_edit", ] [[package]] @@ -27,6 +106,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "serde" version = "1.0.204" @@ -47,6 +132,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_repr" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "syn" version = "2.0.72" @@ -58,8 +154,49 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" + +[[package]] +name = "toml_edit" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] diff --git a/libs/rust/Cargo.toml b/libs/rust/Cargo.toml index d81f72c..48b0cfb 100644 --- a/libs/rust/Cargo.toml +++ b/libs/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dalet" -version = "1.0.0-pre3" +version = "1.0.0-pre4" edition = "2021" authors = ["artegoser"] license = "MIT" @@ -12,7 +12,10 @@ keywords = ["dalet"] categories = ["compression", "compilers", "encoding"] [dependencies] +bitvec = "1.0.1" +num_enum = "0.7.3" serde = { version = "1.0", features = ["derive"] } +serde_repr = "0.1" [features] default = ["types"] diff --git a/libs/rust/src/abstractions.rs b/libs/rust/src/abstractions.rs new file mode 100644 index 0000000..aea42f1 --- /dev/null +++ b/libs/rust/src/abstractions.rs @@ -0,0 +1,106 @@ +use num_enum::TryFromPrimitive; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Tag { + El(NotNullBody), + H(String, HeadingArgument), + P(NotNullBody), + Br, + Ul(Vec), + Ol(Vec), + Row(Vec, AlignArgument), + Link(Body, String), + Navlink(Body, String), + Btn(Body, String), + NavBtn(Body, String), + Img(String), + Table(Vec), + Tcol(Vec), + Tpcol(Vec), + Hr, + B(String), + I(String), + Bq(NotNullBody), + Footlnk(StringOrNumberArgument), + Footn(String, StringOrNumberArgument), + A(StringOrNumberArgument), + S(String), + Sup(String), + Sub(String), + Disc(NotNullBody), + Bl(NotNullBody, AlignArgument), + Carousel(Vec), +} + +#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] +#[repr(u8)] +pub enum HeadingArgument { + One, + Two, + Three, + Four, + Five, + Six, + Seven, + Eight, + Nine, + Ten, +} + +#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] +#[repr(u8)] +pub enum AlignArgument { + Start, + Center, + End, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum StringOrNumberArgument { + String(String), + Number(u8), +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Body { + Text(String), + Tags(Vec), + Null, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Argument { + Text(String), + Number(i8), + Null, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum NotNullArgument { + Text(String), + Number(i8), +} + +impl NotNullArgument { + pub fn to_argument(self) -> Argument { + match self { + NotNullArgument::Number(v) => Argument::Number(v), + NotNullArgument::Text(v) => Argument::Text(v), + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum NotNullBody { + Text(String), + Tags(Vec), +} + +impl NotNullBody { + pub fn to_body(self) -> Body { + match self { + NotNullBody::Text(v) => Body::Text(v), + NotNullBody::Tags(v) => Body::Tags(v), + } + } +} diff --git a/libs/rust/src/daletl.rs b/libs/rust/src/daletl.rs new file mode 100644 index 0000000..2e681f6 --- /dev/null +++ b/libs/rust/src/daletl.rs @@ -0,0 +1,68 @@ +use serde::{Deserialize, Serialize}; +use serde_repr::{Deserialize_repr, Serialize_repr}; + +use num_enum::TryFromPrimitive; + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +pub struct Tag { + id: Tid, + body: Body, + argument: Argument, +} + +impl Tag { + #[inline] + pub fn new(id: Tid, body: Body, argument: Argument) -> Tag { + Tag { id, body, argument } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[serde(untagged)] +pub enum Body { + Text(String), + Tags(Vec), + Null, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[serde(untagged)] +pub enum Argument { + Text(String), + Number(i8), + Null, +} + +#[derive(Serialize_repr, Deserialize_repr, Debug, Clone, PartialEq, Eq, TryFromPrimitive)] +#[repr(u8)] +/// Tag Id +pub enum Tid { + El, + H, + P, + Br, + Ul, + Ol, + Row, + Link, + Navlink, + Btn, + Navbtn, + Img, + Table, + Tcol, + Tpcol, + Hr, + B, + I, + Bq, + Footlnk, + Footn, + A, + S, + Sup, + Sub, + Disc, + Bl, + Carousel, +} diff --git a/libs/rust/src/lib.rs b/libs/rust/src/lib.rs index c627ab6..1b0a58c 100644 --- a/libs/rust/src/lib.rs +++ b/libs/rust/src/lib.rs @@ -1,4 +1,5 @@ -mod types; +#[cfg(feature = "types")] +pub mod daletl; #[cfg(feature = "types")] -pub use types::*; +pub mod abstractions; diff --git a/libs/rust/src/main.rs b/libs/rust/src/main.rs index 933c457..a97cdb5 100644 --- a/libs/rust/src/main.rs +++ b/libs/rust/src/main.rs @@ -1,6 +1,10 @@ -use dalet::{Argument, Body, Tag}; +use dalet::daletl::{Argument, Body, Tag, Tid}; fn main() { - let _ = Tag::new(1, Body::Text("I am Heading".to_string()), Argument::Null); + let _ = Tag::new( + Tid::H, + Body::Text("I am Heading".to_string()), + Argument::Null, + ); println!("Hello, world!"); } diff --git a/libs/rust/src/types.rs b/libs/rust/src/types.rs deleted file mode 100644 index 76a88ce..0000000 --- a/libs/rust/src/types.rs +++ /dev/null @@ -1,54 +0,0 @@ -use serde::Serialize; - -#[derive(Serialize, Debug, Clone)] -pub struct Tag { - id: i8, - body: Body, - argument: Argument, -} - -impl Tag { - pub fn new(id: i8, body: Body, argument: Argument) -> Tag { - Tag { id, body, argument } - } -} - -#[derive(Debug, Clone)] -pub enum Body { - Text(String), - Tags(Vec), - Null, -} - -impl Serialize for Body { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - match *self { - Body::Text(ref text) => serializer.serialize_str(text), - Body::Tags(ref tag) => tag.serialize(serializer), - Body::Null => serializer.serialize_none(), - } - } -} - -#[derive(Debug, Clone)] -pub enum Argument { - Text(String), - Number(i8), - Null, -} - -impl Serialize for Argument { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - match *self { - Argument::Text(ref text) => serializer.serialize_str(text), - Argument::Number(number) => serializer.serialize_i8(number), - Argument::Null => serializer.serialize_none(), - } - } -} diff --git a/specification/daletpack.md b/specification/daletpack.md index 32f02d6..9b306d9 100644 --- a/specification/daletpack.md +++ b/specification/daletpack.md @@ -10,8 +10,7 @@ Mime type: `application/dalet-pack` ## Types -- **Null** -- **Integer** +- **Integer** (2) - **String** (5) - **Tag array** - **Tags** (4) @@ -22,7 +21,7 @@ Mime type: `application/dalet-pack` ## Limitations -- a value of integer (4 bits) must be between 0 and 15 +- a value of integer must be between 0 and 255 - maximum byte size of a String object is (2^32) - string must be encoded in UTF-8 - maximum number of elements of a tag array object is (2^32) @@ -33,8 +32,8 @@ Mime type: `application/dalet-pack` | name | id | id-bits | | ------------------------ | --- | ------- | -| null | 0 | 0000 | -| int | 1 | 0001 | +| int 4 | 0 | 0000 | +| int 8 | 1 | 0001 | | str 3 | 2 | 0010 | | str 4 | 3 | 0011 | | str 8 | 4 | 0100 | @@ -64,22 +63,20 @@ variable number of objects stored in DaletPack format: +~~~~~~~~~~~~~~~~~+ | | +~~~~~~~~~~~~~~~~~+ -``` -### Null format - -```txt -+--------+ -| 0000 | -+--------+ +X - unknown bit ``` ### Integer format ```txt +--------+------+ -| 0001 | XXXX | +| 0000 | XXXX | +--------+------+ + ++--------+----------+ +| 0001 | XXXXXXXX | ++--------+----------+ ``` ### String format @@ -88,7 +85,7 @@ variable number of objects stored in DaletPack format: str 3 (up to 8 bytes): +--------+-----+=========+ | 0010 | XXX | utf-8 | -+--------+-----+========+ ++--------+-----+=========+ str 4 (up to 16 bytes): +--------+------+=========+ diff --git a/specification/tags.md b/specification/tags.md index 6c63982..d67b56f 100644 --- a/specification/tags.md +++ b/specification/tags.md @@ -117,15 +117,15 @@ ol: { ## 6. Row -| Property | Description | -| -------- | ---------------------- | -| name | row | -| id | 6 | -| body | tags | -| argument | (optional); start, end | - -Splits the text into rows. The alignment argument specifies how the items inside the row are aligned. By default it is aligned to the center. +| Property | Description | +| -------- | ---------------------------- | +| name | row | +| id | 6 | +| body | tags | +| argument | optional; start, center, end | +Splits the text into rows. The alignment argument specifies how the items inside the row are aligned. By default it is aligned to the start. +m **Daleth example**: ```txt @@ -151,7 +151,7 @@ end -> 1 | -------- | ----------- | | name | link | | id | 7 | -| body | text, tags | +| body | any | | argument | string | Link to other sites. On click the link opens in new tab. @@ -168,7 +168,7 @@ link[https://example.com]: I am Link | -------- | ----------- | | name | navlink | | id | 8 | -| body | text, tags | +| body | any | | argument | string | Link to the same site. On click the link opens in current tab. @@ -185,7 +185,7 @@ navlink[/specification]: I am Navlink | -------- | ----------- | | name | btn | | id | 9 | -| body | text,tags | +| body | any | | argument | string | Same as link, but with button style. @@ -202,7 +202,7 @@ btn[https://example.com]: I am Button | -------- | ----------- | | name | navbtn | | id | 9 | -| body | text,tags | +| body | any | | argument | string | Same as navlink, but with button style. @@ -479,12 +479,12 @@ disc[Click to expand]: I am Disclosure ## 26. Block -| Property | Description | -| -------- | ----------- | -| name | bl | -| id | 26 | -| body | text, tags | -| argument | start, end | +| Property | Description | +| -------- | ----------------------------- | +| name | bl | +| id | 26 | +| body | text, tags | +| argument | optional; start, center, end | Creates block element. Can be used for aligning. By default aligned to center.