Skip to content

Commit

Permalink
Rename 'reader' API to state machine api
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Feb 2, 2024
1 parent c6d0980 commit 93d3518
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
cargo doc --all-features --no-deps
- name: Run cargo clippy
run: |
cargo hack clippy --feature-powerset --group-features deflate,deflate64,lzma,bzip2
cargo hack clippy --each-feature
- name: Run tests and collect coverage
run: just ci-test
- name: Upload coverage information
Expand Down
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _default:
just --list

check:
cargo hack clippy --feature-powerset --group-features deflate,deflate64,lzma,bzip2
cargo hack clippy --each-feature

# Run all tests locally
test *args:
Expand Down
2 changes: 1 addition & 1 deletion rc-zip-tokio/src/entry_reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn method_to_decoder(
if #[cfg(feature = "deflate")] {
Box::new(deflate_dec::mk_decoder(raw_r))
} else {
return Err(Error::method_not_enabled(self.method));
return Err(Error::method_not_enabled(method));
}
}
}
Expand Down
47 changes: 14 additions & 33 deletions rc-zip/src/reader.rs → rc-zip/src/reader/archive.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
use super::FsmResult;
use crate::{
encoding::Encoding, Archive, DirectoryHeader, EndOfCentralDirectory,
EndOfCentralDirectory64Locator, EndOfCentralDirectory64Record, EndOfCentralDirectoryRecord,
Error, FormatError, Located, StoredEntry,
};

macro_rules! transition {
($state: expr => ($pattern: pat) $body: expr) => {
$state = if let $pattern = std::mem::take(&mut $state) {
$body
} else {
unreachable!()
};
};
}

use tracing::trace;
use winnow::{
error::ErrMode,
Expand All @@ -30,15 +21,6 @@ pub struct ArchiveReader {
state: State,
}

pub enum ArchiveReaderResult {
/// Indicates that [ArchiveReader][] has work left, and the loop should continue.
Continue,
/// Indicates that [ArchiveReader][] is done reading the central directory,
/// contains an [Archive][]. Calling any method after [process()](ArchiveReader::process()) has returned
/// `Done` will panic.
Done(Archive),
}

#[derive(Default)]
enum State {
/// Finding and reading the end of central directory record
Expand Down Expand Up @@ -184,13 +166,12 @@ impl ArchiveReader {
/// Errors returned from process() are caused by invalid zip archives,
/// unsupported format quirks, or implementation bugs - never I/O errors.
///
/// A result of [ArchiveReaderResult::Continue] indicates one should loop again,
/// A result of [FsmResult::Continue] indicates one should loop again,
/// starting with [wants_read()](ArchiveReader::wants_read()).
///
/// A result of [ArchiveReaderResult::Done] contains the [Archive], and indicates that no
/// A result of [FsmResult::Done] contains the [Archive], and indicates that no
/// method should ever be called again on this reader.
pub fn process(&mut self) -> Result<ArchiveReaderResult, Error> {
use ArchiveReaderResult as R;
pub fn process(&mut self) -> Result<FsmResult<Archive>, Error> {
use State as S;
match self.state {
S::ReadEocd {
Expand All @@ -203,7 +184,7 @@ impl ArchiveReader {
haystack_size,
"ReadEocd | need more data"
);
return Ok(R::Continue);
return Ok(FsmResult::Continue);
}

match {
Expand Down Expand Up @@ -235,14 +216,14 @@ impl ArchiveReader {
directory_headers: vec![],
}
});
Ok(R::Continue)
Ok(FsmResult::Continue)
} else {
trace!("ReadEocd | transition to ReadEocd64Locator");
transition!(self.state => (S::ReadEocd { mut buffer, .. }) {
buffer.reset();
S::ReadEocd64Locator { buffer, eocdr }
});
Ok(R::Continue)
Ok(FsmResult::Continue)
}
}
}
Expand All @@ -252,7 +233,7 @@ impl ArchiveReader {
match EndOfCentralDirectory64Locator::parser.parse_peek(input) {
Err(ErrMode::Incomplete(_)) => {
// need more data
Ok(R::Continue)
Ok(FsmResult::Continue)
}
Err(ErrMode::Backtrack(_)) | Err(ErrMode::Cut(_)) => {
// we don't have a zip64 end of central directory locator - that's ok!
Expand All @@ -266,7 +247,7 @@ impl ArchiveReader {
directory_headers: vec![],
}
});
Ok(R::Continue)
Ok(FsmResult::Continue)
}
Ok((_, locator)) => {
trace!(
Expand All @@ -281,7 +262,7 @@ impl ArchiveReader {
eocdr,
}
});
Ok(R::Continue)
Ok(FsmResult::Continue)
}
}
}
Expand All @@ -290,7 +271,7 @@ impl ArchiveReader {
match EndOfCentralDirectory64Record::parser.parse_peek(input) {
Err(ErrMode::Incomplete(_)) => {
// need more data
Ok(R::Continue)
Ok(FsmResult::Continue)
}
Err(ErrMode::Backtrack(_)) | Err(ErrMode::Cut(_)) => {
// at this point, we really expected to have a zip64 end
Expand All @@ -310,7 +291,7 @@ impl ArchiveReader {
directory_headers: vec![],
}
});
Ok(R::Continue)
Ok(FsmResult::Continue)
}
}
}
Expand Down Expand Up @@ -422,7 +403,7 @@ impl ArchiveReader {
}

self.state = S::Done;
return Ok(R::Done(Archive {
return Ok(FsmResult::Done(Archive {
size: self.size,
comment,
entries,
Expand All @@ -445,7 +426,7 @@ impl ArchiveReader {
buffer.consume(consumed);

// need more data
Ok(R::Continue)
Ok(FsmResult::Continue)
}
S::Done { .. } => panic!("Called process() on ArchiveReader in Done state"),
S::Transitioning => unreachable!(),
Expand Down
1 change: 1 addition & 0 deletions rc-zip/src/reader/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

24 changes: 24 additions & 0 deletions rc-zip/src/reader/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
macro_rules! transition {
($state: expr => ($pattern: pat) $body: expr) => {
$state = if let $pattern = std::mem::take(&mut $state) {
$body
} else {
unreachable!()
};
};
}

mod archive;
pub use archive::ArchiveReader;

mod entry;

/// Indicates whether or not the state machine has completed its work
pub enum FsmResult<T> {
/// Indicates that the state machine still has work to do, and
/// needs either data or a call to process
Continue,
/// Indicates that the state machine has completed its work, and
/// the result is the value provided
Done(T),
}

0 comments on commit 93d3518

Please sign in to comment.