anyreader_walker

Struct FileEntry

Source
pub struct FileEntry<T: Read> { /* private fields */ }
Expand description

A FileEntry represents a file in an archive, along with its format and size. It can be used to read the file’s contents, and can also be used to visit the contents of an archive.

§Example

This walks a nested tar file

use std::path::PathBuf;
use anyreader_walker::{FileEntry, AnyWalker, EntryDetails, FormatKind, ArchiveStack};
// Create a tar archive containing a nested tar archive, containing a nested zip archive
let tar_archive = tar_archive([
    ("test", b"Hello, world!".to_vec()),
    ("nested.tar", tar_archive([
        ("nested", b"Hello, nested!".to_vec()),
        ("nested2", b"Hello, nested2!".to_vec()),
        ("nested_zip", zip_archive([("nested3", "Hello, nested zip!")]))
    ])),
]);
let entry = FileEntry::from_bytes("archive.tar.gz", tar_archive).unwrap();

#[derive(Default)]
struct Visitor {
   names: Vec<PathBuf>,
   stack: ArchiveStack
};

impl AnyWalker for Visitor {
    fn visit_file_entry(&mut self, entry: &mut FileEntry<impl Read>) -> std::io::Result<()> {
        self.names.push(self.stack.nested_path().join(&entry.path()));
        Ok(())
    }

    fn begin_visit_archive(&mut self, details: &EntryDetails, format: FormatKind) -> std::io::Result<bool> {
        self.stack.push_archive(&details.path);
        Ok(true)
    }
    fn end_visit_archive(&mut self, details: EntryDetails, format: FormatKind) -> std::io::Result<()> {
       self.stack.pop_archive();
       Ok(())
   }
}

let mut visitor = Visitor::default();
visitor.walk(entry).unwrap();

let names = visitor.names.iter().map(|p| p.to_str().unwrap()).collect::<Vec<_>>();
assert_eq!(names, [
    "archive.tar.gz/test",
    "archive.tar.gz/nested.tar/nested",
    "archive.tar.gz/nested.tar/nested2",
    "archive.tar.gz/nested.tar/nested_zip/nested3"
]);

Implementations§

Source§

impl FileEntry<BufReader<File>>

Source

pub fn from_path(path: impl AsRef<Path>) -> Result<Self>

Examples found in repository?
examples/simple.rs (line 32)
31
32
33
34
35
fn main() {
    let entry = FileEntry::from_path("file.tar.gz").unwrap();
    let mut visitor = Visitor {};
    visitor.walk(entry).unwrap();
}
More examples
Hide additional examples
examples/count_files.rs (line 62)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
fn main() -> std::io::Result<()> {
    let path = PathBuf::from(args().nth(1).unwrap());
    println!("{path:?}");

    let mut elapsed = Duration::from_secs(0);

    let mut visitor = Visitor::default();

    for item in std::fs::read_dir(&path)? {
        let item = item?;
        let path = item.path();
        if path.is_dir() {
            continue;
        }
        let start = Instant::now();
        let entry = FileEntry::from_path(path.clone())?;
        visitor.walk(entry)?;
        elapsed += start.elapsed();
    }
    println!("Total files: {}", visitor.count);
    println!("Total size: {}", visitor.size);

    let elapsed = elapsed.as_secs_f64();
    println!("Elapsed: {:.3}s", elapsed);
    println!("Throughput: {:.3} files/s", visitor.count as f64 / elapsed);
    println!(
        "Throughput: {:.3} MB/s",
        visitor.size as f64 / elapsed / 1024.0 / 1024.0
    );

    let mut counts: Vec<_> = visitor.format_counts.into_iter().collect();
    counts.sort_by_key(|(_, count)| *count);

    for (format, count) in counts {
        println!("{:?}: {}", format, count);
    }

    Ok(())
}
Source§

impl FileEntry<Reader<Bytes>>

Source

pub fn from_bytes( path: impl AsRef<Path>, data: impl Into<Bytes>, ) -> Result<FileEntry<Reader<Bytes>>>

