From c121a533147a10053d4dd7ba74cee64c2ed787c7 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Mon, 5 Aug 2024 15:34:39 +0300 Subject: [PATCH] refactor: move traits to module --- src/daletl.rs | 140 +-------------------------------------- src/daletpack/encode.rs | 6 +- src/lib.rs | 3 + src/traits/mod.rs | 1 + src/traits/typed.rs | 141 ++++++++++++++++++++++++++++++++++++++++ src/typed.rs | 8 --- 6 files changed, 150 insertions(+), 149 deletions(-) create mode 100644 src/traits/mod.rs create mode 100644 src/traits/typed.rs diff --git a/src/daletl.rs b/src/daletl.rs index 0710e6e..a6b0b3b 100644 --- a/src/daletl.rs +++ b/src/daletl.rs @@ -3,12 +3,7 @@ use num_enum::TryFromPrimitive; use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; -use crate::typed::*; - -const NB: DlBody = DlBody::Null; -const NA: DlArgument = DlArgument::Null; - -pub type Page = Vec; +pub type DlPage = Vec; #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct DlTag { @@ -45,24 +40,6 @@ pub enum DlArgument { Null, } -impl IsNull for DlBody { - fn is_null(&self) -> bool { - match self { - Self::Null => true, - _ => false, - } - } -} - -impl IsNull for DlArgument { - fn is_null(&self) -> bool { - match self { - Self::Null => true, - _ => false, - } - } -} - #[derive(Serialize_repr, Deserialize_repr, Debug, Clone, PartialEq, Eq, TryFromPrimitive, Copy)] #[repr(u8)] /// Tag Id @@ -105,118 +82,5 @@ pub trait IsNull { pub trait ToDaletlPage { /// Convert to daletl Page - fn to_dl_page(self) -> Page; -} - -impl From for DlTag { - fn from(item: Tag) -> DlTag { - match item { - Tag::El(b) => dlt_new(DlTid::El, b.into(), NA), - Tag::H(b, a) => dlt_new(DlTid::H, b.into(), a.into()), - Tag::P(b) => dlt_new(DlTid::P, b.into(), NA), - Tag::Br => dlt_new(DlTid::Br, NB, NA), - Tag::Ul(b) => dlt_new(DlTid::Ul, b.into(), NA), - Tag::Ol(b) => dlt_new(DlTid::Ol, b.into(), NA), - Tag::Row(b, a) => dlt_new(DlTid::Row, b.into(), a.into()), - Tag::Link(b, a) => dlt_new(DlTid::Link, b.into(), a.into()), - Tag::Navlink(b, a) => dlt_new(DlTid::Navlink, b.into(), a.into()), - Tag::Btn(b, a) => dlt_new(DlTid::Btn, b.into(), a.into()), - Tag::Navbtn(b, a) => dlt_new(DlTid::Navbtn, b.into(), a.into()), - Tag::Img(a) => dlt_new(DlTid::Img, NB, a.into()), - Tag::Table(b) => dlt_new(DlTid::Table, b.into(), NA), - Tag::Tcol(b) => dlt_new(DlTid::Tcol, b.into(), NA), - Tag::Tpcol(b) => dlt_new(DlTid::Tpcol, b.into(), NA), - Tag::Hr => dlt_new(DlTid::Hr, NB, NA), - Tag::B(b) => dlt_new(DlTid::B, b.into(), NA), - Tag::I(b) => dlt_new(DlTid::I, b.into(), NA), - Tag::Bq(b) => dlt_new(DlTid::Bq, b.into(), NA), - Tag::Footlnk(a) => dlt_new(DlTid::Footlnk, NB, a.into()), - Tag::Footn(b, a) => dlt_new(DlTid::Footn, b.into(), a.into()), - Tag::A(a) => dlt_new(DlTid::A, NB, a.into()), - Tag::S(b) => dlt_new(DlTid::S, b.into(), NA), - Tag::Sup(b) => dlt_new(DlTid::Sup, b.into(), NA), - Tag::Sub(b) => dlt_new(DlTid::Sub, b.into(), NA), - Tag::Disc(b) => dlt_new(DlTid::Disc, b.into(), NA), - Tag::Bl(b, a) => dlt_new(DlTid::Bl, b.into(), a.into()), - Tag::Carousel(b) => dlt_new(DlTid::Carousel, b.into(), NA), - Tag::Code(b, a) => dlt_new(DlTid::Code, b.into(), a.into()), - Tag::Pre(b) => dlt_new(DlTid::Pre, b.into(), NA), - } - } -} - -impl From for DlArgument { - fn from(item: Hl) -> DlArgument { - match item { - Hl::One => NA, - Hl::Two => 2u8.into(), - Hl::Three => 3u8.into(), - Hl::Four => 4u8.into(), - Hl::Five => 5u8.into(), - Hl::Six => 6u8.into(), - } - } -} - -impl From for DlArgument { - fn from(item: AlignArg) -> DlArgument { - match item { - AlignArg::Start => NA, - AlignArg::Center => 1u8.into(), - AlignArg::End => 2u8.into(), - } - } -} - -impl From for DlArgument { - fn from(item: TNArg) -> DlArgument { - match item { - TNArg::Text(s) => s.into(), - TNArg::Null => NA, - } - } -} - -impl From for DlBody { - fn from(item: Body) -> DlBody { - match item { - Body::Null => NB, - Body::Tags(v) => v.into(), - Body::Text(v) => v.into(), - } - } -} - -impl From for DlArgument { - fn from(item: Arg) -> DlArgument { - match item { - Arg::Null => NA, - Arg::Number(v) => v.into(), - Arg::Text(v) => v.into(), - } - } -} - -impl From for DlArgument { - fn from(item: NNArg) -> DlArgument { - match item { - NNArg::Number(v) => v.into(), - NNArg::Text(v) => v.into(), - } - } -} - -impl From for DlBody { - fn from(item: NNBody) -> DlBody { - match item { - NNBody::Text(v) => v.into(), - NNBody::Tags(v) => v.into(), - } - } -} - -impl From> for DlBody { - fn from(item: Vec) -> DlBody { - DlBody::Tags(item.into_iter().map(|tag| tag.into()).collect()) - } + fn to_dl_page(self) -> DlPage; } diff --git a/src/daletpack/encode.rs b/src/daletpack/encode.rs index be582d0..844173b 100644 --- a/src/daletpack/encode.rs +++ b/src/daletpack/encode.rs @@ -1,13 +1,13 @@ -use crate::daletl::{DlArgument, DlBody, DlTag, DlTid, IsNull, Page}; +use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid, IsNull}; use super::{utils, DaletPackError, TypeId}; -pub fn encode(page: &Page) -> Result, DaletPackError> { +pub fn encode(page: &DlPage) -> Result, DaletPackError> { Ok(utils::compress_zstd(&encode_no_compress(page)?) .map_err(|_| DaletPackError::ZstdCompressError)?) } -pub fn encode_no_compress(page: &Page) -> Result, DaletPackError> { +pub fn encode_no_compress(page: &DlPage) -> Result, DaletPackError> { if page.len() > 2usize.pow(32) { return Err(DaletPackError::PageMaxSizeExceeded); } diff --git a/src/lib.rs b/src/lib.rs index 7134d3e..c04812b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,9 @@ pub mod daletl; #[cfg(feature = "types")] pub mod typed; +#[cfg(feature = "types")] +mod traits; + #[cfg(feature = "daletpack")] pub mod daletpack; diff --git a/src/traits/mod.rs b/src/traits/mod.rs new file mode 100644 index 0000000..2f716a8 --- /dev/null +++ b/src/traits/mod.rs @@ -0,0 +1 @@ +mod typed; diff --git a/src/traits/typed.rs b/src/traits/typed.rs new file mode 100644 index 0000000..88ec21f --- /dev/null +++ b/src/traits/typed.rs @@ -0,0 +1,141 @@ +use crate::{daletl::*, typed::*}; + +const NB: DlBody = DlBody::Null; +const NA: DlArgument = DlArgument::Null; + +impl From for DlTag { + fn from(item: Tag) -> DlTag { + match item { + Tag::El(b) => dlt_new(DlTid::El, b.into(), NA), + Tag::H(b, a) => dlt_new(DlTid::H, b.into(), a.into()), + Tag::P(b) => dlt_new(DlTid::P, b.into(), NA), + Tag::Br => dlt_new(DlTid::Br, NB, NA), + Tag::Ul(b) => dlt_new(DlTid::Ul, b.into(), NA), + Tag::Ol(b) => dlt_new(DlTid::Ol, b.into(), NA), + Tag::Row(b, a) => dlt_new(DlTid::Row, b.into(), a.into()), + Tag::Link(b, a) => dlt_new(DlTid::Link, b.into(), a.into()), + Tag::Navlink(b, a) => dlt_new(DlTid::Navlink, b.into(), a.into()), + Tag::Btn(b, a) => dlt_new(DlTid::Btn, b.into(), a.into()), + Tag::Navbtn(b, a) => dlt_new(DlTid::Navbtn, b.into(), a.into()), + Tag::Img(a) => dlt_new(DlTid::Img, NB, a.into()), + Tag::Table(b) => dlt_new(DlTid::Table, b.into(), NA), + Tag::Tcol(b) => dlt_new(DlTid::Tcol, b.into(), NA), + Tag::Tpcol(b) => dlt_new(DlTid::Tpcol, b.into(), NA), + Tag::Hr => dlt_new(DlTid::Hr, NB, NA), + Tag::B(b) => dlt_new(DlTid::B, b.into(), NA), + Tag::I(b) => dlt_new(DlTid::I, b.into(), NA), + Tag::Bq(b) => dlt_new(DlTid::Bq, b.into(), NA), + Tag::Footlnk(a) => dlt_new(DlTid::Footlnk, NB, a.into()), + Tag::Footn(b, a) => dlt_new(DlTid::Footn, b.into(), a.into()), + Tag::A(a) => dlt_new(DlTid::A, NB, a.into()), + Tag::S(b) => dlt_new(DlTid::S, b.into(), NA), + Tag::Sup(b) => dlt_new(DlTid::Sup, b.into(), NA), + Tag::Sub(b) => dlt_new(DlTid::Sub, b.into(), NA), + Tag::Disc(b) => dlt_new(DlTid::Disc, b.into(), NA), + Tag::Bl(b, a) => dlt_new(DlTid::Bl, b.into(), a.into()), + Tag::Carousel(b) => dlt_new(DlTid::Carousel, b.into(), NA), + Tag::Code(b, a) => dlt_new(DlTid::Code, b.into(), a.into()), + Tag::Pre(b) => dlt_new(DlTid::Pre, b.into(), NA), + } + } +} + +impl From for DlArgument { + fn from(item: Hl) -> DlArgument { + match item { + Hl::One => NA, + Hl::Two => 2u8.into(), + Hl::Three => 3u8.into(), + Hl::Four => 4u8.into(), + Hl::Five => 5u8.into(), + Hl::Six => 6u8.into(), + } + } +} + +impl From for DlArgument { + fn from(item: AlignArg) -> DlArgument { + match item { + AlignArg::Start => NA, + AlignArg::Center => 1u8.into(), + AlignArg::End => 2u8.into(), + } + } +} + +impl From for DlArgument { + fn from(item: TNArg) -> DlArgument { + match item { + TNArg::Text(s) => s.into(), + TNArg::Null => NA, + } + } +} + +impl From for DlBody { + fn from(item: Body) -> DlBody { + match item { + Body::Null => NB, + Body::Tags(v) => v.into(), + Body::Text(v) => v.into(), + } + } +} + +impl From for DlArgument { + fn from(item: Arg) -> DlArgument { + match item { + Arg::Null => NA, + Arg::Number(v) => v.into(), + Arg::Text(v) => v.into(), + } + } +} + +impl From for DlArgument { + fn from(item: NNArg) -> DlArgument { + match item { + NNArg::Number(v) => v.into(), + NNArg::Text(v) => v.into(), + } + } +} + +impl From for DlBody { + fn from(item: NNBody) -> DlBody { + match item { + NNBody::Text(v) => v.into(), + NNBody::Tags(v) => v.into(), + } + } +} + +impl From> for DlBody { + fn from(item: Vec) -> DlBody { + DlBody::Tags(item.into_iter().map(|tag| tag.into()).collect()) + } +} + +impl ToDaletlPage for Page { + fn to_dl_page(self) -> DlPage { + self.into_iter().map(|tag| tag.into()).collect() + } +} + +impl IsNull for DlBody { + fn is_null(&self) -> bool { + match self { + Self::Null => true, + _ => false, + } + } +} + +impl IsNull for DlArgument { + fn is_null(&self) -> bool { + match self { + Self::Null => true, + _ => false, + } + } +} diff --git a/src/typed.rs b/src/typed.rs index 2d1fc52..6c30c94 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -1,8 +1,6 @@ use enum_procs::AutoFrom; use num_enum::TryFromPrimitive; -use crate::daletl::{self, ToDaletlPage}; - pub type Page = Vec; #[derive(Debug, Clone, PartialEq, Eq)] @@ -97,9 +95,3 @@ pub enum Hl { Five, Six, } - -impl ToDaletlPage for Page { - fn to_dl_page(self) -> daletl::Page { - self.into_iter().map(|tag| tag.into()).collect() - } -}