refactor: add type aliases

This commit is contained in:
Artemy Egorov 2024-08-03 14:29:15 +03:00
parent 8b23712619
commit fac56efca0
3 changed files with 92 additions and 86 deletions

View file

@ -1,9 +1,9 @@
use crate::daletl::{Argument, Body, IsNull, Page, Tag, Tid}; use crate::daletl::{Argument, Body, IsNull, Page, Tag, Tid};
use super::{DaletPackError, TypeId}; use super::{utils, DaletPackError, TypeId};
pub fn encode(page: &Page) -> Result<Vec<u8>, DaletPackError> { pub fn encode(page: &Page) -> Result<Vec<u8>, DaletPackError> {
Ok(zstd::bulk::compress(&encode_no_compress(page)?, 5) Ok(utils::compress_zstd(&encode_no_compress(page)?)
.map_err(|_| DaletPackError::ZstdCompressError)?) .map_err(|_| DaletPackError::ZstdCompressError)?)
} }

View file

@ -11,35 +11,94 @@ pub type Page = Vec<Tag>;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Tag { pub enum Tag {
El(NNBody), El(NNBody),
H(String, Hl), H(TBody, Hl),
P(NNBody), P(NNBody),
Br, Br,
Ul(Vec<Tag>), Ul(Vec<Tag>),
Ol(Vec<Tag>), Ol(Vec<Tag>),
Row(Vec<Tag>, AlignArgument), Row(Vec<Tag>, AlignArg),
Link(Body, String), Link(Body, TArg),
Navlink(Body, String), Navlink(Body, TArg),
Btn(Body, String), Btn(Body, TArg),
Navbtn(Body, String), Navbtn(Body, TArg),
Img(String), Img(TArg),
Table(Vec<Tag>), Table(Vec<Tag>),
Tcol(Vec<Tag>), Tcol(Vec<Tag>),
Tpcol(Vec<Tag>), Tpcol(Vec<Tag>),
Hr, Hr,
B(String), B(TBody),
I(String), I(TBody),
Bq(NNBody), Bq(NNBody),
Footlnk(NNArg), Footlnk(NNArg),
Footn(String, NNArg), Footn(TBody, NNArg),
A(NNArg), A(NNArg),
S(String), S(TBody),
Sup(String), Sup(TBody),
Sub(String), Sub(TBody),
Disc(NNBody), Disc(NNBody),
Bl(NNBody, AlignArgument), Bl(NNBody, AlignArg),
Carousel(Vec<Tag>), Carousel(Vec<Tag>),
Code(String, TNArgument), Code(TBody, TNArg),
Pre(String), Pre(TBody),
}
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
pub enum Body {
Text(String),
Tags(Vec<Tag>),
Null,
}
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
pub enum NNBody {
Text(String),
Tags(Vec<Tag>),
}
/// 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<Tag> for daletl::Tag { impl From<Tag> for daletl::Tag {
@ -73,24 +132,12 @@ impl From<Tag> for daletl::Tag {
Tag::Disc(b) => t_new(Tid::Disc, b.into(), NA), Tag::Disc(b) => t_new(Tid::Disc, b.into(), NA),
Tag::Bl(b, a) => t_new(Tid::Bl, b.into(), a.into()), Tag::Bl(b, a) => t_new(Tid::Bl, b.into(), a.into()),
Tag::Carousel(b) => t_new(Tid::Carousel, b.into(), NA), Tag::Carousel(b) => t_new(Tid::Carousel, b.into(), NA),
Tag::Code(s, a) => t_new(Tid::Code, s.into(), a.into()), Tag::Code(b, a) => t_new(Tid::Code, b.into(), a.into()),
Tag::Pre(s) => t_new(Tid::Pre, s.into(), NA), 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<Hl> for daletl::Argument { impl From<Hl> for daletl::Argument {
fn from(item: Hl) -> daletl::Argument { fn from(item: Hl) -> daletl::Argument {
match item { match item {
@ -104,46 +151,25 @@ impl From<Hl> for daletl::Argument {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] impl From<AlignArg> for daletl::Argument {
#[repr(u8)] fn from(item: AlignArg) -> daletl::Argument {
pub enum AlignArgument {
Start,
Center,
End,
}
impl From<AlignArgument> for daletl::Argument {
fn from(item: AlignArgument) -> daletl::Argument {
match item { match item {
AlignArgument::Start => NA, AlignArg::Start => NA,
AlignArgument::Center => 1u8.into(), AlignArg::Center => 1u8.into(),
AlignArgument::End => 2u8.into(), AlignArg::End => 2u8.into(),
} }
} }
} }
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] impl From<TNArg> for daletl::Argument {
pub enum TNArgument { fn from(item: TNArg) -> daletl::Argument {
Text(String),
Null,
}
impl From<TNArgument> for daletl::Argument {
fn from(item: TNArgument) -> daletl::Argument {
match item { match item {
TNArgument::Text(s) => s.into(), TNArg::Text(s) => s.into(),
TNArgument::Null => NA, TNArg::Null => NA,
} }
} }
} }
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
pub enum Body {
Text(String),
Tags(Vec<Tag>),
Null,
}
impl From<Body> for daletl::Body { impl From<Body> for daletl::Body {
fn from(item: Body) -> daletl::Body { fn from(item: Body) -> daletl::Body {
match item { match item {
@ -154,13 +180,6 @@ impl From<Body> for daletl::Body {
} }
} }
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
pub enum Arg {
Text(String),
Number(u8),
Null,
}
impl From<Arg> for daletl::Argument { impl From<Arg> for daletl::Argument {
fn from(item: Arg) -> daletl::Argument { fn from(item: Arg) -> daletl::Argument {
match item { match item {
@ -171,13 +190,6 @@ impl From<Arg> for daletl::Argument {
} }
} }
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
/// Not null argument
pub enum NNArg {
Text(String),
Number(u8),
}
impl From<NNArg> for daletl::Argument { impl From<NNArg> for daletl::Argument {
fn from(item: NNArg) -> daletl::Argument { fn from(item: NNArg) -> daletl::Argument {
match item { match item {
@ -187,12 +199,6 @@ impl From<NNArg> for daletl::Argument {
} }
} }
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
pub enum NNBody {
Text(String),
Tags(Vec<Tag>),
}
impl From<NNBody> for daletl::Body { impl From<NNBody> for daletl::Body {
fn from(item: NNBody) -> daletl::Body { fn from(item: NNBody) -> daletl::Body {
match item { match item {

View file

@ -2,7 +2,7 @@ use dalet::{
daletl::ToDaletlPage, daletl::ToDaletlPage,
daletpack::*, daletpack::*,
typed::{ typed::{
Body, Hl, NNBody, Page, TNArgument, Body, Hl, NNBody, Page, TNArg,
Tag::{self, *}, Tag::{self, *},
}, },
}; };
@ -38,7 +38,7 @@ fn bench() {
let page: Page = vec![ let page: Page = vec![
H("I am heading".into(), Hl::One), H("I am heading".into(), Hl::One),
H("Heading 2".into(), Hl::Two), H("Heading 2".into(), Hl::Two),
El(vec![ P(vec![
El("Some ".into()), El("Some ".into()),
B("bold".into()), B("bold".into()),
I("italic".into()), I("italic".into()),
@ -46,7 +46,7 @@ fn bench() {
] ]
.into()), .into()),
Br, Br,
Code("Hello world".into(), TNArgument::Null), Code("Hello world".into(), TNArg::Null),
Br, Br,
Ul(vec![ Ul(vec![
El("abc".into()), El("abc".into()),
@ -58,7 +58,7 @@ fn bench() {
El("xyz".into()), El("xyz".into()),
]), ]),
Br, Br,
El(vec![ P(vec![
El("Lorem ipsum ".into()), El("Lorem ipsum ".into()),
Link( Link(
vec![Img("https://my-picture".into())].into(), vec![Img("https://my-picture".into())].into(),