Source§

impl<T: Read> FileEntry<T>

Source

pub fn new(path: PathBuf, size: u64, format: AnyFormat<T>) -> Self

Source

pub fn from_reader(path: PathBuf, size: u64, reader: T) -> Result<FileEntry<T>>

Source

pub fn into_components(self) -> (EntryDetails, AnyFormat<T>)

Source

pub fn details(&self) -> &EntryDetails

Source

pub fn path(&self) -> &Path

Examples found in repository?
examples/simple.rs (line 12)
8
9
10
11
12
13
14
15
    fn visit_file_entry(&mut self, entry: &mut FileEntry<impl Read>) -> std::io::Result<()> {
        eprintln!(
            "Found file with format {}: {}",
            entry.format(),
            entry.path().display()
        );
        Ok(())
    }
Source

pub fn size(&self) -> u64

Examples found in repository?
examples/count_files.rs (line 20)
18
19
20
21
22
23
24
25
26
    fn visit_file_entry(&mut self, entry: &mut FileEntry<impl Read>) -> std::io::Result<()> {
        self.count += 1;
        self.size += entry.size();
        self.format_counts
            .entry(entry.format())
            .and_modify(|c| *c += 1)
            .or_insert(1);
        Ok(())
    }
Source

pub fn supports_recursion(&self) -> bool

Source

pub fn format(&self) -> FormatKind

Examples found in repository?
examples/simple.rs (line 11)
8
9
10
11
12
13
14
15
    fn visit_file_entry(&mut self, entry: &mut FileEntry<impl Read>) -> std::io::Result<()> {
        eprintln!(
            "Found file with format {}: {}",
            entry.format(),
            entry.path().display()
        );
        Ok(())
    }
More examples
Hide additional examples
examples/count_files.rs (line 22)
18
19
20
21
22
23
24
25
26
    fn visit_file_entry(&mut self, entry: &mut FileEntry<impl Read>) -> std::io::Result<()> {
        self.count += 1;
        self.size += entry.size();
        self.format_counts
            .entry(entry.format())
            .and_modify(|c| *c += 1)
            .or_insert(1);
        Ok(())
    }
Source

pub fn get_ref(&self) -> &T

Trait Implementations§

Source§

impl<T: Read> Debug for FileEntry<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Read> Read for FileEntry<T>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more

Auto Trait Implementations§

§

impl<T> Freeze for FileEntry<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for FileEntry<T>

§

impl<T> Send for FileEntry<T>
where T: Send,

§

impl<T> Sync for FileEntry<T>
where T: Sync,

§

impl<T> Unpin for FileEntry<T>
where T: Unpin,

§

impl<T> !UnwindSafe for FileEntry<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<R> LittleEndianReadExt for R
where R: Read,

Source§

fn read_u16_le(&mut self) -> Result<u16, Error>

Source§

fn read_u32_le(&mut self) -> Result<u32, Error>

Source§

fn read_u64_le(&mut self) -> Result<u64, Error>

Source§

impl<R> PeekExt for R
where R: Read + ?Sized,

Source§

fn peekable(self) -> Peekable<Self>
where Self: Sized,

Wraps a Read type in a Peekable which provides a peek related methods.
Source§

fn peekable_with_capacity(self, capacity: usize) -> Peekable<Self>
where Self: Sized,

Wraps a Read type in a Peekable which provides a peek related methods with a specified capacity.
Source§

impl<R> ReadBytesExt for R
where R: Read + ?Sized,

Source§

fn read_u8(&mut self) -> Result<u8, Error>

Reads an unsigned 8 bit integer from the underlying reader. Read more
Source§

fn read_i8(&mut self) -> Result<i8, Error>

Reads a signed 8 bit integer from the underlying reader. Read more
Source§

fn read_u16<T>(&mut self) -> Result<u16, Error>
where T: ByteOrder,

Reads an unsigned 16 bit integer from the underlying reader. Read more
Source§

fn read_i16<T>(&mut self) -> Result<i16, Error>
where T: ByteOrder,

