From d79fbffa7a85e00f281cd0169cb75db26abdb9d2 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Mon, 5 Aug 2024 15:03:25 +0300 Subject: [PATCH] refactor: move traits from typed to daletl --- src/daletl.rs | 150 +++++++++++++++++++++++++++++++++++----- src/daletpack/encode.rs | 24 +++---- src/typed.rs | 118 +------------------------------ 3 files changed, 147 insertions(+), 145 deletions(-) diff --git a/src/daletl.rs b/src/daletl.rs index 47ae849..0710e6e 100644 --- a/src/daletl.rs +++ b/src/daletl.rs @@ -3,44 +3,49 @@ use num_enum::TryFromPrimitive; use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; -pub type Page = Vec; +use crate::typed::*; + +const NB: DlBody = DlBody::Null; +const NA: DlArgument = DlArgument::Null; + +pub type Page = Vec; #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] -pub struct Tag { - pub id: Tid, - pub body: Body, - pub argument: Argument, +pub struct DlTag { + pub id: DlTid, + pub body: DlBody, + pub argument: DlArgument, } -impl Tag { +impl DlTag { #[inline] - pub fn new(id: Tid, body: Body, argument: Argument) -> Tag { - Tag { id, body, argument } + pub fn new(id: DlTid, body: DlBody, argument: DlArgument) -> DlTag { + DlTag { id, body, argument } } } #[inline] -pub fn t_new(id: Tid, body: Body, argument: Argument) -> Tag { - Tag::new(id, body, argument) +pub fn dlt_new(id: DlTid, body: DlBody, argument: DlArgument) -> DlTag { + DlTag::new(id, body, argument) } #[derive(AutoFrom, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(untagged)] -pub enum Body { +pub enum DlBody { Text(String), - Tags(Vec), + Tags(Vec), Null, } #[derive(AutoFrom, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(untagged)] -pub enum Argument { +pub enum DlArgument { Text(String), Number(u8), Null, } -impl IsNull for Body { +impl IsNull for DlBody { fn is_null(&self) -> bool { match self { Self::Null => true, @@ -49,7 +54,7 @@ impl IsNull for Body { } } -impl IsNull for Argument { +impl IsNull for DlArgument { fn is_null(&self) -> bool { match self { Self::Null => true, @@ -61,7 +66,7 @@ impl IsNull for Argument { #[derive(Serialize_repr, Deserialize_repr, Debug, Clone, PartialEq, Eq, TryFromPrimitive, Copy)] #[repr(u8)] /// Tag Id -pub enum Tid { +pub enum DlTid { El, H, P, @@ -102,3 +107,116 @@ 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()) + } +} diff --git a/src/daletpack/encode.rs b/src/daletpack/encode.rs index 0d447f6..be582d0 100644 --- a/src/daletpack/encode.rs +++ b/src/daletpack/encode.rs @@ -1,4 +1,4 @@ -use crate::daletl::{Argument, Body, IsNull, Page, Tag, Tid}; +use crate::daletl::{DlArgument, DlBody, DlTag, DlTid, IsNull, Page}; use super::{utils, DaletPackError, TypeId}; @@ -49,7 +49,7 @@ fn write_str(bv: &mut Vec, string: &String) -> Result<(), DaletPackError> { Ok(()) } -fn write_array(bv: &mut Vec, arr: &Vec) -> Result<(), DaletPackError> { +fn write_array(bv: &mut Vec, arr: &Vec) -> Result<(), DaletPackError> { if arr.len() > 2usize.pow(32) { return Err(DaletPackError::ArrMaxSizeExceeded); } @@ -65,8 +65,8 @@ fn write_array(bv: &mut Vec, arr: &Vec) -> Result<(), DaletPackError> { Ok(()) } -fn write_tag(bv: &mut Vec, tag: &Tag) -> Result<(), DaletPackError> { - if tag.id == Tid::El { +fn write_tag(bv: &mut Vec, tag: &DlTag) -> Result<(), DaletPackError> { + if tag.id == DlTid::El { write_tag_body(bv, &tag.body)?; } else if tag.body.is_null() && tag.argument.is_null() { bv.push(TypeId::TagId as u8); @@ -89,21 +89,21 @@ fn write_tag(bv: &mut Vec, tag: &Tag) -> Result<(), DaletPackError> { Ok(()) } -fn write_tag_body(bv: &mut Vec, body: &Body) -> Result<(), DaletPackError> { +fn write_tag_body(bv: &mut Vec, body: &DlBody) -> Result<(), DaletPackError> { match body { - Body::Text(s) => write_str(bv, s)?, - Body::Tags(tags) => write_array(bv, tags)?, - Body::Null => Err(DaletPackError::WriteNullBody)?, + DlBody::Text(s) => write_str(bv, s)?, + DlBody::Tags(tags) => write_array(bv, tags)?, + DlBody::Null => Err(DaletPackError::WriteNullBody)?, }; Ok(()) } -fn write_tag_argument(bv: &mut Vec, argument: &Argument) -> Result<(), DaletPackError> { +fn write_tag_argument(bv: &mut Vec, argument: &DlArgument) -> Result<(), DaletPackError> { match argument { - Argument::Text(s) => write_str(bv, s)?, - Argument::Number(n) => write_int(bv, *n), - Argument::Null => Err(DaletPackError::WriteNullArgument)?, + DlArgument::Text(s) => write_str(bv, s)?, + DlArgument::Number(n) => write_int(bv, *n), + DlArgument::Null => Err(DaletPackError::WriteNullArgument)?, }; Ok(()) diff --git a/src/typed.rs b/src/typed.rs index f731028..2d1fc52 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -1,10 +1,7 @@ use enum_procs::AutoFrom; use num_enum::TryFromPrimitive; -use crate::daletl::{self, t_new, Tid, ToDaletlPage}; - -const NB: daletl::Body = daletl::Body::Null; -const NA: daletl::Argument = daletl::Argument::Null; +use crate::daletl::{self, ToDaletlPage}; pub type Page = Vec; @@ -101,119 +98,6 @@ pub enum Hl { Six, } -impl From for daletl::Tag { - fn from(item: Tag) -> daletl::Tag { - match item { - Tag::El(b) => t_new(Tid::El, b.into(), NA), - Tag::H(b, a) => t_new(Tid::H, b.into(), a.into()), - Tag::P(b) => t_new(Tid::P, b.into(), NA), - Tag::Br => t_new(Tid::Br, NB, NA), - Tag::Ul(b) => t_new(Tid::Ul, b.into(), NA), - Tag::Ol(b) => t_new(Tid::Ol, b.into(), NA), - Tag::Row(b, a) => t_new(Tid::Row, b.into(), a.into()), - Tag::Link(b, a) => t_new(Tid::Link, b.into(), a.into()), - Tag::Navlink(b, a) => t_new(Tid::Navlink, b.into(), a.into()), - Tag::Btn(b, a) => t_new(Tid::Btn, b.into(), a.into()), - Tag::Navbtn(b, a) => t_new(Tid::Navbtn, b.into(), a.into()), - Tag::Img(a) => t_new(Tid::Img, NB, a.into()), - Tag::Table(b) => t_new(Tid::Table, b.into(), NA), - Tag::Tcol(b) => t_new(Tid::Tcol, b.into(), NA), - Tag::Tpcol(b) => t_new(Tid::Tpcol, b.into(), NA), - Tag::Hr => t_new(Tid::Hr, NB, NA), - Tag::B(b) => t_new(Tid::B, b.into(), NA), - Tag::I(b) => t_new(Tid::I, b.into(), NA), - Tag::Bq(b) => t_new(Tid::Bq, b.into(), NA), - Tag::Footlnk(a) => t_new(Tid::Footlnk, NB, a.into()), - Tag::Footn(b, a) => t_new(Tid::Footn, b.into(), a.into()), - Tag::A(a) => t_new(Tid::A, NB, a.into()), - Tag::S(b) => t_new(Tid::S, b.into(), NA), - Tag::Sup(b) => t_new(Tid::Sup, b.into(), NA), - Tag::Sub(b) => t_new(Tid::Sub, 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::Carousel(b) => t_new(Tid::Carousel, b.into(), NA), - Tag::Code(b, a) => t_new(Tid::Code, b.into(), a.into()), - Tag::Pre(b) => t_new(Tid::Pre, b.into(), NA), - } - } -} - -impl From for daletl::Argument { - fn from(item: Hl) -> daletl::Argument { - 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 daletl::Argument { - fn from(item: AlignArg) -> daletl::Argument { - match item { - AlignArg::Start => NA, - AlignArg::Center => 1u8.into(), - AlignArg::End => 2u8.into(), - } - } -} - -impl From for daletl::Argument { - fn from(item: TNArg) -> daletl::Argument { - match item { - TNArg::Text(s) => s.into(), - TNArg::Null => NA, - } - } -} - -impl From for daletl::Body { - fn from(item: Body) -> daletl::Body { - match item { - Body::Null => NB, - Body::Tags(v) => v.into(), - Body::Text(v) => v.into(), - } - } -} - -impl From for daletl::Argument { - fn from(item: Arg) -> daletl::Argument { - match item { - Arg::Null => NA, - Arg::Number(v) => v.into(), - Arg::Text(v) => v.into(), - } - } -} - -impl From for daletl::Argument { - fn from(item: NNArg) -> daletl::Argument { - match item { - NNArg::Number(v) => v.into(), - NNArg::Text(v) => v.into(), - } - } -} - -impl From for daletl::Body { - fn from(item: NNBody) -> daletl::Body { - match item { - NNBody::Text(v) => v.into(), - NNBody::Tags(v) => v.into(), - } - } -} - -impl From> for daletl::Body { - fn from(item: Vec) -> daletl::Body { - daletl::Body::Tags(item.into_iter().map(|tag| tag.into()).collect()) - } -} - impl ToDaletlPage for Page { fn to_dl_page(self) -> daletl::Page { self.into_iter().map(|tag| tag.into()).collect()