Skip to content

Commit 0e7aa81

Browse files
committed
refactor
* Thanks clippy * typed errors
1 parent 5a2361b commit 0e7aa81

File tree

7 files changed

+74
-42
lines changed

7 files changed

+74
-42
lines changed

gix-features/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
doc = ::document_features::document_features!()
1313
)]
1414
#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg, doc_auto_cfg))]
15-
#![deny(rust_2018_idioms)]
15+
#![deny(rust_2018_idioms, missing_docs)]
1616

1717
///
1818
pub mod cache;

gix-features/src/zlib/mod.rs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
use std::fmt::Display;
1+
use std::ffi::c_int;
22

3+
/// A type to hold all state needed for decompressing a ZLIB encoded stream.
34
pub struct Decompress(Box<libz_rs_sys::z_stream>);
45

56
unsafe impl Sync for Decompress {}
67
unsafe impl Send for Decompress {}
78

9+
impl Default for Decompress {
10+
fn default() -> Self {
11+
Self::new()
12+
}
13+
}
14+
815
impl Decompress {
16+
/// The amount of bytes consumed from the input so far.
917
pub fn total_in(&self) -> u64 {
10-
self.0.total_in
18+
self.0.total_in as _
1119
}
1220

21+
/// The amount of decompressed bytes that have been written to the output thus far.
1322
pub fn total_out(&self) -> u64 {
14-
self.0.total_out
23+
self.0.total_out as _
1524
}
1625

17-
pub fn new(_zlib_header: bool) -> Self {
26+
/// Create a new instance. Note that it allocates in various ways and thus should be re-used.
27+
pub fn new() -> Self {
1828
let mut this = Box::new(libz_rs_sys::z_stream::default());
1929

2030
unsafe {
@@ -28,10 +38,12 @@ impl Decompress {
2838
Self(this)
2939
}
3040

31-
pub fn reset(&mut self, _: bool) {
41+
/// Reset the state to allow handling a new stream.
42+
pub fn reset(&mut self) {
3243
unsafe { libz_rs_sys::inflateReset(&mut *self.0) };
3344
}
3445

46+
/// Decompress `input` and write all decompressed bytes into `output`, with `flush` defining some details about this.
3547
pub fn decompress(
3648
&mut self,
3749
input: &[u8],
@@ -49,11 +61,10 @@ impl Decompress {
4961
libz_rs_sys::Z_BUF_ERROR => Ok(Status::BufError),
5062
libz_rs_sys::Z_STREAM_END => Ok(Status::StreamEnd),
5163

52-
libz_rs_sys::Z_STREAM_ERROR => Err(DecompressError("stream error")),
53-
libz_rs_sys::Z_DATA_ERROR => Err(DecompressError("data error")),
54-
libz_rs_sys::Z_MEM_ERROR => Err(DecompressError("insufficient memory")),
55-
libz_rs_sys::Z_NEED_DICT => Err(DecompressError("need dictionary")),
56-
c => panic!("unknown return code: {}", c),
64+
libz_rs_sys::Z_STREAM_ERROR => Err(DecompressError::StreamError),
65+
libz_rs_sys::Z_DATA_ERROR => Err(DecompressError::DataError),
66+
libz_rs_sys::Z_MEM_ERROR => Err(DecompressError::InsufficientMemory),
67+
err => Err(DecompressError::Unknown { err }),
5768
}
5869
}
5970
}
@@ -64,19 +75,29 @@ impl Drop for Decompress {
6475
}
6576
}
6677

78+
/// The error produced by [`Decompress::decompress()`].
6779
#[derive(Debug, thiserror::Error)]
68-
pub struct DecompressError(&'static str);
69-
70-
impl Display for DecompressError {
71-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72-
f.write_str(self.0)
73-
}
80+
#[allow(missing_docs)]
81+
pub enum DecompressError {
82+
#[error("stream error")]
83+
StreamError,
84+
#[error("Not enough memory")]
85+
InsufficientMemory,
86+
#[error("Invalid input data")]
87+
DataError,
88+
#[error("An unknown error occurred: {err}")]
89+
Unknown { err: c_int },
7490
}
7591

92+
/// The status returned by [`Decompress::decompress()`].
7693
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7794
pub enum Status {
95+
/// The decompress operation went well. Not to be confused with `StreamEnd`, so one can continue
96+
/// the decompression.
7897
Ok,
98+
/// An error occurred when decompression.
7999
BufError,
100+
/// The stream was fully decompressed.
80101
StreamEnd,
81102
}
82103

@@ -123,19 +144,12 @@ pub mod inflate {
123144
}
124145

125146
/// Decompress a few bytes of a zlib stream without allocation
147+
#[derive(Default)]
126148
pub struct Inflate {
127149
/// The actual decompressor doing all the work.
128150
pub state: Decompress,
129151
}
130152

131-
impl Default for Inflate {
132-
fn default() -> Self {
133-
Inflate {
134-
state: Decompress::new(true),
135-
}
136-
}
137-
}
138-
139153
impl Inflate {
140154
/// Run the decompressor exactly once. Cannot be run multiple times
141155
pub fn once(&mut self, input: &[u8], out: &mut [u8]) -> Result<(Status, usize, usize), inflate::Error> {
@@ -151,7 +165,7 @@ impl Inflate {
151165

152166
/// Ready this instance for decoding another data stream.
153167
pub fn reset(&mut self) {
154-
self.state.reset(true);
168+
self.state.reset();
155169
}
156170
}
157171

gix-features/src/zlib/stream/deflate/mod.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::zlib::Status;
2+
use std::ffi::c_int;
23

34
const BUF_SIZE: usize = 4096 * 8;
45

@@ -24,20 +25,30 @@ where
2425
}
2526
}
2627

28+
/// Hold all state needed for compressing data.
2729
pub struct Compress(Box<libz_rs_sys::z_stream>);
2830

2931
unsafe impl Sync for Compress {}
3032
unsafe impl Send for Compress {}
3133

34+
impl Default for Compress {
35+
fn default() -> Self {
36+
Self::new()
37+
}
38+
}
39+
3240
impl Compress {
41+
/// The number of bytes that were read from the input.
3342
pub fn total_in(&self) -> u64 {
34-
self.0.total_in
43+
self.0.total_in as _
3544
}
3645

46+
/// The number of compressed bytes that were written to the output.
3747
pub fn total_out(&self) -> u64 {
38-
self.0.total_out
48+
self.0.total_out as _
3949
}
4050

51+
/// Create a new instance - this allocates so should be done with care.
4152
pub fn new() -> Self {
4253
let mut this = Box::new(libz_rs_sys::z_stream::default());
4354

@@ -53,10 +64,12 @@ impl Compress {
5364
Self(this)
5465
}
5566

67+
/// Prepare the instance for a new stream.
5668
pub fn reset(&mut self) {
5769
unsafe { libz_rs_sys::deflateReset(&mut *self.0) };
5870
}
5971

72+
/// Compress `input` and write compressed bytes to `output`, with `flush` controlling additional characteristics.
6073
pub fn compress(&mut self, input: &[u8], output: &mut [u8], flush: FlushCompress) -> Result<Status, CompressError> {
6174
self.0.avail_in = input.len() as _;
6275
self.0.avail_out = output.len() as _;
@@ -69,8 +82,9 @@ impl Compress {
6982
libz_rs_sys::Z_BUF_ERROR => Ok(Status::BufError),
7083
libz_rs_sys::Z_STREAM_END => Ok(Status::StreamEnd),
7184

72-
libz_rs_sys::Z_STREAM_ERROR => Err(CompressError("stream error")),
73-
_ => todo!(),
85+
libz_rs_sys::Z_STREAM_ERROR => Err(CompressError::StreamError),
86+
libz_rs_sys::Z_MEM_ERROR => Err(CompressError::InsufficientMemory),
87+
err => Err(CompressError::Unknown { err }),
7488
}
7589
}
7690
}
@@ -81,13 +95,17 @@ impl Drop for Compress {
8195
}
8296
}
8397

98+
/// The error produced by [`Compress::compress()`].
8499
#[derive(Debug, thiserror::Error)]
85-
pub struct CompressError(&'static str);
86-
87-
impl std::fmt::Display for CompressError {
88-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89-
f.write_str(self.0)
90-
}
100+
#[error("{msg}")]
101+
#[allow(missing_docs)]
102+
pub enum CompressError {
103+
#[error("stream error")]
104+
StreamError,
105+
#[error("Not enough memory")]
106+
InsufficientMemory,
107+
#[error("An unknown error occurred: {err}")]
108+
Unknown { err: c_int },
91109
}
92110

93111
/// Values which indicate the form of flushing to be used when compressing

gix-features/src/zlib/stream/deflate/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod deflate_stream {
2222
{
2323
pub fn from_read(read: R) -> InflateReader<R> {
2424
InflateReader {
25-
decompressor: Decompress::new(true),
25+
decompressor: Decompress::new(),
2626
inner: read,
2727
}
2828
}

gix-pack/src/data/input/bytes_to_entries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ where
6262
);
6363
Ok(BytesToEntriesIter {
6464
read,
65-
decompressor: Decompress::new(true),
65+
decompressor: Decompress::new(),
6666
compressed,
6767
offset: 12,
6868
had_error: false,
@@ -101,7 +101,7 @@ where
101101

102102
// Decompress object to learn its compressed bytes
103103
let compressed_buf = self.compressed_buf.take().unwrap_or_else(|| Vec::with_capacity(4096));
104-
self.decompressor.reset(true);
104+
self.decompressor.reset();
105105
let mut decompressed_reader = DecompressRead {
106106
inner: read_and_pass_to(
107107
&mut self.read,

gix/tests/gix/repository/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ mod dirwalk {
134134
#[test]
135135
fn size_in_memory() {
136136
let actual_size = std::mem::size_of::<Repository>();
137-
let limit = 1200;
137+
let limit = 1250;
138138
assert!(
139139
actual_size <= limit,
140140
"size of Repository shouldn't change without us noticing, it's meant to be cloned: should have been below {limit:?}, was {actual_size} (bigger on windows)"

gix/tests/gix/revision/spec/from_bytes/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ fn bad_objects_are_valid_until_they_are_actually_read_from_the_odb() {
126126
Spec::from_id(hex_to_id("cafea31147e840161a1860c50af999917ae1536b").attach(&repo))
127127
);
128128
assert_eq!(
129-
&format!("{:?}", parse_spec("cafea^{object}", &repo).unwrap_err())[..80],
130-
r#"FindObject(Find(Loose(DecompressFile { source: Inflate(DecompressError("data err"#
129+
&format!("{:?}", parse_spec("cafea^{object}", &repo).unwrap_err())[..65],
130+
r#"FindObject(Find(Loose(DecompressFile { source: Inflate(DataError)"#
131131
);
132132
}
133133
}

0 commit comments

Comments
 (0)