From fac56efca00c908031e18dc2bf5e68c7029dc467 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Sat, 3 Aug 2024 14:29:15 +0300 Subject: [PATCH] refactor: add type aliases --- src/daletpack/encode.rs | 4 +- src/typed.rs | 166 +++++++++++++++++++++------------------- tests/bench.rs | 8 +- 3 files changed, 92 insertions(+), 86 deletions(-) diff --git a/src/daletpack/encode.rs b/src/daletpack/encode.rs index af369ea..0d447f6 100644 --- a/src/daletpack/encode.rs +++ b/src/daletpack/encode.rs @@ -1,9 +1,9 @@ use crate::daletl::{Argument, Body, IsNull, Page, Tag, Tid}; -use super::{DaletPackError, TypeId}; +use super::{utils, DaletPackError, TypeId}; pub fn encode(page: &Page) -> Result, DaletPackError> { - Ok(zstd::bulk::compress(&encode_no_compress(page)?, 5) + Ok(utils::compress_zstd(&encode_no_compress(page)?) .map_err(|_| DaletPackError::ZstdCompressError)?) } diff --git a/src/typed.rs b/src/typed.rs index 57e99aa..f731028 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -11,35 +11,94 @@ pub type Page = Vec; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Tag { El(NNBody), - H(String, Hl), + H(TBody, Hl), P(NNBody), Br, Ul(Vec), Ol(Vec), - Row(Vec, AlignArgument), - Link(Body, String), - Navlink(Body, String), - Btn(Body, String), - Navbtn(Body, String), - Img(String), + Row(Vec, AlignArg), + Link(Body, TArg), + Navlink(Body, TArg), + Btn(Body, TArg), + Navbtn(Body, TArg), + Img(TArg), Table(Vec), Tcol(Vec), Tpcol(Vec), Hr, - B(String), - I(String), + B(TBody), + I(TBody), Bq(NNBody), Footlnk(NNArg), - Footn(String, NNArg), + Footn(TBody, NNArg), A(NNArg), - S(String), - Sup(String), - Sub(String), + S(TBody), + Sup(TBody), + Sub(TBody), Disc(NNBody), - Bl(NNBody, AlignArgument), + Bl(NNBody, AlignArg), Carousel(Vec), - Code(String, TNArgument), - Pre(String), + Code(TBody, TNArg), + Pre(TBody), +} + +#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] +pub enum Body { + Text(String), + Tags(Vec), + Null, +} + +#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] +pub enum NNBody { + Text(String), + Tags(Vec), +} + +/// Text body +pub type TBody = String; + +#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] +pub enum Arg { + Text(String), + Number(u8), + Null, +} + +#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] +pub enum TNArg { + Text(String), + Null, +} + +/// Text argument +pub type TArg = String; + +#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] +/// Not null argument +pub enum NNArg { + Text(String), + Number(u8), +} + +#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] +#[repr(u8)] +pub enum AlignArg { + Start, + Center, + End, +} + +#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] +#[repr(u8)] +/// Heading level +pub enum Hl { + One = 1, + Two, + Three, + Four, + Five, + Six, } impl From for daletl::Tag { @@ -73,24 +132,12 @@ impl From for daletl::Tag { Tag::Disc(b) => t_new(Tid::Disc, b.into(), NA), Tag::Bl(b, a) => t_new(Tid::Bl, b.into(), a.into()), Tag::Carousel(b) => t_new(Tid::Carousel, b.into(), NA), - Tag::Code(s, a) => t_new(Tid::Code, s.into(), a.into()), - Tag::Pre(s) => t_new(Tid::Pre, s.into(), NA), + Tag::Code(b, a) => t_new(Tid::Code, b.into(), a.into()), + Tag::Pre(b) => t_new(Tid::Pre, b.into(), NA), } } } -#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] -#[repr(u8)] -/// Heading level -pub enum Hl { - One = 1, - Two, - Three, - Four, - Five, - Six, -} - impl From for daletl::Argument { fn from(item: Hl) -> daletl::Argument { match item { @@ -104,46 +151,25 @@ impl From for daletl::Argument { } } -#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] -#[repr(u8)] -pub enum AlignArgument { - Start, - Center, - End, -} - -impl From for daletl::Argument { - fn from(item: AlignArgument) -> daletl::Argument { +impl From for daletl::Argument { + fn from(item: AlignArg) -> daletl::Argument { match item { - AlignArgument::Start => NA, - AlignArgument::Center => 1u8.into(), - AlignArgument::End => 2u8.into(), + AlignArg::Start => NA, + AlignArg::Center => 1u8.into(), + AlignArg::End => 2u8.into(), } } } -#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] -pub enum TNArgument { - Text(String), - Null, -} - -impl From for daletl::Argument { - fn from(item: TNArgument) -> daletl::Argument { +impl From for daletl::Argument { + fn from(item: TNArg) -> daletl::Argument { match item { - TNArgument::Text(s) => s.into(), - TNArgument::Null => NA, + TNArg::Text(s) => s.into(), + TNArg::Null => NA, } } } -#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] -pub enum Body { - Text(String), - Tags(Vec), - Null, -} - impl From for daletl::Body { fn from(item: Body) -> daletl::Body { match item { @@ -154,13 +180,6 @@ impl From for daletl::Body { } } -#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] -pub enum Arg { - Text(String), - Number(u8), - Null, -} - impl From for daletl::Argument { fn from(item: Arg) -> daletl::Argument { match item { @@ -171,13 +190,6 @@ impl From for daletl::Argument { } } -#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] -/// Not null argument -pub enum NNArg { - Text(String), - Number(u8), -} - impl From for daletl::Argument { fn from(item: NNArg) -> daletl::Argument { match item { @@ -187,12 +199,6 @@ impl From for daletl::Argument { } } -#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] -pub enum NNBody { - Text(String), - Tags(Vec), -} - impl From for daletl::Body { fn from(item: NNBody) -> daletl::Body { match item { diff --git a/tests/bench.rs b/tests/bench.rs index 5086fef..17908f3 100644 --- a/tests/bench.rs +++ b/tests/bench.rs @@ -2,7 +2,7 @@ use dalet::{ daletl::ToDaletlPage, daletpack::*, typed::{ - Body, Hl, NNBody, Page, TNArgument, + Body, Hl, NNBody, Page, TNArg, Tag::{self, *}, }, }; @@ -38,7 +38,7 @@ fn bench() { let page: Page = vec![ H("I am heading".into(), Hl::One), H("Heading 2".into(), Hl::Two), - El(vec![ + P(vec![ El("Some ".into()), B("bold".into()), I("italic".into()), @@ -46,7 +46,7 @@ fn bench() { ] .into()), Br, - Code("Hello world".into(), TNArgument::Null), + Code("Hello world".into(), TNArg::Null), Br, Ul(vec![ El("abc".into()), @@ -58,7 +58,7 @@ fn bench() { El("xyz".into()), ]), Br, - El(vec![ + P(vec![ El("Lorem ipsum ".into()), Link( vec![Img("https://my-picture".into())].into(),