feat: convert to typed from daletl

This commit is contained in:
Artemy Egorov 2024-08-05 18:43:18 +03:00
parent c121a53314
commit 5a42997045
8 changed files with 239 additions and 29 deletions

View file

@ -3,7 +3,10 @@ use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
pub type DlPage = Vec<DlTag>;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct DlPage {
pub data: Vec<DlTag>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct DlTag {
@ -79,8 +82,3 @@ pub enum DlTid {
pub trait IsNull {
fn is_null(&self) -> bool;
}
pub trait ToDaletlPage {
/// Convert to daletl Page
fn to_dl_page(self) -> DlPage;
}

View file

@ -8,13 +8,13 @@ pub fn encode(page: &DlPage) -> Result<Vec<u8>, DaletPackError> {
}
pub fn encode_no_compress(page: &DlPage) -> Result<Vec<u8>, DaletPackError> {
if page.len() > 2usize.pow(32) {
if page.data.len() > 2usize.pow(32) {
return Err(DaletPackError::PageMaxSizeExceeded);
}
let mut bv: Vec<u8> = Vec::new();
for tag in page {
for tag in &page.data {
write_tag(&mut bv, tag)?;
}

195
src/traits/from_daletl.rs Normal file
View file

@ -0,0 +1,195 @@
use crate::{
daletl::*,
typed::{
Tag::{self, *},
*,
},
};
impl TryFrom<DlTag> for Tag {
type Error = ConversionError;
fn try_from(tag: DlTag) -> Result<Self, Self::Error> {
let result = match tag.id {
DlTid::El => El(tag.body.try_into()?),
DlTid::H => H(tag.body.try_into()?, tag.argument.try_into()?),
DlTid::P => P(tag.body.try_into()?),
DlTid::Br => Br,
DlTid::Ul => Ul(tag.body.try_into()?),
DlTid::Ol => Ol(tag.body.try_into()?),
DlTid::Row => Row(tag.body.try_into()?, tag.argument.try_into()?),
DlTid::Link => Link(tag.body.try_into()?, tag.argument.try_into()?),
DlTid::Navlink => Navlink(tag.body.try_into()?, tag.argument.try_into()?),
DlTid::Btn => Btn(tag.body.try_into()?, tag.argument.try_into()?),
DlTid::Navbtn => Navbtn(tag.body.try_into()?, tag.argument.try_into()?),
DlTid::Img => Img(tag.argument.try_into()?),
DlTid::Table => Table(tag.body.try_into()?),
DlTid::Tcol => Tcol(tag.body.try_into()?),
DlTid::Tpcol => Tpcol(tag.body.try_into()?),
DlTid::Hr => Hr,
DlTid::B => B(tag.body.try_into()?),
DlTid::I => I(tag.body.try_into()?),
DlTid::Bq => Bq(tag.body.try_into()?),
DlTid::Footlnk => Footlnk(tag.argument.try_into()?),
DlTid::Footn => Footn(tag.body.try_into()?, tag.argument.try_into()?),
DlTid::A => A(tag.argument.try_into()?),
DlTid::S => S(tag.body.try_into()?),
DlTid::Sup => Sup(tag.body.try_into()?),
DlTid::Sub => Sub(tag.body.try_into()?),
DlTid::Disc => Disc(tag.body.try_into()?),
DlTid::Bl => Bl(tag.body.try_into()?, tag.argument.try_into()?),
DlTid::Carousel => Carousel(tag.body.try_into()?),
DlTid::Code => Code(tag.body.try_into()?, tag.argument.try_into()?),
DlTid::Pre => Pre(tag.body.try_into()?),
};
Ok(result)
}
}
impl TryFrom<DlArgument> for Hl {
type Error = ConversionError;
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
match value {
DlArgument::Number(n) => n.try_into().map_err(|_| ConversionError),
_ => Err(ConversionError),
}
}
}
impl TryFrom<DlArgument> for AlignArg {
type Error = ConversionError;
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
match value {
DlArgument::Number(n) => n.try_into().map_err(|_| ConversionError),
_ => Err(ConversionError),
}
}
}
impl TryFrom<DlArgument> for TNArg {
type Error = ConversionError;
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
match value {
DlArgument::Text(t) => Ok(TNArg::Text(t.into())),
DlArgument::Null => Ok(TNArg::Null),
_ => Err(ConversionError),
}
}
}
impl TryFrom<DlBody> for Body {
type Error = ConversionError;
fn try_from(value: DlBody) -> Result<Self, Self::Error> {
match value {
DlBody::Text(t) => Ok(t.into()),
DlBody::Tags(t) => Ok(Body::Tags(
t.into_iter()
.map(|dltag| dltag.try_into())
.collect::<Result<Vec<Tag>, Self::Error>>()?,
)),
DlBody::Null => Ok(Body::Null),
}
}
}
impl TryFrom<DlBody> for String {
type Error = ConversionError;
fn try_from(value: DlBody) -> Result<Self, Self::Error> {
match value {
DlBody::Text(s) => Ok(s),
_ => Err(ConversionError),
}
}
}
impl From<DlArgument> for Arg {
fn from(value: DlArgument) -> Self {
match value {
DlArgument::Text(s) => s.into(),
DlArgument::Number(n) => n.into(),
DlArgument::Null => Self::Null,
}
}
}
impl TryFrom<DlArgument> for NNArg {
type Error = ConversionError;
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
match value {
DlArgument::Text(t) => Ok(t.into()),
DlArgument::Number(n) => Ok(n.into()),
DlArgument::Null => Err(ConversionError),
}
}
}
impl TryFrom<DlArgument> for String {
type Error = ConversionError;
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
match value {
DlArgument::Text(s) => Ok(s),
_ => Err(ConversionError),
}
}
}
impl TryFrom<DlBody> for NNBody {
type Error = ConversionError;
fn try_from(value: DlBody) -> Result<Self, Self::Error> {
match value {
DlBody::Text(t) => Ok(t.into()),
DlBody::Tags(t) => Ok(NNBody::Tags(
t.into_iter()
.map(|dltag| dltag.try_into())
.collect::<Result<Vec<Tag>, Self::Error>>()?,
)),
DlBody::Null => Err(ConversionError),
}
}
}
impl TryFrom<DlBody> for Vec<Tag> {
type Error = ConversionError;
fn try_from(value: DlBody) -> Result<Self, Self::Error> {
match value {
DlBody::Tags(t) => t.into_iter().map(|dltag| dltag.try_into()).collect(),
_ => Err(ConversionError),
}
}
}
impl TryFrom<DlPage> for Vec<Tag> {
type Error = ConversionError;
fn try_from(value: DlPage) -> Result<Self, Self::Error> {
value
.data
.into_iter()
.map(|dltag| dltag.try_into())
.collect()
}
}
impl TryFrom<DlPage> for Page {
type Error = ConversionError;
fn try_from(value: DlPage) -> Result<Self, Self::Error> {
Ok(Self {
data: value
.data
.into_iter()
.map(|dltag| dltag.try_into())
.collect::<Result<Vec<Tag>, Self::Error>>()?,
})
}
}

View file

@ -116,26 +116,18 @@ impl From<Vec<Tag>> for DlBody {
}
}
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 From<Vec<Tag>> for DlPage {
fn from(value: Vec<Tag>) -> Self {
Self {
data: value.into_iter().map(|t| t.into()).collect(),
}
}
}
impl IsNull for DlArgument {
fn is_null(&self) -> bool {
match self {
Self::Null => true,
_ => false,
impl From<Page> for DlPage {
fn from(value: Page) -> Self {
Self {
data: value.data.into_iter().map(|t| t.into()).collect(),
}
}
}

View file

@ -0,0 +1,19 @@
use crate::daletl::{DlArgument, DlBody, IsNull};
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 +1,3 @@
mod typed;
mod from_daletl;
mod from_typed;
mod is_null_daletl;

View file

@ -1,7 +1,12 @@
use enum_procs::AutoFrom;
use num_enum::TryFromPrimitive;
pub type Page = Vec<Tag>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Page {
pub data: Vec<Tag>,
}
pub struct ConversionError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Tag {

View file

@ -1,5 +1,4 @@
use dalet::{
daletl::ToDaletlPage,
daletpack::*,
typed::{Hl, Page, TNArg, Tag::*},
};
@ -32,7 +31,7 @@ pub fn compress_zlib(data: &Vec<u8>) -> std::io::Result<Vec<u8>> {
#[test]
fn bench() {
let page: Page = vec![
let page = vec![
H("I am heading".into(), Hl::One),
H("Heading 2".into(), Hl::Two),
P(vec![
@ -78,7 +77,7 @@ fn bench() {
]),
];
let dalet_page = page.to_dl_page();
let dalet_page = page.into();
let markdown = iprint!("Markdown", include_str!("./bench.md").as_bytes().to_vec());
let daletpack = iprint!("Daletpack", encode_no_compress(&dalet_page).unwrap());