Skip to content

Commit

Permalink
remove redundant File::reader fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Jul 17, 2024
1 parent c883a63 commit d51dc50
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
4 changes: 1 addition & 3 deletions hermes/bin/src/hdf5/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,12 @@ mod tests {
dir.copy_resource_file(&FsResource::new(file_1), file_1_name.into())
.expect("Failed to copy file to package.");

let file_1 = dir
let mut file_1 = dir
.get_file(file_1_name.into())
.expect("Failed to get file.");

let mut data = Vec::new();
file_1
.reader()
.expect("Failed to get file reader.")
.read_to_end(&mut data)
.expect("Failed to read file's data.");
assert_eq!(data.as_slice(), file_content);
Expand Down
43 changes: 36 additions & 7 deletions hermes/bin/src/hdf5/file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! A Hermes HDF5 file abstraction over the HDF5 dataset object.

use std::io::Read;

use super::{compression::enable_compression, Path};

/// Hermes HDF5 file object, wrapper of `hdf5::Dataset`
Expand Down Expand Up @@ -39,11 +37,6 @@ impl File {
Path::from_str(&self.hdf5_ds.name())
}

/// Return file reader.
pub(crate) fn reader(&self) -> anyhow::Result<impl Read> {
Ok(self.hdf5_ds.as_byte_reader()?)
}

/// Return file size.
fn size(&self) -> anyhow::Result<usize> {
let dataspace = self.hdf5_ds.space()?;
Expand Down Expand Up @@ -85,8 +78,44 @@ impl std::io::Read for File {
}
}

impl std::io::Seek for File {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
let (base_pos, offset) = match pos {
std::io::SeekFrom::Start(n) => {
self.pos = n.try_into().map_err(map_to_io_error)?;
return Ok(n);
},
std::io::SeekFrom::End(n) => (self.size().map_err(map_to_io_error)?, n),
std::io::SeekFrom::Current(n) => (self.pos, n),
};
let new_pos = if offset.is_negative() {
base_pos.checked_sub(offset.wrapping_abs().try_into().map_err(map_to_io_error)?)
} else {
base_pos.checked_add(offset.try_into().map_err(map_to_io_error)?)
};
match new_pos {
Some(n) => {
self.pos = n;
Ok(self.pos.try_into().map_err(map_to_io_error)?)
},
None => {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid seek to a negative or overflowing position",
))
},
}
}

fn stream_position(&mut self) -> std::io::Result<u64> {
self.pos.try_into().map_err(map_to_io_error)
}
}

#[cfg(test)]
mod tests {
use std::io::Read;

use temp_dir::TempDir;

use super::*;
Expand Down
4 changes: 2 additions & 2 deletions hermes/bin/src/packaging/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl ApplicationPackage {
self.0
.get_file(Self::METADATA_FILE.into())
.map_err(|_| MissingPackageFileError(Self::METADATA_FILE.to_string()))
.map(|file| Metadata::<Self>::from_reader(file.reader()?))?
.map(Metadata::<Self>::from_reader)?
}

/// Get author `Signature` object from package.
Expand All @@ -215,7 +215,7 @@ impl ApplicationPackage {
self.0
.get_file(Self::AUTHOR_COSE_FILE.into())
.ok()
.map(|file| Signature::<author_payload::SignaturePayload>::from_reader(file.reader()?))
.map(Signature::<author_payload::SignaturePayload>::from_reader)
.transpose()
}

Expand Down
13 changes: 6 additions & 7 deletions hermes/bin/src/packaging/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ impl Package {
/// Calculates file hash, if present
pub(crate) fn calculate_file_hash(&self, path: Path) -> anyhow::Result<Option<Blake2b256>> {
let mut hasher = Blake2b256Hasher::new();
let Ok(file) = self.0.get_file(path) else {
let Ok(mut file) = self.0.get_file(path) else {
return Ok(None);
};
calculate_file_hash(&file, &mut hasher)?;
calculate_file_hash(&mut file, &mut hasher)?;
Ok(Some(hasher.finalize()))
}

Expand All @@ -75,12 +75,11 @@ const BUFFER_SIZE: usize = 1024 * 1024;

/// Calculates file hash with the provided hasher.
#[allow(clippy::indexing_slicing)]
fn calculate_file_hash(file: &File, hasher: &mut Blake2b256Hasher) -> anyhow::Result<()> {
let mut reader = file.reader()?;
fn calculate_file_hash(file: &mut File, hasher: &mut Blake2b256Hasher) -> anyhow::Result<()> {
let mut buf = vec![0; BUFFER_SIZE];

loop {
let len = reader.read(&mut buf)?;
let len = file.read(&mut buf)?;
if len == 0 {
break;
}
Expand All @@ -105,9 +104,9 @@ fn calculate_dir_hash(dir: &Dir, hasher: &mut Blake2b256Hasher) -> anyhow::Resul
.into_iter()
.map(|dir| (dir.path().to_string(), dir))
.collect();
for (path_str, file) in files {
for (path_str, mut file) in files {
hasher.update(path_str.as_bytes());
calculate_file_hash(&file, hasher)?;
calculate_file_hash(&mut file, hasher)?;
}
for (path_str, dir) in dirs {
hasher.update(path_str.as_bytes());
Expand Down
12 changes: 6 additions & 6 deletions hermes/bin/src/packaging/wasm_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,23 @@ impl WasmModulePackage {
self.0
.get_file(Self::METADATA_FILE.into())
.map_err(|_| MissingPackageFileError(Self::METADATA_FILE.to_string()))
.map(|file| Metadata::<Self>::from_reader(file.reader()?))?
.map(Metadata::<Self>::from_reader)?
}

/// Get `wasm::module::Module` object from package.
pub(crate) fn get_component(&self) -> anyhow::Result<wasm::module::Module> {
self.0
.get_file(Self::COMPONENT_FILE.into())
.map_err(|_| MissingPackageFileError(Self::METADATA_FILE.to_string()))
.map(|file| wasm::module::Module::from_reader(file.reader()?))?
.map(wasm::module::Module::from_reader)?
}

/// Get `Signature` object from package.
pub(crate) fn get_signature(&self) -> anyhow::Result<Option<Signature<SignaturePayload>>> {
self.0
.get_file(Self::AUTHOR_COSE_FILE.into())
.ok()
.map(|file| Signature::<SignaturePayload>::from_reader(file.reader()?))
.map(Signature::<SignaturePayload>::from_reader)
.transpose()
}

Expand All @@ -213,7 +213,7 @@ impl WasmModulePackage {
self.0
.get_file(Self::CONFIG_SCHEMA_FILE.into())
.ok()
.map(|file| ConfigSchema::from_reader(file.reader()?))
.map(ConfigSchema::from_reader)
.transpose()
}

Expand All @@ -227,7 +227,7 @@ impl WasmModulePackage {
};

if let Ok(file) = self.0.get_file(Self::CONFIG_FILE.into()) {
let config_file = Config::from_reader(file.reader()?, config_schema.validator())?;
let config_file = Config::from_reader(file, config_schema.validator())?;
Ok((Some(config_file), Some(config_schema)))
} else {
Ok((None, Some(config_schema)))
Expand All @@ -239,7 +239,7 @@ impl WasmModulePackage {
self.0
.get_file(Self::SETTINGS_SCHEMA_FILE.into())
.ok()
.map(|file| SettingsSchema::from_reader(file.reader()?))
.map(SettingsSchema::from_reader)
.transpose()
}

Expand Down

0 comments on commit d51dc50

Please sign in to comment.