fix: update daletpack encode format

This commit is contained in:
Artemy Egorov 2024-08-18 12:43:58 +03:00
parent f6195aa708
commit 54c594d3f6
9 changed files with 303 additions and 217 deletions

View file

@ -42,8 +42,8 @@ br
# if no tag is specified but a '{- text}' is present, then the 'p' tag is placed # if no tag is specified but a '{- text}' is present, then the 'p' tag is placed
# '\n' is replaced with ' ' in this format. # '\n' is replaced with ' ' in this format.
{- {-
Check Dalet too Check Dalet too,
This is one paragraph this is one paragraph
} }
{- {-
@ -92,6 +92,10 @@ row [
npm run dev npm run dev
} }
code "markdown" {~4
this is codeblock
}
# {# Text} Text after "{#" not modified # {# Text} Text after "{#" not modified
code "markdown" {# this is codeblock} code "markdown" {# this is codeblock}
]] ]]
@ -122,13 +126,13 @@ row [
# Description # Description
# ] # ]
{> table {> table
[[ Tag | Description ]] [[ Tag | Description | id ]]
[ h | Heading ] [ h | Heading | 0 ]
[ p | Paragraph ] [ p | Paragraph | 1 ]
[ img | Image ] [ img | Image | 2 ]
[ link | Link ] [ link | Link | 3 ]
[ btn | Button ] [ btn | Button | 4 ]
[ ul | Unordered list ] [ ul | Unordered list | 5 ]
[ br | Line break ] [ br | Line break | 6 ]
[[ quantity | 7 ]] [[ quantity | 7 | 7 ]]
} }

View file

@ -14,25 +14,19 @@ use super::types::Span;
pub fn table_to_tag(rows: &Vec<TableCol>) -> Tag { pub fn table_to_tag(rows: &Vec<TableCol>) -> Tag {
Tag::Table( Tag::Table(
rows.into_iter() rows.iter()
.map(|row| match row { .map(|row| match row {
TableCol::Primary(row) => Tag::Tprow( TableCol::Primary(row) => Tag::Tprow(
row.into_iter() row.iter()
.map(|t| { .map(|t| {
Tag::El(NNBody::Text(format!( Tag::El(NNBody::Text(t.replace("\\]", "]").replace("\\|", "|").to_string()))
"{}",
t.replace("\\]", "]").replace("\\|", "|")
)))
}) })
.collect(), .collect(),
), ),
TableCol::Secondary(row) => Tag::Trow( TableCol::Secondary(row) => Tag::Trow(
row.into_iter() row.iter()
.map(|t| { .map(|t| {
Tag::El(NNBody::Text(format!( Tag::El(NNBody::Text(t.replace("\\]", "]").replace("\\|", "|").to_string()))
"{}",
t.replace("\\]", "]").replace("\\|", "|")
)))
}) })
.collect(), .collect(),
), ),

View file

@ -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 current_indent: usize = 0;
let mut formatted = String::new(); let mut formatted = String::new();
let len = spanned_tokens.len(); let len = spanned_tokens.len();
@ -162,7 +162,7 @@ pub fn format<'src>(spanned_tokens: &Vec<Spanned<Token<'src>>>) -> String {
prepend_indent("}", current_indent) prepend_indent("}", current_indent)
), ),
Token::EmptyLine => "\n".to_owned(), Token::EmptyLine => prepend_indent("\n", current_indent),
}; };
formatted.push_str(&to_push); formatted.push_str(&to_push);

View file

@ -134,7 +134,6 @@ fn textual<'src>() -> impl Parser<'src, &'src str, Token<'src>, extra::Err<Rich<
.labelled("Body of multiline text"); .labelled("Body of multiline text");
let paragraph = multiline_text_body let paragraph = multiline_text_body
.clone()
.delimited_by(just("{-"), just("}")) .delimited_by(just("{-"), just("}"))
.map(Token::Paragraph) .map(Token::Paragraph)
.labelled("Paragraph syntax"); .labelled("Paragraph syntax");
@ -145,7 +144,6 @@ fn textual<'src>() -> impl Parser<'src, &'src str, Token<'src>, extra::Err<Rich<
.labelled("Table syntax"); .labelled("Table syntax");
let mltext = multiline_text_body let mltext = multiline_text_body
.clone()
.delimited_by(just('{'), just('}')) .delimited_by(just('{'), just('}'))
.map(Token::MLText) .map(Token::MLText)
.labelled("Multiline text"); .labelled("Multiline text");
@ -156,7 +154,7 @@ fn textual<'src>() -> impl Parser<'src, &'src str, Token<'src>, extra::Err<Rich<
.labelled("Minimum spaces number"); .labelled("Minimum spaces number");
mlms_n mlms_n
.then(multiline_text_body.clone()) .then(multiline_text_body)
.then_ignore(just("}")) .then_ignore(just("}"))
.map(|(n, t)| Token::MLMSText(n, t)) .map(|(n, t)| Token::MLMSText(n, t))
.labelled("Multi line text with min spaces") .labelled("Multi line text with min spaces")

View file

@ -84,7 +84,6 @@ pub fn tag<'tokens, 'src: 'tokens>(
.labelled("Heading level argument"); .labelled("Heading level argument");
let alignarg = text_arg let alignarg = text_arg
.clone()
.validate(|t, e, emmiter| match t.as_str() { .validate(|t, e, emmiter| match t.as_str() {
"start" => AlignArg::Start, "start" => AlignArg::Start,
"center" => AlignArg::Center, "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 ul = just(Token::Ul).ignore_then(tags_body.clone()).map(Ul);
let ol = just(Token::Ol).ignore_then(tags_body.clone()).map(Ol); let ol = just(Token::Ol).ignore_then(tags_body.clone()).map(Ol);
let row = just(Token::Row) let row = just(Token::Row)
.ignore_then(alignarg.clone().or_not()) .ignore_then(alignarg.or_not())
.then(tags_body.clone()) .then(tags_body.clone())
.map(|(arg, body)| Row(body, arg.unwrap_or(AlignArg::Start))); .map(|(arg, body)| Row(body, arg.unwrap_or(AlignArg::Start)));
let link = just(Token::Link) let link = just(Token::Link)
.ignore_then(text_arg.clone()) .ignore_then(text_arg)
.then(body.clone()) .then(body.clone())
.map(|(arg, body)| Link(body, arg)); .map(|(arg, body)| Link(body, arg));
let navlink = just(Token::Navlink) let navlink = just(Token::Navlink)
.ignore_then(text_arg.clone()) .ignore_then(text_arg)
.then(body.clone()) .then(body.clone())
.map(|(arg, body)| Navlink(body, arg)); .map(|(arg, body)| Navlink(body, arg));
let btn = just(Token::Btn) let btn = just(Token::Btn)
.ignore_then(text_arg.clone()) .ignore_then(text_arg)
.then(body.clone()) .then(body.clone())
.map(|(arg, body)| Btn(body, arg)); .map(|(arg, body)| Btn(body, arg));
let navbtn = just(Token::Navbtn) let navbtn = just(Token::Navbtn)
.ignore_then(text_arg.clone()) .ignore_then(text_arg)
.then(body.clone()) .then(body.clone())
.map(|(arg, body)| Navbtn(body, arg)); .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 table = just(Token::Table).ignore_then(tags_body.clone()).map(Table);
let trow = just(Token::Trow).ignore_then(tags_body.clone()).map(Trow); 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 tprow = just(Token::Tprow).ignore_then(tags_body.clone()).map(Tprow);
let hr = just(Token::Hr).to(Hr); let hr = just(Token::Hr).to(Hr);
let b = just(Token::B).ignore_then(text_body.clone()).map(B); let b = just(Token::B).ignore_then(text_body).map(B);
let i = just(Token::I).ignore_then(text_body.clone()).map(I); let i = just(Token::I).ignore_then(text_body).map(I);
let bq = just(Token::Bq).ignore_then(nnbody.clone()).map(Bq); let bq = just(Token::Bq).ignore_then(nnbody.clone()).map(Bq);
let footlnk = just(Token::Footlnk).ignore_then(nnarg).map(Footlnk); let footlnk = just(Token::Footlnk).ignore_then(nnarg).map(Footlnk);
let footn = just(Token::Footn) let footn = just(Token::Footn)
.ignore_then(nnarg.clone()) .ignore_then(nnarg)
.then(text_body.clone()) .then(text_body)
.map(|(arg, body)| Footn(body, arg)); .map(|(arg, body)| Footn(body, arg));
let a = just(Token::A).ignore_then(nnarg.clone()).map(A); let a = just(Token::A).ignore_then(nnarg).map(A);
let s = just(Token::S).ignore_then(text_body.clone()).map(S); let s = just(Token::S).ignore_then(text_body).map(S);
let sup = just(Token::Sup).ignore_then(text_body.clone()).map(Sup); let sup = just(Token::Sup).ignore_then(text_body).map(Sup);
let sub = just(Token::Sub).ignore_then(text_body.clone()).map(Sub); let sub = just(Token::Sub).ignore_then(text_body).map(Sub);
let disc = just(Token::Disc).ignore_then(nnbody.clone()).map(Disc); let disc = just(Token::Disc).ignore_then(nnbody.clone()).map(Disc);
let block = just(Token::Block) let block = just(Token::Block)
.ignore_then(alignarg.or_not()) .ignore_then(alignarg.or_not())
@ -156,12 +155,12 @@ pub fn tag<'tokens, 'src: 'tokens>(
.map(Carousel); .map(Carousel);
let code = just(Token::Code) let code = just(Token::Code)
.ignore_then(tnullarg) .ignore_then(tnullarg)
.then(text_body.clone()) .then(text_body)
.map(|(arg, body)| Code(body, arg)); .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) let meta = just(Token::Meta)
.ignore_then(text_arg) .ignore_then(text_arg)
.then(text_body.clone()) .then(text_body)
.map(|(arg, body)| Meta(body, arg)); .map(|(arg, body)| Meta(body, arg));
let el_text = select! { let el_text = select! {
@ -175,7 +174,7 @@ pub fn tag<'tokens, 'src: 'tokens>(
.map(|v| El(NNBody::Tags(v))); .map(|v| El(NNBody::Tags(v)));
let paragraph = select! { 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"); .labelled("Paragraph");

View file

@ -32,7 +32,7 @@ pub fn set_spaces(input: &str, spaces: usize) -> String {
prepend_spaces(&trim_indent(input), spaces) 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 trim_start = 0;
let mut been_newlines = false; 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 { pub fn prepend_indent(input: &str, indent: usize) -> String {

View file

@ -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> { pub struct Decoder<'a> {
data: Box<dyn Iterator<Item = u8> + 'a>, data: Box<dyn Iterator<Item = u8> + 'a>,
@ -16,155 +16,155 @@ impl<'a> Decoder<'a> {
} }
pub fn decode(&mut self) -> Result<DlPage, DaletPackDecodeError> { 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 { // for _ in 0..u32::MAX {
let typeid = self.data.next(); // let typeid = self.data.next();
match typeid { // match typeid {
Some(typeid) => match typeid.try_into()? { // Some(typeid) => match typeid.try_into()? {
TypeId::Text => array.push(DlTag::new( // TypeId::Text => array.push(DlTag::new(
DlTid::El, // DlTid::El,
self.read_text()?.into(), // self.read_text()?.into(),
DlArgument::Null, // DlArgument::Null,
)), // )),
TypeId::Tags => array.push(DlTag::new( // TypeId::Tags => array.push(DlTag::new(
DlTid::El, // DlTid::El,
self.read_tag_array()?.into(), // self.read_tag_array()?.into(),
DlArgument::Null, // DlArgument::Null,
)), // )),
TypeId::TagId => array.push(self.read_tag_with_id()?), // TypeId::TagId => array.push(self.read_tag_with_id()?),
TypeId::TagIdBody => array.push(self.read_tag_with_id_body()?), // TypeId::TagIdBody => array.push(self.read_tag_with_id_body()?),
TypeId::TagIdArgument => array.push(self.read_tag_with_id_argument()?), // TypeId::TagIdArgument => array.push(self.read_tag_with_id_argument()?),
TypeId::TagIdBodyArgument => array.push(self.read_full_tag()?), // TypeId::TagIdBodyArgument => array.push(self.read_full_tag()?),
_ => Err(DaletPackDecodeError::InvalidSchema)?, // _ => Err(DaletPackDecodeError::InvalidSchema)?,
}, // },
None => break, // None => break,
} // }
} // }
Ok(DlPage { data: array }) Ok(DlPage { data: array })
} }
pub fn read_body(&mut self) -> Result<DlBody, DaletPackDecodeError> { // pub fn read_body(&mut self) -> Result<DlBody, DaletPackDecodeError> {
let typeid: TypeId = self // let typeid: TypeId = self
.data // .data
.next() // .next()
.ok_or(DaletPackDecodeError::InvalidSchema)? // .ok_or(DaletPackDecodeError::InvalidSchema)?
.try_into()?; // .try_into()?;
let value = match typeid { // let value = match typeid {
TypeId::Text => DlBody::Text(self.read_text()?), // TypeId::Text => DlBody::Text(self.read_text()?),
TypeId::Tags => DlBody::Tags(self.read_tag_array()?), // TypeId::Tags => DlBody::Tags(self.read_tag_array()?),
_ => Err(DaletPackDecodeError::InvalidArgument)?, // _ => Err(DaletPackDecodeError::InvalidArgument)?,
}; // };
Ok(value) // Ok(value)
} // }
pub fn read_arg(&mut self) -> Result<DlArgument, DaletPackDecodeError> { // pub fn read_arg(&mut self) -> Result<DlArgument, DaletPackDecodeError> {
let typeid: TypeId = self // let typeid: TypeId = self
.data // .data
.next() // .next()
.ok_or(DaletPackDecodeError::InvalidSchema)? // .ok_or(DaletPackDecodeError::InvalidSchema)?
.try_into()?; // .try_into()?;
let value = match typeid { // let value = match typeid {
TypeId::Text => DlArgument::Text(self.read_text()?), // TypeId::Text => DlArgument::Text(self.read_text()?),
TypeId::Number => DlArgument::Number(self.read_number()?), // TypeId::Number => DlArgument::Number(self.read_number()?),
_ => Err(DaletPackDecodeError::InvalidArgument)?, // _ => Err(DaletPackDecodeError::InvalidArgument)?,
}; // };
Ok(value) // Ok(value)
} // }
fn read_number(&mut self) -> Result<u8, DaletPackDecodeError> { // fn read_number(&mut self) -> Result<u8, DaletPackDecodeError> {
self.data.next().ok_or(DaletPackDecodeError::InvalidSchema) // self.data.next().ok_or(DaletPackDecodeError::InvalidSchema)
} // }
fn read_text(&mut self) -> Result<String, DaletPackDecodeError> { // fn read_text(&mut self) -> Result<String, DaletPackDecodeError> {
let mut str = String::new(); // let mut str = String::new();
for _ in 0..u32::MAX { // for _ in 0..u32::MAX {
let val = self // let val = self
.data // .data
.next() // .next()
.ok_or(DaletPackDecodeError::InvalidTextSchema)?; // .ok_or(DaletPackDecodeError::InvalidTextSchema)?;
if val == TypeId::TextEnd as u8 { // if val == TypeId::TextEnd as u8 {
break; // 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> { // fn read_tag_array(&mut self) -> Result<Vec<DlTag>, DaletPackDecodeError> {
let mut array = Vec::new(); // let mut array = Vec::new();
for _ in 0..u32::MAX { // for _ in 0..u32::MAX {
let typeid: TypeId = self // let typeid: TypeId = self
.data // .data
.next() // .next()
.ok_or(DaletPackDecodeError::InvalidTagsSchema)? // .ok_or(DaletPackDecodeError::InvalidTagsSchema)?
.try_into()?; // .try_into()?;
match typeid { // match typeid {
TypeId::Text => array.push(DlTag::new( // TypeId::Text => array.push(DlTag::new(
DlTid::El, // DlTid::El,
self.read_text()?.into(), // self.read_text()?.into(),
DlArgument::Null, // DlArgument::Null,
)), // )),
TypeId::Tags => array.push(DlTag::new( // TypeId::Tags => array.push(DlTag::new(
DlTid::El, // DlTid::El,
self.read_tag_array()?.into(), // self.read_tag_array()?.into(),
DlArgument::Null, // DlArgument::Null,
)), // )),
TypeId::TagId => array.push(self.read_tag_with_id()?), // TypeId::TagId => array.push(self.read_tag_with_id()?),
TypeId::TagIdBody => array.push(self.read_tag_with_id_body()?), // TypeId::TagIdBody => array.push(self.read_tag_with_id_body()?),
TypeId::TagIdArgument => array.push(self.read_tag_with_id_argument()?), // TypeId::TagIdArgument => array.push(self.read_tag_with_id_argument()?),
TypeId::TagIdBodyArgument => array.push(self.read_full_tag()?), // TypeId::TagIdBodyArgument => array.push(self.read_full_tag()?),
TypeId::TagsEnd => break, // TypeId::TagsEnd => break,
_ => Err(DaletPackDecodeError::InvalidSchema)?, // _ => Err(DaletPackDecodeError::InvalidSchema)?,
} // }
} // }
Ok(array) // Ok(array)
} // }
fn read_tag_with_id(&mut self) -> Result<DlTag, DaletPackDecodeError> { // fn read_tag_with_id(&mut self) -> Result<DlTag, DaletPackDecodeError> {
Ok(DlTag::new( // Ok(DlTag::new(
self.read_number()?.try_into()?, // self.read_number()?.try_into()?,
DlBody::Null, // DlBody::Null,
DlArgument::Null, // DlArgument::Null,
)) // ))
} // }
fn read_tag_with_id_body(&mut self) -> Result<DlTag, DaletPackDecodeError> { // fn read_tag_with_id_body(&mut self) -> Result<DlTag, DaletPackDecodeError> {
Ok(DlTag::new( // Ok(DlTag::new(
self.read_number()?.try_into()?, // self.read_number()?.try_into()?,
self.read_body()?, // self.read_body()?,
DlArgument::Null, // DlArgument::Null,
)) // ))
} // }
fn read_tag_with_id_argument(&mut self) -> Result<DlTag, DaletPackDecodeError> { // fn read_tag_with_id_argument(&mut self) -> Result<DlTag, DaletPackDecodeError> {
Ok(DlTag::new( // Ok(DlTag::new(
self.read_number()?.try_into()?, // self.read_number()?.try_into()?,
DlBody::Null, // DlBody::Null,
self.read_arg()?, // self.read_arg()?,
)) // ))
} // }
fn read_full_tag(&mut self) -> Result<DlTag, DaletPackDecodeError> { // fn read_full_tag(&mut self) -> Result<DlTag, DaletPackDecodeError> {
Ok(DlTag::new( // Ok(DlTag::new(
self.read_number()?.try_into()?, // self.read_number()?.try_into()?,
self.read_body()?, // self.read_body()?,
self.read_arg()?, // self.read_arg()?,
)) // ))
} // }
} }

View file

@ -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}; use super::{utils, DaletPackError, TypeId};
pub fn encode(page: &DlPage) -> Result<Vec<u8>, DaletPackError> { pub fn encode(page: &DlPage) -> Result<Vec<u8>, DaletPackError> {
utils::compress_zstd(&encode_no_compress(page)?) utils::compress_zstd(&encode_no_compress(page)?).map_err(|_| DaletPackError::ZstdCompressError)
.map_err(|_| DaletPackError::ZstdCompressError)
} }
pub fn encode_no_compress(page: &DlPage) -> Result<Vec<u8>, DaletPackError> { 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) 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> { fn write_str(bv: &mut Vec<u8>, string: &String) -> Result<(), DaletPackError> {
let size = string.len(); let size = string.len();
@ -33,7 +27,6 @@ fn write_str(bv: &mut Vec<u8>, string: &String) -> Result<(), DaletPackError> {
return Err(DaletPackError::StrMaxSizeExceeded); return Err(DaletPackError::StrMaxSizeExceeded);
} }
bv.push(TypeId::Text as u8);
bv.extend_from_slice(string.as_bytes()); bv.extend_from_slice(string.as_bytes());
bv.push(TypeId::TextEnd as u8); 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); return Err(DaletPackError::ArrMaxSizeExceeded);
} }
bv.push(TypeId::Tags as u8);
for tag in arr { for tag in arr {
write_tag(bv, tag)?; write_tag(bv, tag)?;
} }
if arr.len() != 1 {
bv.push(TypeId::TagsEnd as u8); bv.push(TypeId::TagsEnd as u8);
}
Ok(()) Ok(())
} }
fn write_tag(bv: &mut Vec<u8>, tag: &DlTag) -> Result<(), DaletPackError> { fn write_tag(bv: &mut Vec<u8>, tag: &DlTag) -> Result<(), DaletPackError> {
if tag.id == DlTid::El { // TypeId and TagId if needed
write_tag_body(bv, &tag.body)?; match (&tag.body, &tag.argument) {
} else if tag.body.is_null() && tag.argument.is_null() { (DlBody::Text(_), DlArgument::Text(_)) => match &tag.id {
bv.push(TypeId::TagId as u8); DlTid::Meta => bv.push(TypeId::Meta as u8),
_ => {
bv.push(TypeId::CompTextText as u8);
bv.push(tag.id 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)?;
} }
},
(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(()) Ok(())
} }
@ -84,7 +153,7 @@ fn write_tag_body(bv: &mut Vec<u8>, body: &DlBody) -> Result<(), DaletPackError>
match body { match body {
DlBody::Text(s) => write_str(bv, s)?, DlBody::Text(s) => write_str(bv, s)?,
DlBody::Tags(tags) => write_array(bv, tags)?, DlBody::Tags(tags) => write_array(bv, tags)?,
DlBody::Null => Err(DaletPackError::WriteNullBody)?, DlBody::Null => {}
}; };
Ok(()) 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> { fn write_tag_argument(bv: &mut Vec<u8>, argument: &DlArgument) -> Result<(), DaletPackError> {
match argument { match argument {
DlArgument::Text(s) => write_str(bv, s)?, DlArgument::Text(s) => write_str(bv, s)?,
DlArgument::Number(n) => write_int(bv, *n), DlArgument::Number(n) => bv.push(*n),
DlArgument::Null => Err(DaletPackError::WriteNullArgument)?, DlArgument::Null => {}
}; };
Ok(()) Ok(())

View file

@ -43,13 +43,35 @@ impl From<TryFromPrimitiveError<DlTid>> for DaletPackDecodeError {
#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive, Copy)] #[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive, Copy)]
#[repr(u8)] #[repr(u8)]
pub enum TypeId { pub enum TypeId {
TextEnd = 0, TextEnd = 0x00,
Text,
Number,
Tags,
TagsEnd, TagsEnd,
TagId, BodyText = 0xa0,
TagIdBody, BodyTag,
TagIdArgument, BodyTags,
TagIdBodyArgument, 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,
} }