refactor: move traits from typed to daletl

This commit is contained in:
Artemy Egorov 2024-08-05 15:03:25 +03:00
parent 8a3f0932eb
commit d79fbffa7a
3 changed files with 147 additions and 145 deletions

View file

@ -3,44 +3,49 @@ use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
pub type Page = Vec<Tag>;
use crate::typed::*;
const NB: DlBody = DlBody::Null;
const NA: DlArgument = DlArgument::Null;
pub type Page = Vec<DlTag>;
#[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<Tag>),
Tags(Vec<DlTag>),
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<Tag> 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<Hl> 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<AlignArg> for DlArgument {
fn from(item: AlignArg) -> DlArgument {
match item {
AlignArg::Start => NA,
AlignArg::Center => 1u8.into(),
AlignArg::End => 2u8.into(),
}
}
}
impl From<TNArg> for DlArgument {
fn from(item: TNArg) -> DlArgument {
match item {
TNArg::Text(s) => s.into(),
TNArg::Null => NA,
}
}
}
impl From<Body> for DlBody {
fn from(item: Body) -> DlBody {
match item {
Body::Null => NB,
Body::Tags(v) => v.into(),
Body::Text(v) => v.into(),
}
}
}
impl From<Arg> for DlArgument {
fn from(item: Arg) -> DlArgument {
match item {
Arg::Null => NA,
Arg::Number(v) => v.into(),
Arg::Text(v) => v.into(),
}
}
}
impl From<NNArg> for DlArgument {
fn from(item: NNArg) -> DlArgument {
match item {
NNArg::Number(v) => v.into(),
NNArg::Text(v) => v.into(),
}
}
}
impl From<NNBody> for DlBody {
fn from(item: NNBody) -> DlBody {
match item {
NNBody::Text(v) => v.into(),
NNBody::Tags(v) => v.into(),
}
}
}
impl From<Vec<Tag>> for DlBody {
fn from(item: Vec<Tag>) -> DlBody {
DlBody::Tags(item.into_iter().map(|tag| tag.into()).collect())
}
}

View file

@ -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<u8>, string: &String) -> Result<(), DaletPackError> {
Ok(())
}
fn write_array(bv: &mut Vec<u8>, arr: &Vec<Tag>) -> Result<(), DaletPackError> {
fn write_array(bv: &mut Vec<u8>, arr: &Vec<DlTag>) -> Result<(), DaletPackError> {
if arr.len() > 2usize.pow(32) {
return Err(DaletPackError::ArrMaxSizeExceeded);
}
@ -65,8 +65,8 @@ fn write_array(bv: &mut Vec<u8>, arr: &Vec<Tag>) -> Result<(), DaletPackError> {
Ok(())
}
fn write_tag(bv: &mut Vec<u8>, tag: &Tag) -> Result<(), DaletPackError> {
if tag.id == Tid::El {
fn write_tag(bv: &mut Vec<u8>, 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<u8>, tag: &Tag) -> Result<(), DaletPackError> {
Ok(())
}
fn write_tag_body(bv: &mut Vec<u8>, body: &Body) -> Result<(), DaletPackError> {
fn write_tag_body(bv: &mut Vec<u8>, 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<u8>, argument: &Argument) -> Result<(), DaletPackError> {
fn write_tag_argument(bv: &mut Vec<u8>, 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(())

View file

@ -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<Tag>;
@ -101,119 +98,6 @@ pub enum Hl {
Six,
}
impl From<Tag> 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<Hl> 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<AlignArg> for daletl::Argument {
fn from(item: AlignArg) -> daletl::Argument {
match item {
AlignArg::Start => NA,
AlignArg::Center => 1u8.into(),
AlignArg::End => 2u8.into(),
}
}
}
impl From<TNArg> for daletl::Argument {
fn from(item: TNArg) -> daletl::Argument {
match item {
TNArg::Text(s) => s.into(),
TNArg::Null => NA,
}
}
}
impl From<Body> 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<Arg> 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<NNArg> for daletl::Argument {
fn from(item: NNArg) -> daletl::Argument {
match item {
NNArg::Number(v) => v.into(),
NNArg::Text(v) => v.into(),
}
}
}
impl From<NNBody> for daletl::Body {
fn from(item: NNBody) -> daletl::Body {
match item {
NNBody::Text(v) => v.into(),
NNBody::Tags(v) => v.into(),
}
}
}
impl From<Vec<Tag>> for daletl::Body {
fn from(item: Vec<Tag>) -> 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()