Skip to content

Commit

Permalink
FIX: Clippy offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
bsodmike committed Sep 8, 2024
1 parent 188b5eb commit 7dffa87
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
8 changes: 7 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{error_from, LimitReaderOutputBuilderError};
use std::{
error::Error as StdError,
fmt::{self},
num::TryFromIntError,
string::FromUtf8Error,
};

Expand All @@ -25,8 +26,10 @@ pub struct Error {
#[allow(clippy::enum_variant_names)]
pub enum ErrorKind {
IoError,
Utf8Error,
LimitReaderOutputBuilderError,
Utf8Error,
TryFromIntError,
LimitReaderError(Box<dyn StdError + Send + Sync>),
}

impl ErrorKind {
Expand All @@ -38,6 +41,8 @@ impl ErrorKind {
IoError => "io error",
Utf8Error => "invalid utf-8",
LimitReaderOutputBuilderError => "builder error",
TryFromIntError => "conversion error",
LimitReaderError(_) => "boxed error",
}
}
}
Expand Down Expand Up @@ -83,6 +88,7 @@ error_from!(
LimitReaderOutputBuilderError,
ErrorKind::LimitReaderOutputBuilderError
);
error_from!(TryFromIntError, ErrorKind::TryFromIntError);

#[macro_use]
pub mod macros {
Expand Down
29 changes: 20 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ pub(crate) mod readable;
/// Default result type for [`LimitReader`]
pub type LimitReaderResult<T> = std::result::Result<T, LimitReaderError>;

/// Re-exports
/// Re-exports Traits and macros used by most projects. Add `use better_limit_reader::prelude::*;` to your code to quickly get started with [`LimitReader`].
pub mod prelude {
//! Traits and macros used by most projects. Add `use better_limit_reader::prelude::*;` to your code to quickly get started with LimitReader.

pub use crate::{error::LimitReaderError, LimitReader, LimitReaderOutput, LimitReaderResult};
}

#[allow(dead_code)]
/// The [LimitReader] reads into `buf` which is held within the record struct.
/// The [`LimitReader`] reads into `buf` which is held within the record struct.
pub struct LimitReader {
buf: [u8; Self::DEFAULT_BUF_SIZE],
expected_size: u64,
Expand All @@ -54,17 +54,19 @@ impl LimitReader {
pub const DEFAULT_BUF_SIZE: usize = 1024;

/// Create a new [`LimitReader`] with a [`LimitReader::DEFAULT_BUF_SIZE`] for the limit-readers max threshold.
#[must_use]
pub fn new() -> Self {
Self {
buf: [0; Self::DEFAULT_BUF_SIZE as usize],
buf: [0; Self::DEFAULT_BUF_SIZE],
expected_size: (Self::DEFAULT_BUF_SIZE - 1) as u64,
decode_zlib: false,
decode_gzip: false,
}
}

/// Return a reference to the internal buffer.
pub fn buffer(&self) -> &[u8; Self::DEFAULT_BUF_SIZE as usize] {
#[must_use]
pub fn buffer(&self) -> &[u8; Self::DEFAULT_BUF_SIZE] {
&self.buf
}

Expand All @@ -86,13 +88,21 @@ impl LimitReader {
// NOTE: This is private until this is implemented in the future.
/// Enable decoding from compressed Gzip
fn enable_decode_gzip(&mut self) -> &mut Self {
unimplemented!()
// self.decode_gzip = true;
self.decode_gzip = true;

// self
self
}

/// Read from provided source file. If the source data is already Zlib compressed, optionally decode the data stream before reading it through a limit-reader.
///
/// # Panics
///
/// If the provided source file does not exist or is inaccessible, it will panic. Refer to [`std::fs::File::open`] for details. This will return [`LimitReaderError`].
///
/// # Errors
///
/// If this function encounters an error of the kind [`LimitReaderError`], this error will be returned.
///
pub fn read(&mut self, source: PathBuf) -> Result<usize> {
let f = std::fs::File::open(source).expect("Unable to open file");
if self.decode_zlib {
Expand All @@ -113,7 +123,8 @@ impl LimitReader {
///
/// # Errors
///
/// FIXME
/// If this function encounters an error of the kind [`LimitReaderError`], this error will be returned.
///
pub fn read_limited(&mut self, source: PathBuf) -> Result<LimitReaderOutput> {
let source_bytes = std::fs::metadata(&source)?.len();
let f = std::fs::File::open(source)?;
Expand Down
26 changes: 20 additions & 6 deletions src/readable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub trait Readable {
fn perform_read(&mut self, buf: &mut [u8]) -> io::Result<usize>;
}

#[allow(dead_code)]
type ReaderResult<T> = std::result::Result<T, LimitReaderError>;

pub(crate) mod falible {
#[allow(clippy::wildcard_imports)]
use super::*;
Expand Down Expand Up @@ -81,6 +84,7 @@ pub(crate) mod falible {
}

pub(crate) mod infalible {

#[allow(clippy::wildcard_imports)]
use super::*;

Expand Down Expand Up @@ -120,12 +124,22 @@ pub(crate) mod infalible {
R: Read,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let max_read = self.limit.min(buf.len() as u64); // min of limit and buf.len()

let bytes_read = self.reader.read(&mut buf[..max_read as usize])?;
self.reader_count += 1;

Ok(bytes_read)
match TryInto::<u64>::try_into(buf.len()) {
Ok(buf_len) => {
let max_read = self.limit.min(buf_len); // min of limit and buf.len()

match TryInto::<usize>::try_into(max_read) {
Ok(m) => {
let bytes_read = self.reader.read(&mut buf[..m])?;
self.reader_count += 1;

Ok(bytes_read)
}
Err(_) => Ok(0),
}
}
Err(_) => Ok(0),
}
}
}

Expand Down

0 comments on commit 7dffa87

Please sign in to comment.