refactor: rename some types

This commit is contained in:
Artemy Egorov 2024-08-02 22:35:49 +03:00
parent a2cb481d29
commit ef1688b3be
4 changed files with 129 additions and 137 deletions

View file

@ -17,6 +17,7 @@ impl Tag {
} }
} }
#[inline]
pub fn t_new(id: Tid, body: Body, argument: Argument) -> Tag { pub fn t_new(id: Tid, body: Body, argument: Argument) -> Tag {
Tag::new(id, body, argument) Tag::new(id, body, argument)
} }
@ -29,6 +30,14 @@ pub enum Body {
Null, Null,
} }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(untagged)]
pub enum Argument {
Text(String),
Number(u8),
Null,
}
impl IsNull for Body { impl IsNull for Body {
fn is_null(&self) -> bool { fn is_null(&self) -> bool {
match self { match self {
@ -38,14 +47,6 @@ impl IsNull for Body {
} }
} }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(untagged)]
pub enum Argument {
Text(String),
Number(u8),
Null,
}
impl IsNull for Argument { impl IsNull for Argument {
fn is_null(&self) -> bool { fn is_null(&self) -> bool {
match self { match self {
@ -96,17 +97,21 @@ pub trait IsNull {
} }
pub trait ToDaletl { pub trait ToDaletl {
fn to_daletl(self) -> Vec<Tag>; /// Convert to daletl root
fn to_dl(self) -> Vec<Tag>;
} }
pub trait ToDaletlTag { pub trait ToDaletlTag {
fn to_daletl_tag(self) -> Tag; /// Convert to daletl tag
fn to_dl_tag(self) -> Tag;
} }
pub trait ToDaletlBody { pub trait ToDaletlBody {
fn to_daletl_body(self) -> Body; /// Convert to daletl body
fn to_dl_body(self) -> Body;
} }
pub trait ToDaletlArgument { pub trait ToDaletlArgument {
fn to_daletl_argument(self) -> Argument; /// Convert to daletl arg
fn to_dl_arg(self) -> Argument;
} }

View file

@ -1,4 +1,4 @@
use crate::typed::{Body, HeadingLevel, NotNullBody, Tag}; use crate::typed::{Body, Hl, NNBody, Tag};
#[derive(Debug)] #[derive(Debug)]
pub enum GemTextParseError { pub enum GemTextParseError {
@ -38,20 +38,20 @@ pub fn parse_gemtext(s: String) -> Result<Vec<Tag>, GemTextParseError> {
}; };
} else if line.starts_with("# ") { } else if line.starts_with("# ") {
let body = line.split_off(2); let body = line.split_off(2);
page.push(Tag::H(body.trim().to_owned(), HeadingLevel::One)); page.push(Tag::H(body.trim().to_owned(), Hl::One));
} else if line.starts_with("## ") { } else if line.starts_with("## ") {
let body = line.split_off(3); let body = line.split_off(3);
page.push(Tag::H(body.trim().to_owned(), HeadingLevel::Two)); page.push(Tag::H(body.trim().to_owned(), Hl::Two));
} else if line.starts_with("### ") { } else if line.starts_with("### ") {
let body = line.split_off(4); let body = line.split_off(4);
page.push(Tag::H(body.trim().to_owned(), HeadingLevel::Three)); page.push(Tag::H(body.trim().to_owned(), Hl::Three));
} else if line.starts_with("* ") { } else if line.starts_with("* ") {
list_before = true; list_before = true;
let body = line.split_off(2); let body = line.split_off(2);
list.push(Tag::El(NotNullBody::Text(body))); list.push(Tag::El(NNBody::Text(body)));
} else if line.starts_with("> ") { } else if line.starts_with("> ") {
let body = line.split_off(2); let body = line.split_off(2);
page.push(Tag::Bq(NotNullBody::Text(body))); page.push(Tag::Bq(NNBody::Text(body)));
} else if line.starts_with("```") { } else if line.starts_with("```") {
if preformatted { if preformatted {
page.push(Tag::Pre(preformatted_text.clone())); page.push(Tag::Pre(preformatted_text.clone()));
@ -60,7 +60,7 @@ pub fn parse_gemtext(s: String) -> Result<Vec<Tag>, GemTextParseError> {
preformatted = !preformatted; preformatted = !preformatted;
} else if !line.is_empty() { } else if !line.is_empty() {
page.push(Tag::P(NotNullBody::Text(line))); page.push(Tag::P(NNBody::Text(line)));
} }
} }

View file

@ -7,9 +7,9 @@ const NA: daletl::Argument = daletl::Argument::Null;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Tag { pub enum Tag {
El(NotNullBody), El(NNBody),
H(String, HeadingLevel), H(String, Hl),
P(NotNullBody), P(NNBody),
Br, Br,
Ul(Vec<Tag>), Ul(Vec<Tag>),
Ol(Vec<Tag>), Ol(Vec<Tag>),
@ -25,60 +25,61 @@ pub enum Tag {
Hr, Hr,
B(String), B(String),
I(String), I(String),
Bq(NotNullBody), Bq(NNBody),
Footlnk(TextOrNumberArgument), Footlnk(NNArg),
Footn(String, TextOrNumberArgument), Footn(String, NNArg),
A(TextOrNumberArgument), A(NNArg),
S(String), S(String),
Sup(String), Sup(String),
Sub(String), Sub(String),
Disc(NotNullBody), Disc(NNBody),
Bl(NotNullBody, AlignArgument), Bl(NNBody, AlignArgument),
Carousel(Vec<Tag>), Carousel(Vec<Tag>),
Code(String, TextOrNullArgument), Code(String, TNArgument),
Pre(String), Pre(String),
} }
impl ToDaletlTag for Tag { impl ToDaletlTag for Tag {
fn to_daletl_tag(self) -> daletl::Tag { fn to_dl_tag(self) -> daletl::Tag {
match self { match self {
Tag::El(b) => t_new(Tid::El, b.to_daletl_body(), NA), Tag::El(b) => t_new(Tid::El, b.to_dl_body(), NA),
Tag::H(b, a) => t_new(Tid::H, b.to_daletl_body(), a.to_daletl_argument()), Tag::H(b, a) => t_new(Tid::H, b.to_dl_body(), a.to_dl_arg()),
Tag::P(b) => t_new(Tid::P, b.to_daletl_body(), NA), Tag::P(b) => t_new(Tid::P, b.to_dl_body(), NA),
Tag::Br => t_new(Tid::Br, NB, NA), Tag::Br => t_new(Tid::Br, NB, NA),
Tag::Ul(b) => t_new(Tid::Ul, b.to_daletl_body(), NA), Tag::Ul(b) => t_new(Tid::Ul, b.to_dl_body(), NA),
Tag::Ol(b) => t_new(Tid::Ol, b.to_daletl_body(), NA), Tag::Ol(b) => t_new(Tid::Ol, b.to_dl_body(), NA),
Tag::Row(b, a) => t_new(Tid::Row, b.to_daletl_body(), a.to_daletl_argument()), Tag::Row(b, a) => t_new(Tid::Row, b.to_dl_body(), a.to_dl_arg()),
Tag::Link(b, a) => t_new(Tid::Link, b.to_daletl_body(), a.to_daletl_argument()), Tag::Link(b, a) => t_new(Tid::Link, b.to_dl_body(), a.to_dl_arg()),
Tag::Navlink(b, a) => t_new(Tid::Navlink, b.to_daletl_body(), a.to_daletl_argument()), Tag::Navlink(b, a) => t_new(Tid::Navlink, b.to_dl_body(), a.to_dl_arg()),
Tag::Btn(b, a) => t_new(Tid::Btn, b.to_daletl_body(), a.to_daletl_argument()), Tag::Btn(b, a) => t_new(Tid::Btn, b.to_dl_body(), a.to_dl_arg()),
Tag::Navbtn(b, a) => t_new(Tid::Navbtn, b.to_daletl_body(), a.to_daletl_argument()), Tag::Navbtn(b, a) => t_new(Tid::Navbtn, b.to_dl_body(), a.to_dl_arg()),
Tag::Img(a) => t_new(Tid::Img, NB, a.to_daletl_argument()), Tag::Img(a) => t_new(Tid::Img, NB, a.to_dl_arg()),
Tag::Table(b) => t_new(Tid::Table, b.to_daletl_body(), NA), Tag::Table(b) => t_new(Tid::Table, b.to_dl_body(), NA),
Tag::Tcol(b) => t_new(Tid::Tcol, b.to_daletl_body(), NA), Tag::Tcol(b) => t_new(Tid::Tcol, b.to_dl_body(), NA),
Tag::Tpcol(b) => t_new(Tid::Tpcol, b.to_daletl_body(), NA), Tag::Tpcol(b) => t_new(Tid::Tpcol, b.to_dl_body(), NA),
Tag::Hr => t_new(Tid::Hr, NB, NA), Tag::Hr => t_new(Tid::Hr, NB, NA),
Tag::B(b) => t_new(Tid::B, b.to_daletl_body(), NA), Tag::B(b) => t_new(Tid::B, b.to_dl_body(), NA),
Tag::I(b) => t_new(Tid::I, b.to_daletl_body(), NA), Tag::I(b) => t_new(Tid::I, b.to_dl_body(), NA),
Tag::Bq(b) => t_new(Tid::Bq, b.to_daletl_body(), NA), Tag::Bq(b) => t_new(Tid::Bq, b.to_dl_body(), NA),
Tag::Footlnk(a) => t_new(Tid::Footlnk, NB, a.to_daletl_argument()), Tag::Footlnk(a) => t_new(Tid::Footlnk, NB, a.to_dl_arg()),
Tag::Footn(b, a) => t_new(Tid::Footn, b.to_daletl_body(), a.to_daletl_argument()), Tag::Footn(b, a) => t_new(Tid::Footn, b.to_dl_body(), a.to_dl_arg()),
Tag::A(a) => t_new(Tid::A, NB, a.to_daletl_argument()), Tag::A(a) => t_new(Tid::A, NB, a.to_dl_arg()),
Tag::S(b) => t_new(Tid::S, b.to_daletl_body(), NA), Tag::S(b) => t_new(Tid::S, b.to_dl_body(), NA),
Tag::Sup(b) => t_new(Tid::Sup, b.to_daletl_body(), NA), Tag::Sup(b) => t_new(Tid::Sup, b.to_dl_body(), NA),
Tag::Sub(b) => t_new(Tid::Sub, b.to_daletl_body(), NA), Tag::Sub(b) => t_new(Tid::Sub, b.to_dl_body(), NA),
Tag::Disc(b) => t_new(Tid::Disc, b.to_daletl_body(), NA), Tag::Disc(b) => t_new(Tid::Disc, b.to_dl_body(), NA),
Tag::Bl(b, a) => t_new(Tid::Bl, b.to_daletl_body(), a.to_daletl_argument()), Tag::Bl(b, a) => t_new(Tid::Bl, b.to_dl_body(), a.to_dl_arg()),
Tag::Carousel(b) => t_new(Tid::Carousel, b.to_daletl_body(), NA), Tag::Carousel(b) => t_new(Tid::Carousel, b.to_dl_body(), NA),
Tag::Code(s, a) => t_new(Tid::Code, s.to_daletl_body(), a.to_daletl_argument()), Tag::Code(s, a) => t_new(Tid::Code, s.to_dl_body(), a.to_dl_arg()),
Tag::Pre(s) => t_new(Tid::Pre, s.to_daletl_body(), NA), Tag::Pre(s) => t_new(Tid::Pre, s.to_dl_body(), NA),
} }
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] #[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)]
#[repr(u8)] #[repr(u8)]
pub enum HeadingLevel { /// Heading level
pub enum Hl {
One = 1, One = 1,
Two, Two,
Three, Three,
@ -87,15 +88,15 @@ pub enum HeadingLevel {
Six, Six,
} }
impl ToDaletlArgument for HeadingLevel { impl ToDaletlArgument for Hl {
fn to_daletl_argument(self) -> daletl::Argument { fn to_dl_arg(self) -> daletl::Argument {
match self { match self {
HeadingLevel::One => NA, Hl::One => NA,
HeadingLevel::Two => 2u8.to_daletl_argument(), Hl::Two => 2u8.to_dl_arg(),
HeadingLevel::Three => 3u8.to_daletl_argument(), Hl::Three => 3u8.to_dl_arg(),
HeadingLevel::Four => 4u8.to_daletl_argument(), Hl::Four => 4u8.to_dl_arg(),
HeadingLevel::Five => 5u8.to_daletl_argument(), Hl::Five => 5u8.to_dl_arg(),
HeadingLevel::Six => 6u8.to_daletl_argument(), Hl::Six => 6u8.to_dl_arg(),
} }
} }
} }
@ -109,40 +110,25 @@ pub enum AlignArgument {
} }
impl ToDaletlArgument for AlignArgument { impl ToDaletlArgument for AlignArgument {
fn to_daletl_argument(self) -> daletl::Argument { fn to_dl_arg(self) -> daletl::Argument {
match self { match self {
Self::Start => NA, Self::Start => NA,
Self::Center => 1u8.to_daletl_argument(), Self::Center => 1u8.to_dl_arg(),
Self::End => 2u8.to_daletl_argument(), Self::End => 2u8.to_dl_arg(),
} }
} }
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum TextOrNumberArgument { pub enum TNArgument {
Text(String),
Number(u8),
}
impl ToDaletlArgument for TextOrNumberArgument {
fn to_daletl_argument(self) -> daletl::Argument {
match self {
Self::Number(n) => n.to_daletl_argument(),
Self::Text(s) => s.to_daletl_argument(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TextOrNullArgument {
Text(String), Text(String),
Null, Null,
} }
impl ToDaletlArgument for TextOrNullArgument { impl ToDaletlArgument for TNArgument {
fn to_daletl_argument(self) -> daletl::Argument { fn to_dl_arg(self) -> daletl::Argument {
match self { match self {
Self::Text(s) => s.to_daletl_argument(), Self::Text(s) => s.to_dl_arg(),
Self::Null => NA, Self::Null => NA,
} }
} }
@ -156,88 +142,89 @@ pub enum Body {
} }
impl ToDaletlBody for Body { impl ToDaletlBody for Body {
fn to_daletl_body(self) -> daletl::Body { fn to_dl_body(self) -> daletl::Body {
match self { match self {
Body::Null => NB, Body::Null => NB,
Body::Tags(v) => v.to_daletl_body(), Body::Tags(v) => v.to_dl_body(),
Body::Text(v) => v.to_daletl_body(), Body::Text(v) => v.to_dl_body(),
} }
} }
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Argument { pub enum Arg {
Text(String), Text(String),
Number(u8), Number(u8),
Null, Null,
} }
impl ToDaletlArgument for Argument { impl ToDaletlArgument for Arg {
fn to_daletl_argument(self) -> daletl::Argument { fn to_dl_arg(self) -> daletl::Argument {
match self { match self {
Argument::Null => NA, Arg::Null => NA,
Argument::Number(v) => v.to_daletl_argument(), Arg::Number(v) => v.to_dl_arg(),
Argument::Text(v) => v.to_daletl_argument(), Arg::Text(v) => v.to_dl_arg(),
} }
} }
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotNullArgument { /// Not null argument
pub enum NNArg {
Text(String), Text(String),
Number(u8), Number(u8),
} }
impl ToDaletlArgument for NotNullArgument { impl ToDaletlArgument for NNArg {
fn to_daletl_argument(self) -> daletl::Argument { fn to_dl_arg(self) -> daletl::Argument {
match self { match self {
NotNullArgument::Number(v) => v.to_daletl_argument(), NNArg::Number(v) => v.to_dl_arg(),
NotNullArgument::Text(v) => v.to_daletl_argument(), NNArg::Text(v) => v.to_dl_arg(),
} }
} }
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotNullBody { pub enum NNBody {
Text(String), Text(String),
Tags(Vec<Tag>), Tags(Vec<Tag>),
} }
impl ToDaletlBody for NotNullBody { impl ToDaletlBody for NNBody {
fn to_daletl_body(self) -> daletl::Body { fn to_dl_body(self) -> daletl::Body {
match self { match self {
NotNullBody::Text(v) => v.to_daletl_body(), NNBody::Text(v) => v.to_dl_body(),
NotNullBody::Tags(v) => v.to_daletl_body(), NNBody::Tags(v) => v.to_dl_body(),
} }
} }
} }
impl ToDaletlBody for Vec<Tag> { impl ToDaletlBody for Vec<Tag> {
fn to_daletl_body(self) -> daletl::Body { fn to_dl_body(self) -> daletl::Body {
daletl::Body::Tags(self.to_daletl()) daletl::Body::Tags(self.to_dl())
} }
} }
impl ToDaletl for Vec<Tag> { impl ToDaletl for Vec<Tag> {
fn to_daletl(self) -> Vec<daletl::Tag> { fn to_dl(self) -> Vec<daletl::Tag> {
self.into_iter().map(|tag| tag.to_daletl_tag()).collect() self.into_iter().map(|tag| tag.to_dl_tag()).collect()
} }
} }
impl ToDaletlBody for String { impl ToDaletlBody for String {
fn to_daletl_body(self) -> daletl::Body { fn to_dl_body(self) -> daletl::Body {
daletl::Body::Text(self) daletl::Body::Text(self)
} }
} }
impl ToDaletlArgument for String { impl ToDaletlArgument for String {
fn to_daletl_argument(self) -> daletl::Argument { fn to_dl_arg(self) -> daletl::Argument {
daletl::Argument::Text(self) daletl::Argument::Text(self)
} }
} }
impl ToDaletlArgument for u8 { impl ToDaletlArgument for u8 {
fn to_daletl_argument(self) -> daletl::Argument { fn to_dl_arg(self) -> daletl::Argument {
daletl::Argument::Number(self) daletl::Argument::Number(self)
} }
} }

View file

@ -1,7 +1,7 @@
use dalet::{ use dalet::{
daletl::ToDaletl, daletl::ToDaletl,
daletpack::*, daletpack::*,
typed::{Body, HeadingLevel, NotNullBody, Tag, TextOrNullArgument}, typed::{Body, Hl, NNBody, TNArgument, Tag},
}; };
use flate2::Compression; use flate2::Compression;
use std::io::Write; use std::io::Write;
@ -33,54 +33,54 @@ pub fn compress_zlib(data: &Vec<u8>) -> std::io::Result<Vec<u8>> {
#[test] #[test]
fn bench() { fn bench() {
let page: Vec<Tag> = vec![ let page: Vec<Tag> = vec![
Tag::H("I am heading".to_owned(), HeadingLevel::One), Tag::H("I am heading".to_owned(), Hl::One),
Tag::H("Heading 2".to_owned(), HeadingLevel::Two), Tag::H("Heading 2".to_owned(), Hl::Two),
Tag::El(NotNullBody::Tags(vec![ Tag::El(NNBody::Tags(vec![
Tag::El(NotNullBody::Text("Some ".to_owned())), Tag::El(NNBody::Text("Some ".to_owned())),
Tag::B("bold".to_owned()), Tag::B("bold".to_owned()),
Tag::I("italic".to_owned()), Tag::I("italic".to_owned()),
Tag::S("strike".to_owned()), Tag::S("strike".to_owned()),
])), ])),
Tag::Br, Tag::Br,
Tag::Code("Hello world".to_owned(), TextOrNullArgument::Null), Tag::Code("Hello world".to_owned(), TNArgument::Null),
Tag::Br, Tag::Br,
Tag::Ol(vec![ Tag::Ol(vec![
Tag::El(NotNullBody::Text("abc".to_owned())), Tag::El(NNBody::Text("abc".to_owned())),
Tag::El(NotNullBody::Tags(vec![ Tag::El(NNBody::Tags(vec![
Tag::El(NotNullBody::Text("def".to_owned())), Tag::El(NNBody::Text("def".to_owned())),
Tag::Ol(vec![ Tag::Ol(vec![
Tag::El(NotNullBody::Text("defabc".to_owned())), Tag::El(NNBody::Text("defabc".to_owned())),
Tag::El(NotNullBody::Text("defdef".to_owned())), Tag::El(NNBody::Text("defdef".to_owned())),
]), ]),
])), ])),
Tag::El(NotNullBody::Text("xyz".to_owned())), Tag::El(NNBody::Text("xyz".to_owned())),
]), ]),
Tag::Br, Tag::Br,
Tag::El(NotNullBody::Tags(vec![ Tag::El(NNBody::Tags(vec![
Tag::El(NotNullBody::Text("Lorem ipsum ".to_owned())), Tag::El(NNBody::Text("Lorem ipsum ".to_owned())),
Tag::Link( Tag::Link(
Body::Tags(vec![Tag::Img("https://my-picture".to_owned())]), Body::Tags(vec![Tag::Img("https://my-picture".to_owned())]),
"https://some-link".to_owned(), "https://some-link".to_owned(),
), ),
Tag::El(NotNullBody::Text( Tag::El(NNBody::Text(
" dolor sit amet consequetur adipiscing elit".to_owned(), " dolor sit amet consequetur adipiscing elit".to_owned(),
)), )),
])), ])),
Tag::Table(vec![ Tag::Table(vec![
Tag::Tpcol(vec![ Tag::Tpcol(vec![
Tag::El(NotNullBody::Text("Col 1".to_owned())), Tag::El(NNBody::Text("Col 1".to_owned())),
Tag::El(NotNullBody::Text("Col 2".to_owned())), Tag::El(NNBody::Text("Col 2".to_owned())),
Tag::El(NotNullBody::Text("Col 3".to_owned())), Tag::El(NNBody::Text("Col 3".to_owned())),
]), ]),
Tag::Tcol(vec![ Tag::Tcol(vec![
Tag::El(NotNullBody::Text("Never gonna".to_owned())), Tag::El(NNBody::Text("Never gonna".to_owned())),
Tag::El(NotNullBody::Text("give you".to_owned())), Tag::El(NNBody::Text("give you".to_owned())),
Tag::El(NotNullBody::Text("up".to_owned())), Tag::El(NNBody::Text("up".to_owned())),
]), ]),
]), ]),
]; ];
let dalet_page = page.to_daletl(); let dalet_page = page.to_dl();
let markdown = iprint!("Markdown", include_str!("./bench.md").as_bytes().to_vec()); let markdown = iprint!("Markdown", include_str!("./bench.md").as_bytes().to_vec());
let daletpack = iprint!("Daletpack", encode_no_compress(&dalet_page).unwrap()); let daletpack = iprint!("Daletpack", encode_no_compress(&dalet_page).unwrap());