mirror of
https://github.com/TxtDot/dalet-rs.git
synced 2024-11-05 17:33:58 +03:00
fix: update daletpack encode format
This commit is contained in:
parent
f6195aa708
commit
54c594d3f6
9 changed files with 303 additions and 217 deletions
|
@ -42,8 +42,8 @@ br
|
|||
# if no tag is specified but a '{- text}' is present, then the 'p' tag is placed
|
||||
# '\n' is replaced with ' ' in this format.
|
||||
{-
|
||||
Check Dalet too
|
||||
This is one paragraph
|
||||
Check Dalet too,
|
||||
this is one paragraph
|
||||
}
|
||||
|
||||
{-
|
||||
|
@ -63,7 +63,7 @@ row "center" [
|
|||
row [
|
||||
# if no tag is specified but a '[[]]' is present, then the 'el' tag
|
||||
# with multiple tags body placed
|
||||
|
||||
|
||||
[[
|
||||
h2: Features
|
||||
|
||||
|
@ -92,6 +92,10 @@ row [
|
|||
npm run dev
|
||||
}
|
||||
|
||||
code "markdown" {~4
|
||||
this is codeblock
|
||||
}
|
||||
|
||||
# {# Text} Text after "{#" not modified
|
||||
code "markdown" {# this is codeblock}
|
||||
]]
|
||||
|
@ -122,13 +126,13 @@ row [
|
|||
# Description
|
||||
# ]
|
||||
{> table
|
||||
[[ Tag | Description ]]
|
||||
[ h | Heading ]
|
||||
[ p | Paragraph ]
|
||||
[ img | Image ]
|
||||
[ link | Link ]
|
||||
[ btn | Button ]
|
||||
[ ul | Unordered list ]
|
||||
[ br | Line break ]
|
||||
[[ quantity | 7 ]]
|
||||
[[ Tag | Description | id ]]
|
||||
[ h | Heading | 0 ]
|
||||
[ p | Paragraph | 1 ]
|
||||
[ img | Image | 2 ]
|
||||
[ link | Link | 3 ]
|
||||
[ btn | Button | 4 ]
|
||||
[ ul | Unordered list | 5 ]
|
||||
[ br | Line break | 6 ]
|
||||
[[ quantity | 7 | 7 ]]
|
||||
}
|
||||
|
|
|
@ -14,25 +14,19 @@ use super::types::Span;
|
|||
|
||||
pub fn table_to_tag(rows: &Vec<TableCol>) -> Tag {
|
||||
Tag::Table(
|
||||
rows.into_iter()
|
||||
rows.iter()
|
||||
.map(|row| match row {
|
||||
TableCol::Primary(row) => Tag::Tprow(
|
||||
row.into_iter()
|
||||
row.iter()
|
||||
.map(|t| {
|
||||
Tag::El(NNBody::Text(format!(
|
||||
"{}",
|
||||
t.replace("\\]", "]").replace("\\|", "|")
|
||||
)))
|
||||
Tag::El(NNBody::Text(t.replace("\\]", "]").replace("\\|", "|").to_string()))
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
TableCol::Secondary(row) => Tag::Trow(
|
||||
row.into_iter()
|
||||
row.iter()
|
||||
.map(|t| {
|
||||
Tag::El(NNBody::Text(format!(
|
||||
"{}",
|
||||
t.replace("\\]", "]").replace("\\|", "|")
|
||||
)))
|
||||
Tag::El(NNBody::Text(t.replace("\\]", "]").replace("\\|", "|").to_string()))
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
|
|
|
@ -52,7 +52,7 @@ fn additional_str<'src>(
|
|||
""
|
||||
}
|
||||
|
||||
pub fn format<'src>(spanned_tokens: &Vec<Spanned<Token<'src>>>) -> String {
|
||||
pub fn format(spanned_tokens: &Vec<Spanned<Token<'_>>>) -> String {
|
||||
let mut current_indent: usize = 0;
|
||||
let mut formatted = String::new();
|
||||
let len = spanned_tokens.len();
|
||||
|
@ -162,7 +162,7 @@ pub fn format<'src>(spanned_tokens: &Vec<Spanned<Token<'src>>>) -> String {
|
|||
prepend_indent("}", current_indent)
|
||||
),
|
||||
|
||||
Token::EmptyLine => "\n".to_owned(),
|
||||
Token::EmptyLine => prepend_indent("\n", current_indent),
|
||||
};
|
||||
|
||||
formatted.push_str(&to_push);
|
||||
|
|
|
@ -134,7 +134,6 @@ fn textual<'src>() -> impl Parser<'src, &'src str, Token<'src>, extra::Err<Rich<
|
|||
.labelled("Body of multiline text");
|
||||
|
||||
let paragraph = multiline_text_body
|
||||
.clone()
|
||||
.delimited_by(just("{-"), just("}"))
|
||||
.map(Token::Paragraph)
|
||||
.labelled("Paragraph syntax");
|
||||
|
@ -145,7 +144,6 @@ fn textual<'src>() -> impl Parser<'src, &'src str, Token<'src>, extra::Err<Rich<
|
|||
.labelled("Table syntax");
|
||||
|
||||
let mltext = multiline_text_body
|
||||
.clone()
|
||||
.delimited_by(just('{'), just('}'))
|
||||
.map(Token::MLText)
|
||||
.labelled("Multiline text");
|
||||
|
@ -156,7 +154,7 @@ fn textual<'src>() -> impl Parser<'src, &'src str, Token<'src>, extra::Err<Rich<
|
|||
.labelled("Minimum spaces number");
|
||||
|
||||
mlms_n
|
||||
.then(multiline_text_body.clone())
|
||||
.then(multiline_text_body)
|
||||
.then_ignore(just("}"))
|
||||
.map(|(n, t)| Token::MLMSText(n, t))
|
||||
.labelled("Multi line text with min spaces")
|
||||
|
|
|
@ -84,7 +84,6 @@ pub fn tag<'tokens, 'src: 'tokens>(
|
|||
.labelled("Heading level argument");
|
||||
|
||||
let alignarg = text_arg
|
||||
.clone()
|
||||
.validate(|t, e, emmiter| match t.as_str() {
|
||||
"start" => AlignArg::Start,
|
||||
"center" => AlignArg::Center,
|
||||
|
@ -110,42 +109,42 @@ pub fn tag<'tokens, 'src: 'tokens>(
|
|||
let ul = just(Token::Ul).ignore_then(tags_body.clone()).map(Ul);
|
||||
let ol = just(Token::Ol).ignore_then(tags_body.clone()).map(Ol);
|
||||
let row = just(Token::Row)
|
||||
.ignore_then(alignarg.clone().or_not())
|
||||
.ignore_then(alignarg.or_not())
|
||||
.then(tags_body.clone())
|
||||
.map(|(arg, body)| Row(body, arg.unwrap_or(AlignArg::Start)));
|
||||
let link = just(Token::Link)
|
||||
.ignore_then(text_arg.clone())
|
||||
.ignore_then(text_arg)
|
||||
.then(body.clone())
|
||||
.map(|(arg, body)| Link(body, arg));
|
||||
let navlink = just(Token::Navlink)
|
||||
.ignore_then(text_arg.clone())
|
||||
.ignore_then(text_arg)
|
||||
.then(body.clone())
|
||||
.map(|(arg, body)| Navlink(body, arg));
|
||||
let btn = just(Token::Btn)
|
||||
.ignore_then(text_arg.clone())
|
||||
.ignore_then(text_arg)
|
||||
.then(body.clone())
|
||||
.map(|(arg, body)| Btn(body, arg));
|
||||
let navbtn = just(Token::Navbtn)
|
||||
.ignore_then(text_arg.clone())
|
||||
.ignore_then(text_arg)
|
||||
.then(body.clone())
|
||||
.map(|(arg, body)| Navbtn(body, arg));
|
||||
let img = just(Token::Img).ignore_then(text_arg.clone()).map(Img);
|
||||
let img = just(Token::Img).ignore_then(text_arg).map(Img);
|
||||
let table = just(Token::Table).ignore_then(tags_body.clone()).map(Table);
|
||||
let trow = just(Token::Trow).ignore_then(tags_body.clone()).map(Trow);
|
||||
let tprow = just(Token::Tprow).ignore_then(tags_body.clone()).map(Tprow);
|
||||
let hr = just(Token::Hr).to(Hr);
|
||||
let b = just(Token::B).ignore_then(text_body.clone()).map(B);
|
||||
let i = just(Token::I).ignore_then(text_body.clone()).map(I);
|
||||
let b = just(Token::B).ignore_then(text_body).map(B);
|
||||
let i = just(Token::I).ignore_then(text_body).map(I);
|
||||
let bq = just(Token::Bq).ignore_then(nnbody.clone()).map(Bq);
|
||||
let footlnk = just(Token::Footlnk).ignore_then(nnarg).map(Footlnk);
|
||||
let footn = just(Token::Footn)
|
||||
.ignore_then(nnarg.clone())
|
||||
.then(text_body.clone())
|
||||
.ignore_then(nnarg)
|
||||
.then(text_body)
|
||||
.map(|(arg, body)| Footn(body, arg));
|
||||
let a = just(Token::A).ignore_then(nnarg.clone()).map(A);
|
||||
let s = just(Token::S).ignore_then(text_body.clone()).map(S);
|
||||
let sup = just(Token::Sup).ignore_then(text_body.clone()).map(Sup);
|
||||
let sub = just(Token::Sub).ignore_then(text_body.clone()).map(Sub);
|
||||
let a = just(Token::A).ignore_then(nnarg).map(A);
|
||||
let s = just(Token::S).ignore_then(text_body).map(S);
|
||||
let sup = just(Token::Sup).ignore_then(text_body).map(Sup);
|
||||
let sub = just(Token::Sub).ignore_then(text_body).map(Sub);
|
||||
let disc = just(Token::Disc).ignore_then(nnbody.clone()).map(Disc);
|
||||
let block = just(Token::Block)
|
||||
.ignore_then(alignarg.or_not())
|
||||
|
@ -156,12 +155,12 @@ pub fn tag<'tokens, 'src: 'tokens>(
|
|||
.map(Carousel);
|
||||
let code = just(Token::Code)
|
||||
.ignore_then(tnullarg)
|
||||
.then(text_body.clone())
|
||||
.then(text_body)
|
||||
.map(|(arg, body)| Code(body, arg));
|
||||
let pre = just(Token::Pre).ignore_then(text_body.clone()).map(Pre);
|
||||
let pre = just(Token::Pre).ignore_then(text_body).map(Pre);
|
||||
let meta = just(Token::Meta)
|
||||
.ignore_then(text_arg)
|
||||
.then(text_body.clone())
|
||||
.then(text_body)
|
||||
.map(|(arg, body)| Meta(body, arg));
|
||||
|
||||
let el_text = select! {
|
||||
|
@ -175,7 +174,7 @@ pub fn tag<'tokens, 'src: 'tokens>(
|
|||
.map(|v| El(NNBody::Tags(v)));
|
||||
|
||||
let paragraph = select! {
|
||||
Token::Paragraph(t) => P(NNBody::Text(t.replace("\n", " ").trim().to_owned()))
|
||||
Token::Paragraph(t) => P(NNBody::Text(t.replace('\n', " ").trim().to_owned()))
|
||||
}
|
||||
.labelled("Paragraph");
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ pub fn set_spaces(input: &str, spaces: usize) -> String {
|
|||
prepend_spaces(&trim_indent(input), spaces)
|
||||
}
|
||||
|
||||
fn trim_unused<'a>(s: &'a str) -> &'a str {
|
||||
fn trim_unused(s: &str) -> &str {
|
||||
let mut trim_start = 0;
|
||||
let mut been_newlines = false;
|
||||
|
||||
|
@ -50,7 +50,7 @@ fn trim_unused<'a>(s: &'a str) -> &'a str {
|
|||
}
|
||||
}
|
||||
|
||||
&s[(trim_start)..].trim_end()
|
||||
s[(trim_start)..].trim_end()
|
||||
}
|
||||
|
||||
pub fn prepend_indent(input: &str, indent: usize) -> String {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid};
|
||||
use crate::daletl::{DlPage, DlTag};
|
||||
|
||||
use super::{utils, DaletPackDecodeError, TypeId};
|
||||
use super::{utils, DaletPackDecodeError};
|
||||
|
||||
pub struct Decoder<'a> {
|
||||
data: Box<dyn Iterator<Item = u8> + 'a>,
|
||||
|
@ -16,155 +16,155 @@ impl<'a> Decoder<'a> {
|
|||
}
|
||||
|
||||
pub fn decode(&mut self) -> Result<DlPage, DaletPackDecodeError> {
|
||||
let mut array: Vec<DlTag> = Vec::new();
|
||||
let array: Vec<DlTag> = Vec::new();
|
||||
|
||||
for _ in 0..u32::MAX {
|
||||
let typeid = self.data.next();
|
||||
// for _ in 0..u32::MAX {
|
||||
// let typeid = self.data.next();
|
||||
|
||||
match typeid {
|
||||
Some(typeid) => match typeid.try_into()? {
|
||||
TypeId::Text => array.push(DlTag::new(
|
||||
DlTid::El,
|
||||
self.read_text()?.into(),
|
||||
DlArgument::Null,
|
||||
)),
|
||||
TypeId::Tags => array.push(DlTag::new(
|
||||
DlTid::El,
|
||||
self.read_tag_array()?.into(),
|
||||
DlArgument::Null,
|
||||
)),
|
||||
TypeId::TagId => array.push(self.read_tag_with_id()?),
|
||||
TypeId::TagIdBody => array.push(self.read_tag_with_id_body()?),
|
||||
TypeId::TagIdArgument => array.push(self.read_tag_with_id_argument()?),
|
||||
TypeId::TagIdBodyArgument => array.push(self.read_full_tag()?),
|
||||
// match typeid {
|
||||
// Some(typeid) => match typeid.try_into()? {
|
||||
// TypeId::Text => array.push(DlTag::new(
|
||||
// DlTid::El,
|
||||
// self.read_text()?.into(),
|
||||
// DlArgument::Null,
|
||||
// )),
|
||||
// TypeId::Tags => array.push(DlTag::new(
|
||||
// DlTid::El,
|
||||
// self.read_tag_array()?.into(),
|
||||
// DlArgument::Null,
|
||||
// )),
|
||||
// TypeId::TagId => array.push(self.read_tag_with_id()?),
|
||||
// TypeId::TagIdBody => array.push(self.read_tag_with_id_body()?),
|
||||
// TypeId::TagIdArgument => array.push(self.read_tag_with_id_argument()?),
|
||||
// TypeId::TagIdBodyArgument => array.push(self.read_full_tag()?),
|
||||
|
||||
_ => Err(DaletPackDecodeError::InvalidSchema)?,
|
||||
},
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
// _ => Err(DaletPackDecodeError::InvalidSchema)?,
|
||||
// },
|
||||
// None => break,
|
||||
// }
|
||||
// }
|
||||
|
||||
Ok(DlPage { data: array })
|
||||
}
|
||||
|
||||
pub fn read_body(&mut self) -> Result<DlBody, DaletPackDecodeError> {
|
||||
let typeid: TypeId = self
|
||||
.data
|
||||
.next()
|
||||
.ok_or(DaletPackDecodeError::InvalidSchema)?
|
||||
.try_into()?;
|
||||
// pub fn read_body(&mut self) -> Result<DlBody, DaletPackDecodeError> {
|
||||
// let typeid: TypeId = self
|
||||
// .data
|
||||
// .next()
|
||||
// .ok_or(DaletPackDecodeError::InvalidSchema)?
|
||||
// .try_into()?;
|
||||
|
||||
let value = match typeid {
|
||||
TypeId::Text => DlBody::Text(self.read_text()?),
|
||||
TypeId::Tags => DlBody::Tags(self.read_tag_array()?),
|
||||
_ => Err(DaletPackDecodeError::InvalidArgument)?,
|
||||
};
|
||||
// let value = match typeid {
|
||||
// TypeId::Text => DlBody::Text(self.read_text()?),
|
||||
// TypeId::Tags => DlBody::Tags(self.read_tag_array()?),
|
||||
// _ => Err(DaletPackDecodeError::InvalidArgument)?,
|
||||
// };
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
// Ok(value)
|
||||
// }
|
||||
|
||||
pub fn read_arg(&mut self) -> Result<DlArgument, DaletPackDecodeError> {
|
||||
let typeid: TypeId = self
|
||||
.data
|
||||
.next()
|
||||
.ok_or(DaletPackDecodeError::InvalidSchema)?
|
||||
.try_into()?;
|
||||
// pub fn read_arg(&mut self) -> Result<DlArgument, DaletPackDecodeError> {
|
||||
// let typeid: TypeId = self
|
||||
// .data
|
||||
// .next()
|
||||
// .ok_or(DaletPackDecodeError::InvalidSchema)?
|
||||
// .try_into()?;
|
||||
|
||||
let value = match typeid {
|
||||
TypeId::Text => DlArgument::Text(self.read_text()?),
|
||||
TypeId::Number => DlArgument::Number(self.read_number()?),
|
||||
_ => Err(DaletPackDecodeError::InvalidArgument)?,
|
||||
};
|
||||
// let value = match typeid {
|
||||
// TypeId::Text => DlArgument::Text(self.read_text()?),
|
||||
// TypeId::Number => DlArgument::Number(self.read_number()?),
|
||||
// _ => Err(DaletPackDecodeError::InvalidArgument)?,
|
||||
// };
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
// Ok(value)
|
||||
// }
|
||||
|
||||
fn read_number(&mut self) -> Result<u8, DaletPackDecodeError> {
|
||||
self.data.next().ok_or(DaletPackDecodeError::InvalidSchema)
|
||||
}
|
||||
// fn read_number(&mut self) -> Result<u8, DaletPackDecodeError> {
|
||||
// self.data.next().ok_or(DaletPackDecodeError::InvalidSchema)
|
||||
// }
|
||||
|
||||
fn read_text(&mut self) -> Result<String, DaletPackDecodeError> {
|
||||
let mut str = String::new();
|
||||
// fn read_text(&mut self) -> Result<String, DaletPackDecodeError> {
|
||||
// let mut str = String::new();
|
||||
|
||||
for _ in 0..u32::MAX {
|
||||
let val = self
|
||||
.data
|
||||
.next()
|
||||
.ok_or(DaletPackDecodeError::InvalidTextSchema)?;
|
||||
// for _ in 0..u32::MAX {
|
||||
// let val = self
|
||||
// .data
|
||||
// .next()
|
||||
// .ok_or(DaletPackDecodeError::InvalidTextSchema)?;
|
||||
|
||||
if val == TypeId::TextEnd as u8 {
|
||||
break;
|
||||
}
|
||||
// if val == TypeId::TextEnd as u8 {
|
||||
// break;
|
||||
// }
|
||||
|
||||
str.push(val as char);
|
||||
}
|
||||
// str.push(val as char);
|
||||
// }
|
||||
|
||||
Ok(str)
|
||||
}
|
||||
// Ok(str)
|
||||
// }
|
||||
|
||||
fn read_tag_array(&mut self) -> Result<Vec<DlTag>, DaletPackDecodeError> {
|
||||
let mut array = Vec::new();
|
||||
// fn read_tag_array(&mut self) -> Result<Vec<DlTag>, DaletPackDecodeError> {
|
||||
// let mut array = Vec::new();
|
||||
|
||||
for _ in 0..u32::MAX {
|
||||
let typeid: TypeId = self
|
||||
.data
|
||||
.next()
|
||||
.ok_or(DaletPackDecodeError::InvalidTagsSchema)?
|
||||
.try_into()?;
|
||||
// for _ in 0..u32::MAX {
|
||||
// let typeid: TypeId = self
|
||||
// .data
|
||||
// .next()
|
||||
// .ok_or(DaletPackDecodeError::InvalidTagsSchema)?
|
||||
// .try_into()?;
|
||||
|
||||
match typeid {
|
||||
TypeId::Text => array.push(DlTag::new(
|
||||
DlTid::El,
|
||||
self.read_text()?.into(),
|
||||
DlArgument::Null,
|
||||
)),
|
||||
TypeId::Tags => array.push(DlTag::new(
|
||||
DlTid::El,
|
||||
self.read_tag_array()?.into(),
|
||||
DlArgument::Null,
|
||||
)),
|
||||
TypeId::TagId => array.push(self.read_tag_with_id()?),
|
||||
TypeId::TagIdBody => array.push(self.read_tag_with_id_body()?),
|
||||
TypeId::TagIdArgument => array.push(self.read_tag_with_id_argument()?),
|
||||
TypeId::TagIdBodyArgument => array.push(self.read_full_tag()?),
|
||||
// match typeid {
|
||||
// TypeId::Text => array.push(DlTag::new(
|
||||
// DlTid::El,
|
||||
// self.read_text()?.into(),
|
||||
// DlArgument::Null,
|
||||
// )),
|
||||
// TypeId::Tags => array.push(DlTag::new(
|
||||
// DlTid::El,
|
||||
// self.read_tag_array()?.into(),
|
||||
// DlArgument::Null,
|
||||
// )),
|
||||
// TypeId::TagId => array.push(self.read_tag_with_id()?),
|
||||
// TypeId::TagIdBody => array.push(self.read_tag_with_id_body()?),
|
||||
// TypeId::TagIdArgument => array.push(self.read_tag_with_id_argument()?),
|
||||
// TypeId::TagIdBodyArgument => array.push(self.read_full_tag()?),
|
||||
|
||||
TypeId::TagsEnd => break,
|
||||
_ => Err(DaletPackDecodeError::InvalidSchema)?,
|
||||
}
|
||||
}
|
||||
// TypeId::TagsEnd => break,
|
||||
// _ => Err(DaletPackDecodeError::InvalidSchema)?,
|
||||
// }
|
||||
// }
|
||||
|
||||
Ok(array)
|
||||
}
|
||||
// Ok(array)
|
||||
// }
|
||||
|
||||
fn read_tag_with_id(&mut self) -> Result<DlTag, DaletPackDecodeError> {
|
||||
Ok(DlTag::new(
|
||||
self.read_number()?.try_into()?,
|
||||
DlBody::Null,
|
||||
DlArgument::Null,
|
||||
))
|
||||
}
|
||||
// fn read_tag_with_id(&mut self) -> Result<DlTag, DaletPackDecodeError> {
|
||||
// Ok(DlTag::new(
|
||||
// self.read_number()?.try_into()?,
|
||||
// DlBody::Null,
|
||||
// DlArgument::Null,
|
||||
// ))
|
||||
// }
|
||||
|
||||
fn read_tag_with_id_body(&mut self) -> Result<DlTag, DaletPackDecodeError> {
|
||||
Ok(DlTag::new(
|
||||
self.read_number()?.try_into()?,
|
||||
self.read_body()?,
|
||||
DlArgument::Null,
|
||||
))
|
||||
}
|
||||
// fn read_tag_with_id_body(&mut self) -> Result<DlTag, DaletPackDecodeError> {
|
||||
// Ok(DlTag::new(
|
||||
// self.read_number()?.try_into()?,
|
||||
// self.read_body()?,
|
||||
// DlArgument::Null,
|
||||
// ))
|
||||
// }
|
||||
|
||||
fn read_tag_with_id_argument(&mut self) -> Result<DlTag, DaletPackDecodeError> {
|
||||
Ok(DlTag::new(
|
||||
self.read_number()?.try_into()?,
|
||||
DlBody::Null,
|
||||
self.read_arg()?,
|
||||
))
|
||||
}
|
||||
// fn read_tag_with_id_argument(&mut self) -> Result<DlTag, DaletPackDecodeError> {
|
||||
// Ok(DlTag::new(
|
||||
// self.read_number()?.try_into()?,
|
||||
// DlBody::Null,
|
||||
// self.read_arg()?,
|
||||
// ))
|
||||
// }
|
||||
|
||||
fn read_full_tag(&mut self) -> Result<DlTag, DaletPackDecodeError> {
|
||||
Ok(DlTag::new(
|
||||
self.read_number()?.try_into()?,
|
||||
self.read_body()?,
|
||||
self.read_arg()?,
|
||||
))
|
||||
}
|
||||
// fn read_full_tag(&mut self) -> Result<DlTag, DaletPackDecodeError> {
|
||||
// Ok(DlTag::new(
|
||||
// self.read_number()?.try_into()?,
|
||||
// self.read_body()?,
|
||||
// self.read_arg()?,
|
||||
// ))
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid, IsNull};
|
||||
use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid};
|
||||
|
||||
use super::{utils, DaletPackError, TypeId};
|
||||
|
||||
pub fn encode(page: &DlPage) -> Result<Vec<u8>, DaletPackError> {
|
||||
utils::compress_zstd(&encode_no_compress(page)?)
|
||||
.map_err(|_| DaletPackError::ZstdCompressError)
|
||||
utils::compress_zstd(&encode_no_compress(page)?).map_err(|_| DaletPackError::ZstdCompressError)
|
||||
}
|
||||
|
||||
pub fn encode_no_compress(page: &DlPage) -> Result<Vec<u8>, DaletPackError> {
|
||||
|
@ -21,11 +20,6 @@ pub fn encode_no_compress(page: &DlPage) -> Result<Vec<u8>, DaletPackError> {
|
|||
Ok(bv)
|
||||
}
|
||||
|
||||
fn write_int(bv: &mut Vec<u8>, n: u8) {
|
||||
bv.push(TypeId::Number as u8);
|
||||
bv.push(n);
|
||||
}
|
||||
|
||||
fn write_str(bv: &mut Vec<u8>, string: &String) -> Result<(), DaletPackError> {
|
||||
let size = string.len();
|
||||
|
||||
|
@ -33,7 +27,6 @@ fn write_str(bv: &mut Vec<u8>, string: &String) -> Result<(), DaletPackError> {
|
|||
return Err(DaletPackError::StrMaxSizeExceeded);
|
||||
}
|
||||
|
||||
bv.push(TypeId::Text as u8);
|
||||
bv.extend_from_slice(string.as_bytes());
|
||||
bv.push(TypeId::TextEnd as u8);
|
||||
|
||||
|
@ -45,37 +38,113 @@ fn write_array(bv: &mut Vec<u8>, arr: &Vec<DlTag>) -> Result<(), DaletPackError>
|
|||
return Err(DaletPackError::ArrMaxSizeExceeded);
|
||||
}
|
||||
|
||||
bv.push(TypeId::Tags as u8);
|
||||
|
||||
for tag in arr {
|
||||
write_tag(bv, tag)?;
|
||||
}
|
||||
|
||||
bv.push(TypeId::TagsEnd as u8);
|
||||
if arr.len() != 1 {
|
||||
bv.push(TypeId::TagsEnd as u8);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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);
|
||||
bv.push(tag.id as u8);
|
||||
} else if tag.argument.is_null() {
|
||||
bv.push(TypeId::TagIdBody as u8);
|
||||
bv.push(tag.id as u8);
|
||||
write_tag_body(bv, &tag.body)?;
|
||||
} else if tag.body.is_null() {
|
||||
bv.push(TypeId::TagIdArgument as u8);
|
||||
bv.push(tag.id as u8);
|
||||
write_tag_argument(bv, &tag.argument)?;
|
||||
} else {
|
||||
bv.push(TypeId::TagIdBodyArgument as u8);
|
||||
bv.push(tag.id as u8);
|
||||
write_tag_body(bv, &tag.body)?;
|
||||
write_tag_argument(bv, &tag.argument)?;
|
||||
}
|
||||
// TypeId and TagId if needed
|
||||
match (&tag.body, &tag.argument) {
|
||||
(DlBody::Text(_), DlArgument::Text(_)) => match &tag.id {
|
||||
DlTid::Meta => bv.push(TypeId::Meta as u8),
|
||||
_ => {
|
||||
bv.push(TypeId::CompTextText as u8);
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
},
|
||||
(DlBody::Text(_), DlArgument::Number(_)) => {
|
||||
bv.push(TypeId::CompTextNumber as u8);
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
(DlBody::Text(_), DlArgument::Null) => match &tag.id {
|
||||
DlTid::El => bv.push(TypeId::ElText as u8),
|
||||
DlTid::P => bv.push(TypeId::PText as u8),
|
||||
DlTid::B => bv.push(TypeId::B as u8),
|
||||
DlTid::I => bv.push(TypeId::I as u8),
|
||||
DlTid::S => bv.push(TypeId::S as u8),
|
||||
DlTid::Sup => bv.push(TypeId::Sup as u8),
|
||||
DlTid::Sub => bv.push(TypeId::Sub as u8),
|
||||
|
||||
_ => {
|
||||
bv.push(TypeId::BodyText as u8);
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
},
|
||||
(DlBody::Tags(tags), DlArgument::Text(_)) => {
|
||||
if tags.len() == 1 {
|
||||
bv.push(TypeId::CompTagText as u8);
|
||||
} else {
|
||||
bv.push(TypeId::CompTagsText as u8);
|
||||
}
|
||||
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
(DlBody::Tags(tags), DlArgument::Number(_)) => {
|
||||
if tags.len() == 1 {
|
||||
bv.push(TypeId::CompTagNumber as u8);
|
||||
} else {
|
||||
bv.push(TypeId::CompTagsNumber as u8);
|
||||
}
|
||||
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
(DlBody::Tags(tags), DlArgument::Null) => {
|
||||
if tags.len() == 1 {
|
||||
match &tag.id {
|
||||
DlTid::El => bv.push(TypeId::ElTag as u8),
|
||||
DlTid::P => bv.push(TypeId::PTag as u8),
|
||||
_ => {
|
||||
bv.push(TypeId::BodyTag as u8);
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match &tag.id {
|
||||
DlTid::El => bv.push(TypeId::ElTags as u8),
|
||||
DlTid::P => bv.push(TypeId::PTags as u8),
|
||||
_ => {
|
||||
bv.push(TypeId::BodyTags as u8);
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(DlBody::Null, DlArgument::Text(_)) => match &tag.id {
|
||||
DlTid::Img => bv.push(TypeId::Img as u8),
|
||||
DlTid::A => bv.push(TypeId::AText as u8),
|
||||
|
||||
_ => {
|
||||
bv.push(TypeId::ArgText as u8);
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
},
|
||||
(DlBody::Null, DlArgument::Number(_)) => match &tag.id {
|
||||
DlTid::A => bv.push(TypeId::ANumber as u8),
|
||||
_ => {
|
||||
bv.push(TypeId::ArgNumber as u8);
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
},
|
||||
(DlBody::Null, DlArgument::Null) => match &tag.id {
|
||||
DlTid::Br => bv.push(TypeId::Br as u8),
|
||||
DlTid::Hr => bv.push(TypeId::Hr as u8),
|
||||
|
||||
_ => {
|
||||
bv.push(TypeId::Id as u8);
|
||||
bv.push(tag.id as u8);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
write_tag_body(bv, &tag.body)?;
|
||||
write_tag_argument(bv, &tag.argument)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -84,7 +153,7 @@ fn write_tag_body(bv: &mut Vec<u8>, body: &DlBody) -> Result<(), DaletPackError>
|
|||
match body {
|
||||
DlBody::Text(s) => write_str(bv, s)?,
|
||||
DlBody::Tags(tags) => write_array(bv, tags)?,
|
||||
DlBody::Null => Err(DaletPackError::WriteNullBody)?,
|
||||
DlBody::Null => {}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
|
@ -93,8 +162,8 @@ fn write_tag_body(bv: &mut Vec<u8>, body: &DlBody) -> Result<(), DaletPackError>
|
|||
fn write_tag_argument(bv: &mut Vec<u8>, argument: &DlArgument) -> Result<(), DaletPackError> {
|
||||
match argument {
|
||||
DlArgument::Text(s) => write_str(bv, s)?,
|
||||
DlArgument::Number(n) => write_int(bv, *n),
|
||||
DlArgument::Null => Err(DaletPackError::WriteNullArgument)?,
|
||||
DlArgument::Number(n) => bv.push(*n),
|
||||
DlArgument::Null => {}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -43,13 +43,35 @@ impl From<TryFromPrimitiveError<DlTid>> for DaletPackDecodeError {
|
|||
#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive, Copy)]
|
||||
#[repr(u8)]
|
||||
pub enum TypeId {
|
||||
TextEnd = 0,
|
||||
Text,
|
||||
Number,
|
||||
Tags,
|
||||
TextEnd = 0x00,
|
||||
TagsEnd,
|
||||
TagId,
|
||||
TagIdBody,
|
||||
TagIdArgument,
|
||||
TagIdBodyArgument,
|
||||
BodyText = 0xa0,
|
||||
BodyTag,
|
||||
BodyTags,
|
||||
ArgText = 0xb0,
|
||||
ArgNumber,
|
||||
CompTextText = 0xc0,
|
||||
CompTagText,
|
||||
CompTagsText,
|
||||
CompTextNumber,
|
||||
CompTagNumber,
|
||||
CompTagsNumber,
|
||||
Id = 0xd0,
|
||||
ElText,
|
||||
ElTag,
|
||||
ElTags,
|
||||
PText,
|
||||
PTag,
|
||||
PTags,
|
||||
Br,
|
||||
Hr,
|
||||
Img,
|
||||
B,
|
||||
I,
|
||||
ANumber,
|
||||
AText,
|
||||
S,
|
||||
Sup,
|
||||
Sub,
|
||||
Meta,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue