refactor: move traits to module

This commit is contained in:
Artemy Egorov 2024-08-05 15:34:39 +03:00
parent d79fbffa7a
commit c121a53314
6 changed files with 150 additions and 149 deletions

View file

@ -3,12 +3,7 @@ use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr}; use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::typed::*; pub type DlPage = Vec<DlTag>;
const NB: DlBody = DlBody::Null;
const NA: DlArgument = DlArgument::Null;
pub type Page = Vec<DlTag>;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct DlTag { pub struct DlTag {
@ -45,24 +40,6 @@ pub enum DlArgument {
Null, 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)] #[derive(Serialize_repr, Deserialize_repr, Debug, Clone, PartialEq, Eq, TryFromPrimitive, Copy)]
#[repr(u8)] #[repr(u8)]
/// Tag Id /// Tag Id
@ -105,118 +82,5 @@ pub trait IsNull {
pub trait ToDaletlPage { pub trait ToDaletlPage {
/// Convert to daletl Page /// Convert to daletl Page
fn to_dl_page(self) -> Page; fn to_dl_page(self) -> DlPage;
}
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,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}; use super::{utils, DaletPackError, TypeId};
pub fn encode(page: &Page) -> Result<Vec<u8>, DaletPackError> { pub fn encode(page: &DlPage) -> Result<Vec<u8>, DaletPackError> {
Ok(utils::compress_zstd(&encode_no_compress(page)?) Ok(utils::compress_zstd(&encode_no_compress(page)?)
.map_err(|_| DaletPackError::ZstdCompressError)?) .map_err(|_| DaletPackError::ZstdCompressError)?)
} }
pub fn encode_no_compress(page: &Page) -> Result<Vec<u8>, DaletPackError> { pub fn encode_no_compress(page: &DlPage) -> Result<Vec<u8>, DaletPackError> {
if page.len() > 2usize.pow(32) { if page.len() > 2usize.pow(32) {
return Err(DaletPackError::PageMaxSizeExceeded); return Err(DaletPackError::PageMaxSizeExceeded);
} }

View file

@ -4,6 +4,9 @@ pub mod daletl;
#[cfg(feature = "types")] #[cfg(feature = "types")]
pub mod typed; pub mod typed;
#[cfg(feature = "types")]
mod traits;
#[cfg(feature = "daletpack")] #[cfg(feature = "daletpack")]
pub mod daletpack; pub mod daletpack;

1
src/traits/mod.rs Normal file
View file

@ -0,0 +1 @@
mod typed;

141
src/traits/typed.rs Normal file
View file

@ -0,0 +1,141 @@
use crate::{daletl::*, typed::*};
const NB: DlBody = DlBody::Null;
const NA: DlArgument = DlArgument::Null;
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())
}
}
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,
}
}
}

View file

@ -1,8 +1,6 @@
use enum_procs::AutoFrom; use enum_procs::AutoFrom;
use num_enum::TryFromPrimitive; use num_enum::TryFromPrimitive;
use crate::daletl::{self, ToDaletlPage};
pub type Page = Vec<Tag>; pub type Page = Vec<Tag>;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -97,9 +95,3 @@ pub enum Hl {
Five, Five,
Six, Six,
} }
impl ToDaletlPage for Page {
fn to_dl_page(self) -> daletl::Page {
self.into_iter().map(|tag| tag.into()).collect()
}
}