Reads a signed 16 bit integer from the underlying reader. Read more
Source§

fn read_u24<T>(&mut self) -> Result<u32, Error>
where T: ByteOrder,

Reads an unsigned 24 bit integer from the underlying reader. Read more
Source§

fn read_i24<T>(&mut self) -> Result<i32, Error>
where T: ByteOrder,

Reads a signed 24 bit integer from the underlying reader. Read more
Source§

fn read_u32<T>(&mut self) -> Result<u32, Error>
where T: ByteOrder,

Reads an unsigned 32 bit integer from the underlying reader. Read more
Source§

fn read_i32<T>(&mut self) -> Result<i32, Error>
where T: ByteOrder,

Reads a signed 32 bit integer from the underlying reader. Read more
Source§

fn read_u48<T>(&mut self) -> Result<u64, Error>
where T: ByteOrder,

Reads an unsigned 48 bit integer from the underlying reader. Read more
Source§

fn read_i48<T>(&mut self) -> Result<i64, Error>
where T: ByteOrder,

Reads a signed 48 bit integer from the underlying reader. Read more
Source§

fn read_u64<T>(&mut self) -> Result<u64, Error>
where T: ByteOrder,

Reads an unsigned 64 bit integer from the underlying reader. Read more
Source§

fn read_i64<T>(&mut self) -> Result<i64, Error>
where T: ByteOrder,

Reads a signed 64 bit integer from the underlying reader. Read more
Source§

fn read_u128<T>(&mut self) -> Result<u128, Error>
where T: ByteOrder,

Reads an unsigned 128 bit integer from the underlying reader. Read more
Source§

fn read_i128<T>(&mut self) -> Result<i128, Error>
where T: ByteOrder,

Reads a signed 128 bit integer from the underlying reader. Read more
Source§

fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>
where T: ByteOrder,

Reads an unsigned n-bytes integer from the underlying reader. Read more
Source§

fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>
where T: ByteOrder,

Reads a signed n-bytes integer from the underlying reader. Read more
Source§

fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>
where T: ByteOrder,

Reads an unsigned n-bytes integer from the underlying reader.
Source§

fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>
where T: ByteOrder,

Reads a signed n-bytes integer from the underlying reader.
Source§

fn read_f32<T>(&mut self) -> Result<f32, Error>
where T: ByteOrder,

Reads a IEEE754 single-precision (4 bytes) floating point number from the underlying reader. Read more
Source§

fn read_f64<T>(&mut self) -> Result<f64, Error>
where T: ByteOrder,

Reads a IEEE754 double-precision (8 bytes) floating point number from the underlying reader. Read more
Source§

fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of unsigned 16 bit integers from the underlying reader. Read more
Source§

fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of unsigned 32 bit integers from the underlying reader. Read more
Source§

fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of unsigned 64 bit integers from the underlying reader. Read more
Source§

fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of unsigned 128 bit integers from the underlying reader. Read more
Source§

fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>

Reads a sequence of signed 8 bit integers from the underlying reader. Read more
Source§

fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of signed 16 bit integers from the underlying reader. Read more
Source§

fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of signed 32 bit integers from the underlying reader. Read more
Source§

fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of signed 64 bit integers from the underlying reader. Read more
Source§

fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of signed 128 bit integers from the underlying reader. Read more
Source§

fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of IEEE754 single-precision (4 bytes) floating point numbers from the underlying reader. Read more
Source§

fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>
where T: ByteOrder,

👎Deprecated since 1.2.0: please use read_f32_into instead
DEPRECATED. Read more
Source§

fn read_f64_into<T>(&mut self, dst: &mut [f64]) -> Result<(), Error>
where T: ByteOrder,

Reads a sequence of IEEE754 double-precision (8 bytes) floating point numbers from the underlying reader. Read more
Source§

fn read_f64_into_unchecked<T>(&mut self, dst: &mut [f64]) -> Result<(), Error>
where T: ByteOrder,

👎Deprecated since 1.2.0: please use read_f64_into instead
DEPRECATED. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more