refactor: rename root to page

This commit is contained in:
Artemy Egorov 2024-08-03 12:56:08 +03:00
parent 5058bb812b
commit a9f7d54ee1
5 changed files with 20 additions and 20 deletions

View file

@ -3,7 +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};
pub type Root = Vec<Tag>; pub type Page = Vec<Tag>;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Tag { pub struct Tag {
@ -98,7 +98,7 @@ pub trait IsNull {
fn is_null(&self) -> bool; fn is_null(&self) -> bool;
} }
pub trait ToDaletlRoot { pub trait ToDaletlPage {
/// Convert to daletl root /// Convert to daletl Page
fn to_dl_root(self) -> Root; fn to_dl_page(self) -> Page;
} }

View file

@ -1,20 +1,20 @@
use crate::daletl::{Argument, Body, IsNull, Tag, Tid}; use crate::daletl::{Argument, Body, IsNull, Page, Tag, Tid};
use super::{DaletPackError, TypeId}; use super::{DaletPackError, TypeId};
pub fn encode(root: &Vec<Tag>) -> Result<Vec<u8>, DaletPackError> { pub fn encode(page: &Page) -> Result<Vec<u8>, DaletPackError> {
Ok(zstd::bulk::compress(&encode_no_compress(root)?, 5) Ok(zstd::bulk::compress(&encode_no_compress(page)?, 5)
.map_err(|_| DaletPackError::ZstdCompressError)?) .map_err(|_| DaletPackError::ZstdCompressError)?)
} }
pub fn encode_no_compress(root: &Vec<Tag>) -> Result<Vec<u8>, DaletPackError> { pub fn encode_no_compress(page: &Page) -> Result<Vec<u8>, DaletPackError> {
if root.len() > 2usize.pow(32) { if page.len() > 2usize.pow(32) {
return Err(DaletPackError::RootMaxSizeExceeded); return Err(DaletPackError::PageMaxSizeExceeded);
} }
let mut bv: Vec<u8> = Vec::new(); let mut bv: Vec<u8> = Vec::new();
for tag in root { for tag in page {
write_tag(&mut bv, tag)?; write_tag(&mut bv, tag)?;
} }

View file

@ -4,7 +4,7 @@ use num_enum::TryFromPrimitive;
pub enum DaletPackError { pub enum DaletPackError {
StrMaxSizeExceeded, StrMaxSizeExceeded,
ArrMaxSizeExceeded, ArrMaxSizeExceeded,
RootMaxSizeExceeded, PageMaxSizeExceeded,
ZstdCompressError, ZstdCompressError,
WriteNullBody, WriteNullBody,

View file

@ -1,12 +1,12 @@
use enum_procs::AutoFrom; use enum_procs::AutoFrom;
use num_enum::TryFromPrimitive; use num_enum::TryFromPrimitive;
use crate::daletl::{self, t_new, Tid, ToDaletlRoot}; use crate::daletl::{self, t_new, Tid, ToDaletlPage};
const NB: daletl::Body = daletl::Body::Null; const NB: daletl::Body = daletl::Body::Null;
const NA: daletl::Argument = daletl::Argument::Null; const NA: daletl::Argument = daletl::Argument::Null;
pub type Root = Vec<Tag>; pub type Page = Vec<Tag>;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Tag { pub enum Tag {
@ -208,8 +208,8 @@ impl From<Vec<Tag>> for daletl::Body {
} }
} }
impl ToDaletlRoot for Root { impl ToDaletlPage for Page {
fn to_dl_root(self) -> daletl::Root { fn to_dl_page(self) -> daletl::Page {
self.into_iter().map(|tag| tag.into()).collect() self.into_iter().map(|tag| tag.into()).collect()
} }
} }

View file

@ -1,8 +1,8 @@
use dalet::{ use dalet::{
daletl::ToDaletlRoot, daletl::ToDaletlPage,
daletpack::*, daletpack::*,
typed::{ typed::{
Body, Hl, NNBody, Root, TNArgument, Body, Hl, NNBody, Page, TNArgument,
Tag::{self, *}, Tag::{self, *},
}, },
}; };
@ -35,7 +35,7 @@ pub fn compress_zlib(data: &Vec<u8>) -> std::io::Result<Vec<u8>> {
#[test] #[test]
fn bench() { fn bench() {
let page: Root = vec![ let page: Page = vec![
H("I am heading".into(), Hl::One), H("I am heading".into(), Hl::One),
H("Heading 2".into(), Hl::Two), H("Heading 2".into(), Hl::Two),
El(vec![ El(vec![
@ -81,7 +81,7 @@ fn bench() {
]), ]),
]; ];
let dalet_page = page.to_dl_root(); let dalet_page = page.to_dl_page();
let markdown = iprint!("Markdown", include_str!("./bench.md").as_bytes().to_vec()); let markdown = iprint!("Markdown", include_str!("./bench.md").as_bytes().to_vec());
let daletpack = iprint!("Daletpack", encode_no_compress(&dalet_page).unwrap()); let daletpack = iprint!("Daletpack", encode_no_compress(&dalet_page).unwrap());