From a5443bf0fcd70b27d1229d5e945af1771949d8d5 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Wed, 7 Aug 2024 17:19:16 +0300 Subject: [PATCH] fix: decompress max capacity, clippy --fix --- src/daletpack/decode.rs | 8 +++----- src/daletpack/encode.rs | 4 ++-- src/daletpack/utils.rs | 12 ++++++++---- src/parsers/gemtext.rs | 2 +- src/traits/from_daletl.rs | 2 +- src/traits/is_null_daletl.rs | 10 ++-------- src/traits/resolve_title.rs | 7 +++---- tests/bench.rs | 8 ++++---- tests/gemtext.rs | 2 +- 9 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/daletpack/decode.rs b/src/daletpack/decode.rs index 9352b52..524f2fc 100644 --- a/src/daletpack/decode.rs +++ b/src/daletpack/decode.rs @@ -1,5 +1,3 @@ -use std::u32::MAX; - use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid}; use super::{utils, DaletPackDecodeError, TypeId}; @@ -20,7 +18,7 @@ impl<'a> Decoder<'a> { pub fn decode(&mut self) -> Result { let mut array: Vec = Vec::new(); - for _ in 0..MAX { + for _ in 0..u32::MAX { let typeid = self.data.next(); match typeid { @@ -88,7 +86,7 @@ impl<'a> Decoder<'a> { fn read_text(&mut self) -> Result { let mut str = String::new(); - for _ in 0..MAX { + for _ in 0..u32::MAX { let val = self .data .next() @@ -107,7 +105,7 @@ impl<'a> Decoder<'a> { fn read_tag_array(&mut self) -> Result, DaletPackDecodeError> { let mut array = Vec::new(); - for _ in 0..MAX { + for _ in 0..u32::MAX { let typeid: TypeId = self .data .next() diff --git a/src/daletpack/encode.rs b/src/daletpack/encode.rs index 51bec42..4b4b3cc 100644 --- a/src/daletpack/encode.rs +++ b/src/daletpack/encode.rs @@ -3,8 +3,8 @@ use crate::daletl::{DlArgument, DlBody, DlPage, DlTag, DlTid, IsNull}; use super::{utils, DaletPackError, TypeId}; pub fn encode(page: &DlPage) -> Result, DaletPackError> { - Ok(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, DaletPackError> { diff --git a/src/daletpack/utils.rs b/src/daletpack/utils.rs index 0570dd8..1f8f257 100644 --- a/src/daletpack/utils.rs +++ b/src/daletpack/utils.rs @@ -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> { +pub fn compress_zstd(data: &[u8]) -> io::Result> { zstd::bulk::compress(data, 22) } -pub fn decompress_zstd(data: &[u8]) -> std::io::Result> { - zstd::bulk::decompress(data, MAX as usize) +pub fn decompress_zstd(data: &[u8]) -> io::Result> { + let mut decoder = Decoder::new(data)?; + let mut decompressed = Vec::new(); + decoder.read_to_end(&mut decompressed)?; + Ok(decompressed) } diff --git a/src/parsers/gemtext.rs b/src/parsers/gemtext.rs index 0c000e7..aa25b28 100644 --- a/src/parsers/gemtext.rs +++ b/src/parsers/gemtext.rs @@ -33,7 +33,7 @@ pub fn parse_gemtext(s: &str) -> Result { match body.next() { 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())), }; diff --git a/src/traits/from_daletl.rs b/src/traits/from_daletl.rs index 0aec2cd..bde216d 100644 --- a/src/traits/from_daletl.rs +++ b/src/traits/from_daletl.rs @@ -75,7 +75,7 @@ impl TryFrom for TNArg { fn try_from(value: DlArgument) -> Result { match value { - DlArgument::Text(t) => Ok(TNArg::Text(t.into())), + DlArgument::Text(t) => Ok(TNArg::Text(t)), DlArgument::Null => Ok(TNArg::Null), _ => Err(ConversionError), } diff --git a/src/traits/is_null_daletl.rs b/src/traits/is_null_daletl.rs index 3f33717..5817afd 100644 --- a/src/traits/is_null_daletl.rs +++ b/src/traits/is_null_daletl.rs @@ -2,18 +2,12 @@ use crate::daletl::{DlArgument, DlBody, IsNull}; impl IsNull for DlBody { fn is_null(&self) -> bool { - match self { - Self::Null => true, - _ => false, - } + matches!(self, Self::Null) } } impl IsNull for DlArgument { fn is_null(&self) -> bool { - match self { - Self::Null => true, - _ => false, - } + matches!(self, Self::Null) } } diff --git a/src/traits/resolve_title.rs b/src/traits/resolve_title.rs index b3d6dc5..779f17d 100644 --- a/src/traits/resolve_title.rs +++ b/src/traits/resolve_title.rs @@ -24,10 +24,9 @@ fn resolve_from_tags(tags: &Vec) -> Option { } } - Block(body, _) => match body { - NNBody::Tags(tags) => return resolve_from_tags(tags), - _ => {} - }, + Block(NNBody::Tags(tags), _) => { + return resolve_from_tags(tags); + } _ => {} }; diff --git a/tests/bench.rs b/tests/bench.rs index fc14c63..f30d785 100644 --- a/tests/bench.rs +++ b/tests/bench.rs @@ -17,15 +17,15 @@ macro_rules! iprint { }}; } -pub fn compress_deflate(data: &Vec) -> std::io::Result> { +pub fn compress_deflate(data: &[u8]) -> std::io::Result> { let mut c = flate2::write::DeflateEncoder::new(Vec::new(), Compression::default()); - c.write(data)?; + c.write_all(data)?; c.finish() } -pub fn compress_zlib(data: &Vec) -> std::io::Result> { +pub fn compress_zlib(data: &[u8]) -> std::io::Result> { let mut c = flate2::write::ZlibEncoder::new(Vec::new(), Compression::default()); - c.write(data)?; + c.write_all(data)?; c.finish() } diff --git a/tests/gemtext.rs b/tests/gemtext.rs index bda2175..beaf33b 100644 --- a/tests/gemtext.rs +++ b/tests/gemtext.rs @@ -4,7 +4,7 @@ use dalet::parsers::gemtext::parse_gemtext; fn gem_text() { let text = include_str!("./gemtext.gmi"); - let _ = parse_gemtext(&text).unwrap(); + let _ = parse_gemtext(text).unwrap(); // println!("{:#?}", parsed); }