Rev to 0.7.0 after updating read & read_and
`TailedFile::read` is public again, and no longer takes a `&File` as `TailedFile` already has a `Path` that is of use. The method now checks rotation/truncation, does the read, and returns `Result<Vec<u8>>` for users who may just want the bytes. `TailedFile::read_and` now calls `read` and passes the resultant underlying `&[u8]` to a user defined function that is its only argument. Tests updated accordingly. Update made after discussion with user Thomas Herzog <@un9l:matrix.org> in https://matrix.to/#/#staart:txrx.staart.one on 2023-01-12.
This commit is contained in:
parent
26e3f301c0
commit
c8b172de6e
|
@ -58,7 +58,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "staart"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
dependencies = [
|
||||
"tempfile",
|
||||
]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "staart"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
authors = ["Anthony Martinez <anthony@ajmartinez.com>"]
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
|
18
src/lib.rs
18
src/lib.rs
|
@ -72,8 +72,13 @@ where
|
|||
|
||||
/// Reads new data for an instance of `staart::TailedFile` and returns
|
||||
/// `Result<Vec<u8>>`
|
||||
fn read(&mut self, file: &File) -> Result<Vec<u8>> {
|
||||
let mut reader = BufReader::with_capacity(65536, file);
|
||||
///
|
||||
/// Prior to reading the file, it is checked for rotation and/or truncation.
|
||||
pub fn read(&mut self) -> Result<Vec<u8>> {
|
||||
let fd = File::open(self.path)?;
|
||||
self.check_rotate(&fd)?;
|
||||
self.check_truncate(&fd)?;
|
||||
let mut reader = BufReader::with_capacity(65536, &fd);
|
||||
let mut data: [u8; 65536] = [0u8; 65536];
|
||||
reader.seek(SeekFrom::Start(self.pos))?;
|
||||
let n: u64 = reader.read(&mut data)?.try_into()?;
|
||||
|
@ -87,10 +92,7 @@ where
|
|||
|
||||
/// Passes `&Vec<u8>` read from the tailed file to a user-defined function returning the unit type ()`.
|
||||
pub fn read_and<F: Fn(&[u8])>(&mut self, f: F) -> Result<()> {
|
||||
let fd = File::open(self.path)?;
|
||||
self.check_rotate(&fd)?;
|
||||
self.check_truncate(&fd)?;
|
||||
let data = self.read(&fd)?;
|
||||
let data = self.read()?;
|
||||
|
||||
f(&data);
|
||||
|
||||
|
@ -181,9 +183,7 @@ mod tests {
|
|||
|
||||
f.write_all(test_data).unwrap();
|
||||
|
||||
let f = File::open(path).unwrap();
|
||||
|
||||
let data = tailed_file.read(&f).unwrap();
|
||||
let data = tailed_file.read().unwrap();
|
||||
assert_eq!(data.len(), test_data.len());
|
||||
assert_eq!(tailed_file.pos, 9);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue