fix: decompress max capacity, clippy --fix

This commit is contained in:
Artemy Egorov 2024-08-07 17:19:16 +03:00
parent 915c5c0a6d
commit a5443bf0fc
9 changed files with 25 additions and 30 deletions

View file

@ -1,5 +1,3 @@
use std::u32::MAX;
use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid}; use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid};
use super::{utils, DaletPackDecodeError, TypeId}; use super::{utils, DaletPackDecodeError, TypeId};
@ -20,7 +18,7 @@ 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 mut array: Vec<DlTag> = Vec::new();
for _ in 0..MAX { for _ in 0..u32::MAX {
let typeid = self.data.next(); let typeid = self.data.next();
match typeid { match typeid {
@ -88,7 +86,7 @@ impl<'a> Decoder<'a> {
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..MAX { for _ in 0..u32::MAX {
let val = self let val = self
.data .data
.next() .next()
@ -107,7 +105,7 @@ impl<'a> Decoder<'a> {
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..MAX { for _ in 0..u32::MAX {
let typeid: TypeId = self let typeid: TypeId = self
.data .data
.next() .next()

View file

@ -3,8 +3,8 @@ use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid, IsNull};
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> {
Ok(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> {

View file

@ -1,9 +1,13 @@
use std::u32::MAX; use std::io::{self, Read};
use zstd::stream::read::Decoder;
pub fn compress_zstd(data: &[u8]) -> std::io::Result<Vec<u8>> { pub fn compress_zstd(data: &[u8]) -> io::Result<Vec<u8>> {
zstd::bulk::compress(data, 22) zstd::bulk::compress(data, 22)
} }
pub fn decompress_zstd(data: &[u8]) -> std::io::Result<Vec<u8>> { pub fn decompress_zstd(data: &[u8]) -> io::Result<Vec<u8>> {
zstd::bulk::decompress(data, MAX as usize) let mut decoder = Decoder::new(data)?;
let mut decompressed = Vec::new();
decoder.read_to_end(&mut decompressed)?;
Ok(decompressed)
} }

View file

@ -33,7 +33,7 @@ pub fn parse_gemtext(s: &str) -> Result<Page, GemTextParseError> {
match body.next() { match body.next() {
Some(label) => page.push(P( Some(label) => page.push(P(
vec![Navlink(label.trim().into(), url.into()).into()].into() vec![Navlink(label.trim().into(), url.into())].into()
)), )),
None => page.push(P(vec![Navlink(Body::Null, url.into())].into())), None => page.push(P(vec![Navlink(Body::Null, url.into())].into())),
}; };

View file

@ -75,7 +75,7 @@ impl TryFrom<DlArgument> for TNArg {
fn try_from(value: DlArgument) -> Result<Self, Self::Error> { fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
match value { match value {
DlArgument::Text(t) => Ok(TNArg::Text(t.into())), DlArgument::Text(t) => Ok(TNArg::Text(t)),
DlArgument::Null => Ok(TNArg::Null), DlArgument::Null => Ok(TNArg::Null),
_ => Err(ConversionError), _ => Err(ConversionError),
} }

View file

@ -2,18 +2,12 @@ use crate::daletl::{DlArgument, DlBody, IsNull};
impl IsNull for DlBody { impl IsNull for DlBody {
fn is_null(&self) -> bool { fn is_null(&self) -> bool {
match self { matches!(self, Self::Null)
Self::Null => true,
_ => false,
}
} }
} }
impl IsNull for DlArgument { impl IsNull for DlArgument {
fn is_null(&self) -> bool { fn is_null(&self) -> bool {
match self { matches!(self, Self::Null)
Self::Null => true,
_ => false,
}
} }
} }

View file

@ -24,10 +24,9 @@ fn resolve_from_tags(tags: &Vec<Tag>) -> Option<String> {
} }
} }
Block(body, _) => match body { Block(NNBody::Tags(tags), _) => {
NNBody::Tags(tags) => return resolve_from_tags(tags), return resolve_from_tags(tags);
_ => {} }
},
_ => {} _ => {}
}; };

View file

@ -17,15 +17,15 @@ macro_rules! iprint {
}}; }};
} }
pub fn compress_deflate(data: &Vec<u8>) -> std::io::Result<Vec<u8>> { pub fn compress_deflate(data: &[u8]) -> std::io::Result<Vec<u8>> {
let mut c = flate2::write::DeflateEncoder::new(Vec::new(), Compression::default()); let mut c = flate2::write::DeflateEncoder::new(Vec::new(), Compression::default());
c.write(data)?; c.write_all(data)?;
c.finish() c.finish()
} }
pub fn compress_zlib(data: &Vec<u8>) -> std::io::Result<Vec<u8>> { pub fn compress_zlib(data: &[u8]) -> std::io::Result<Vec<u8>> {
let mut c = flate2::write::ZlibEncoder::new(Vec::new(), Compression::default()); let mut c = flate2::write::ZlibEncoder::new(Vec::new(), Compression::default());
c.write(data)?; c.write_all(data)?;
c.finish() c.finish()
} }

View file

@ -4,7 +4,7 @@ use dalet::parsers::gemtext::parse_gemtext;
fn gem_text() { fn gem_text() {
let text = include_str!("./gemtext.gmi"); let text = include_str!("./gemtext.gmi");
let _ = parse_gemtext(&text).unwrap(); let _ = parse_gemtext(text).unwrap();
// println!("{:#?}", parsed); // println!("{:#?}", parsed);
} }