From ef1688b3be7cd6ecbad3cf61c329a5b46dde6859 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Fri, 2 Aug 2024 22:35:49 +0300 Subject: [PATCH] refactor: rename some types --- src/daletl.rs | 29 ++++--- src/parsers/gemtext.rs | 14 ++-- src/typed.rs | 179 +++++++++++++++++++---------------------- tests/bench.rs | 44 +++++----- 4 files changed, 129 insertions(+), 137 deletions(-) diff --git a/src/daletl.rs b/src/daletl.rs index 10f2c65..7951b60 100644 --- a/src/daletl.rs +++ b/src/daletl.rs @@ -17,6 +17,7 @@ impl Tag { } } +#[inline] pub fn t_new(id: Tid, body: Body, argument: Argument) -> Tag { Tag::new(id, body, argument) } @@ -29,6 +30,14 @@ pub enum Body { Null, } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[serde(untagged)] +pub enum Argument { + Text(String), + Number(u8), + Null, +} + impl IsNull for Body { fn is_null(&self) -> bool { 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 { fn is_null(&self) -> bool { match self { @@ -96,17 +97,21 @@ pub trait IsNull { } pub trait ToDaletl { - fn to_daletl(self) -> Vec; + /// Convert to daletl root + fn to_dl(self) -> Vec; } pub trait ToDaletlTag { - fn to_daletl_tag(self) -> Tag; + /// Convert to daletl tag + fn to_dl_tag(self) -> Tag; } pub trait ToDaletlBody { - fn to_daletl_body(self) -> Body; + /// Convert to daletl body + fn to_dl_body(self) -> Body; } pub trait ToDaletlArgument { - fn to_daletl_argument(self) -> Argument; + /// Convert to daletl arg + fn to_dl_arg(self) -> Argument; } diff --git a/src/parsers/gemtext.rs b/src/parsers/gemtext.rs index 0692cec..7053383 100644 --- a/src/parsers/gemtext.rs +++ b/src/parsers/gemtext.rs @@ -1,4 +1,4 @@ -use crate::typed::{Body, HeadingLevel, NotNullBody, Tag}; +use crate::typed::{Body, Hl, NNBody, Tag}; #[derive(Debug)] pub enum GemTextParseError { @@ -38,20 +38,20 @@ pub fn parse_gemtext(s: String) -> Result, GemTextParseError> { }; } else if line.starts_with("# ") { 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("## ") { 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("### ") { 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("* ") { list_before = true; 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("> ") { 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("```") { if preformatted { page.push(Tag::Pre(preformatted_text.clone())); @@ -60,7 +60,7 @@ pub fn parse_gemtext(s: String) -> Result, GemTextParseError> { preformatted = !preformatted; } else if !line.is_empty() { - page.push(Tag::P(NotNullBody::Text(line))); + page.push(Tag::P(NNBody::Text(line))); } } diff --git a/src/typed.rs b/src/typed.rs index 98cf1d0..cfa2a9d 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -7,9 +7,9 @@ const NA: daletl::Argument = daletl::Argument::Null; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Tag { - El(NotNullBody), - H(String, HeadingLevel), - P(NotNullBody), + El(NNBody), + H(String, Hl), + P(NNBody), Br, Ul(Vec), Ol(Vec), @@ -25,60 +25,61 @@ pub enum Tag { Hr, B(String), I(String), - Bq(NotNullBody), - Footlnk(TextOrNumberArgument), - Footn(String, TextOrNumberArgument), - A(TextOrNumberArgument), + Bq(NNBody), + Footlnk(NNArg), + Footn(String, NNArg), + A(NNArg), S(String), Sup(String), Sub(String), - Disc(NotNullBody), - Bl(NotNullBody, AlignArgument), + Disc(NNBody), + Bl(NNBody, AlignArgument), Carousel(Vec), - Code(String, TextOrNullArgument), + Code(String, TNArgument), Pre(String), } impl ToDaletlTag for Tag { - fn to_daletl_tag(self) -> daletl::Tag { + fn to_dl_tag(self) -> daletl::Tag { match self { - Tag::El(b) => t_new(Tid::El, b.to_daletl_body(), NA), - Tag::H(b, a) => t_new(Tid::H, b.to_daletl_body(), a.to_daletl_argument()), - Tag::P(b) => t_new(Tid::P, 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_dl_body(), a.to_dl_arg()), + Tag::P(b) => t_new(Tid::P, b.to_dl_body(), NA), Tag::Br => t_new(Tid::Br, NB, NA), - Tag::Ul(b) => t_new(Tid::Ul, b.to_daletl_body(), NA), - Tag::Ol(b) => t_new(Tid::Ol, b.to_daletl_body(), NA), - Tag::Row(b, a) => t_new(Tid::Row, b.to_daletl_body(), a.to_daletl_argument()), - Tag::Link(b, a) => t_new(Tid::Link, b.to_daletl_body(), a.to_daletl_argument()), - Tag::Navlink(b, a) => t_new(Tid::Navlink, b.to_daletl_body(), a.to_daletl_argument()), - Tag::Btn(b, a) => t_new(Tid::Btn, b.to_daletl_body(), a.to_daletl_argument()), - Tag::Navbtn(b, a) => t_new(Tid::Navbtn, b.to_daletl_body(), a.to_daletl_argument()), - Tag::Img(a) => t_new(Tid::Img, NB, a.to_daletl_argument()), - Tag::Table(b) => t_new(Tid::Table, b.to_daletl_body(), NA), - Tag::Tcol(b) => t_new(Tid::Tcol, b.to_daletl_body(), NA), - Tag::Tpcol(b) => t_new(Tid::Tpcol, 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_dl_body(), NA), + 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_dl_body(), a.to_dl_arg()), + 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_dl_body(), a.to_dl_arg()), + 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_dl_arg()), + Tag::Table(b) => t_new(Tid::Table, b.to_dl_body(), NA), + Tag::Tcol(b) => t_new(Tid::Tcol, b.to_dl_body(), NA), + Tag::Tpcol(b) => t_new(Tid::Tpcol, b.to_dl_body(), NA), Tag::Hr => t_new(Tid::Hr, NB, NA), - Tag::B(b) => t_new(Tid::B, b.to_daletl_body(), NA), - Tag::I(b) => t_new(Tid::I, b.to_daletl_body(), NA), - Tag::Bq(b) => t_new(Tid::Bq, b.to_daletl_body(), NA), - Tag::Footlnk(a) => t_new(Tid::Footlnk, NB, a.to_daletl_argument()), - Tag::Footn(b, a) => t_new(Tid::Footn, b.to_daletl_body(), a.to_daletl_argument()), - Tag::A(a) => t_new(Tid::A, NB, a.to_daletl_argument()), - Tag::S(b) => t_new(Tid::S, b.to_daletl_body(), NA), - Tag::Sup(b) => t_new(Tid::Sup, b.to_daletl_body(), NA), - Tag::Sub(b) => t_new(Tid::Sub, b.to_daletl_body(), NA), - Tag::Disc(b) => t_new(Tid::Disc, b.to_daletl_body(), NA), - Tag::Bl(b, a) => t_new(Tid::Bl, b.to_daletl_body(), a.to_daletl_argument()), - Tag::Carousel(b) => t_new(Tid::Carousel, b.to_daletl_body(), NA), - Tag::Code(s, a) => t_new(Tid::Code, s.to_daletl_body(), a.to_daletl_argument()), - Tag::Pre(s) => t_new(Tid::Pre, s.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_dl_body(), NA), + Tag::Bq(b) => t_new(Tid::Bq, b.to_dl_body(), NA), + Tag::Footlnk(a) => t_new(Tid::Footlnk, NB, a.to_dl_arg()), + 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_dl_arg()), + Tag::S(b) => t_new(Tid::S, b.to_dl_body(), NA), + Tag::Sup(b) => t_new(Tid::Sup, b.to_dl_body(), NA), + Tag::Sub(b) => t_new(Tid::Sub, b.to_dl_body(), NA), + Tag::Disc(b) => t_new(Tid::Disc, b.to_dl_body(), NA), + 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_dl_body(), NA), + 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_dl_body(), NA), } } } #[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] #[repr(u8)] -pub enum HeadingLevel { +/// Heading level +pub enum Hl { One = 1, Two, Three, @@ -87,15 +88,15 @@ pub enum HeadingLevel { Six, } -impl ToDaletlArgument for HeadingLevel { - fn to_daletl_argument(self) -> daletl::Argument { +impl ToDaletlArgument for Hl { + fn to_dl_arg(self) -> daletl::Argument { match self { - HeadingLevel::One => NA, - HeadingLevel::Two => 2u8.to_daletl_argument(), - HeadingLevel::Three => 3u8.to_daletl_argument(), - HeadingLevel::Four => 4u8.to_daletl_argument(), - HeadingLevel::Five => 5u8.to_daletl_argument(), - HeadingLevel::Six => 6u8.to_daletl_argument(), + Hl::One => NA, + Hl::Two => 2u8.to_dl_arg(), + Hl::Three => 3u8.to_dl_arg(), + Hl::Four => 4u8.to_dl_arg(), + Hl::Five => 5u8.to_dl_arg(), + Hl::Six => 6u8.to_dl_arg(), } } } @@ -109,40 +110,25 @@ pub enum AlignArgument { } impl ToDaletlArgument for AlignArgument { - fn to_daletl_argument(self) -> daletl::Argument { + fn to_dl_arg(self) -> daletl::Argument { match self { Self::Start => NA, - Self::Center => 1u8.to_daletl_argument(), - Self::End => 2u8.to_daletl_argument(), + Self::Center => 1u8.to_dl_arg(), + Self::End => 2u8.to_dl_arg(), } } } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum TextOrNumberArgument { - 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 { +pub enum TNArgument { Text(String), Null, } -impl ToDaletlArgument for TextOrNullArgument { - fn to_daletl_argument(self) -> daletl::Argument { +impl ToDaletlArgument for TNArgument { + fn to_dl_arg(self) -> daletl::Argument { match self { - Self::Text(s) => s.to_daletl_argument(), + Self::Text(s) => s.to_dl_arg(), Self::Null => NA, } } @@ -156,88 +142,89 @@ pub enum Body { } impl ToDaletlBody for Body { - fn to_daletl_body(self) -> daletl::Body { + fn to_dl_body(self) -> daletl::Body { match self { Body::Null => NB, - Body::Tags(v) => v.to_daletl_body(), - Body::Text(v) => v.to_daletl_body(), + Body::Tags(v) => v.to_dl_body(), + Body::Text(v) => v.to_dl_body(), } } } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum Argument { +pub enum Arg { Text(String), Number(u8), Null, } -impl ToDaletlArgument for Argument { - fn to_daletl_argument(self) -> daletl::Argument { +impl ToDaletlArgument for Arg { + fn to_dl_arg(self) -> daletl::Argument { match self { - Argument::Null => NA, - Argument::Number(v) => v.to_daletl_argument(), - Argument::Text(v) => v.to_daletl_argument(), + Arg::Null => NA, + Arg::Number(v) => v.to_dl_arg(), + Arg::Text(v) => v.to_dl_arg(), } } } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum NotNullArgument { +/// Not null argument +pub enum NNArg { Text(String), Number(u8), } -impl ToDaletlArgument for NotNullArgument { - fn to_daletl_argument(self) -> daletl::Argument { +impl ToDaletlArgument for NNArg { + fn to_dl_arg(self) -> daletl::Argument { match self { - NotNullArgument::Number(v) => v.to_daletl_argument(), - NotNullArgument::Text(v) => v.to_daletl_argument(), + NNArg::Number(v) => v.to_dl_arg(), + NNArg::Text(v) => v.to_dl_arg(), } } } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum NotNullBody { +pub enum NNBody { Text(String), Tags(Vec), } -impl ToDaletlBody for NotNullBody { - fn to_daletl_body(self) -> daletl::Body { +impl ToDaletlBody for NNBody { + fn to_dl_body(self) -> daletl::Body { match self { - NotNullBody::Text(v) => v.to_daletl_body(), - NotNullBody::Tags(v) => v.to_daletl_body(), + NNBody::Text(v) => v.to_dl_body(), + NNBody::Tags(v) => v.to_dl_body(), } } } impl ToDaletlBody for Vec { - fn to_daletl_body(self) -> daletl::Body { - daletl::Body::Tags(self.to_daletl()) + fn to_dl_body(self) -> daletl::Body { + daletl::Body::Tags(self.to_dl()) } } impl ToDaletl for Vec { - fn to_daletl(self) -> Vec { - self.into_iter().map(|tag| tag.to_daletl_tag()).collect() + fn to_dl(self) -> Vec { + self.into_iter().map(|tag| tag.to_dl_tag()).collect() } } impl ToDaletlBody for String { - fn to_daletl_body(self) -> daletl::Body { + fn to_dl_body(self) -> daletl::Body { daletl::Body::Text(self) } } impl ToDaletlArgument for String { - fn to_daletl_argument(self) -> daletl::Argument { + fn to_dl_arg(self) -> daletl::Argument { daletl::Argument::Text(self) } } impl ToDaletlArgument for u8 { - fn to_daletl_argument(self) -> daletl::Argument { + fn to_dl_arg(self) -> daletl::Argument { daletl::Argument::Number(self) } } diff --git a/tests/bench.rs b/tests/bench.rs index ee36521..07330ad 100644 --- a/tests/bench.rs +++ b/tests/bench.rs @@ -1,7 +1,7 @@ use dalet::{ daletl::ToDaletl, daletpack::*, - typed::{Body, HeadingLevel, NotNullBody, Tag, TextOrNullArgument}, + typed::{Body, Hl, NNBody, TNArgument, Tag}, }; use flate2::Compression; use std::io::Write; @@ -33,54 +33,54 @@ pub fn compress_zlib(data: &Vec) -> std::io::Result> { #[test] fn bench() { let page: Vec = vec![ - Tag::H("I am heading".to_owned(), HeadingLevel::One), - Tag::H("Heading 2".to_owned(), HeadingLevel::Two), - Tag::El(NotNullBody::Tags(vec![ - Tag::El(NotNullBody::Text("Some ".to_owned())), + Tag::H("I am heading".to_owned(), Hl::One), + Tag::H("Heading 2".to_owned(), Hl::Two), + Tag::El(NNBody::Tags(vec![ + Tag::El(NNBody::Text("Some ".to_owned())), Tag::B("bold".to_owned()), Tag::I("italic".to_owned()), Tag::S("strike".to_owned()), ])), Tag::Br, - Tag::Code("Hello world".to_owned(), TextOrNullArgument::Null), + Tag::Code("Hello world".to_owned(), TNArgument::Null), Tag::Br, Tag::Ol(vec![ - Tag::El(NotNullBody::Text("abc".to_owned())), - Tag::El(NotNullBody::Tags(vec![ - Tag::El(NotNullBody::Text("def".to_owned())), + Tag::El(NNBody::Text("abc".to_owned())), + Tag::El(NNBody::Tags(vec![ + Tag::El(NNBody::Text("def".to_owned())), Tag::Ol(vec![ - Tag::El(NotNullBody::Text("defabc".to_owned())), - Tag::El(NotNullBody::Text("defdef".to_owned())), + Tag::El(NNBody::Text("defabc".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::El(NotNullBody::Tags(vec![ - Tag::El(NotNullBody::Text("Lorem ipsum ".to_owned())), + Tag::El(NNBody::Tags(vec![ + Tag::El(NNBody::Text("Lorem ipsum ".to_owned())), Tag::Link( Body::Tags(vec![Tag::Img("https://my-picture".to_owned())]), "https://some-link".to_owned(), ), - Tag::El(NotNullBody::Text( + Tag::El(NNBody::Text( " dolor sit amet consequetur adipiscing elit".to_owned(), )), ])), Tag::Table(vec![ Tag::Tpcol(vec![ - Tag::El(NotNullBody::Text("Col 1".to_owned())), - Tag::El(NotNullBody::Text("Col 2".to_owned())), - Tag::El(NotNullBody::Text("Col 3".to_owned())), + Tag::El(NNBody::Text("Col 1".to_owned())), + Tag::El(NNBody::Text("Col 2".to_owned())), + Tag::El(NNBody::Text("Col 3".to_owned())), ]), Tag::Tcol(vec![ - Tag::El(NotNullBody::Text("Never gonna".to_owned())), - Tag::El(NotNullBody::Text("give you".to_owned())), - Tag::El(NotNullBody::Text("up".to_owned())), + Tag::El(NNBody::Text("Never gonna".to_owned())), + Tag::El(NNBody::Text("give you".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 daletpack = iprint!("Daletpack", encode_no_compress(&dalet_page).unwrap());