refactor: add digest_file for hashing file content#6855
Conversation
WalkthroughThe PR introduces streaming hash utility functions in a new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
✨ Simplify code
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/utils/hash/mod.rs (2)
14-15: Minor:H: Updatebound may be redundant.In
digest0.11, theDigesttrait already requiresUpdate, soH: Digestalone should suffice. This is a very minor nit and doesn't affect correctness.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils/hash/mod.rs` around lines 14 - 15, The generic bound on H in the function/impl (the clause "where H: Digest + Update") is redundant because the Digest trait already requires Update in digest 0.11; remove the explicit "Update" bound and leave "where H: Digest" (update any occurrences of that where-clause on the functions/impls referencing H such as the Digest-related function signatures) to simplify the constraints.
12-30: Consider adding doc comments for public functions.These functions are
puband will be part of the module's API. Adding brief doc comments would improve discoverability and maintainability.📝 Suggested doc comments
+/// Computes the digest of data read from a stream. +/// +/// Reads from `reader` until EOF, feeding all bytes into the hasher `H`, +/// and returns the finalized digest. #[allow(dead_code)] pub fn digest_stream<H, R>(reader: &mut R) -> std::io::Result<Output<H>> where H: Digest + Update, R: Read, { let mut hasher = IoWrapper(H::new()); std::io::copy(reader, &mut hasher)?; Ok(hasher.0.finalize()) } +/// Computes the digest of a file's contents. +/// +/// Opens the file at `path`, reads it through a buffered reader, +/// and returns the finalized digest. #[allow(dead_code)] pub fn digest_file<H>(path: impl AsRef<Path>) -> std::io::Result<Output<H>> where H: Digest + Update, { let mut reader = BufReader::new(File::open(path)?); digest_stream::<H, _>(&mut reader) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils/hash/mod.rs` around lines 12 - 30, Add brief doc comments for the public functions to document their purpose, parameters and return value: add a doc comment above pub fn digest_stream<H, R>(reader: &mut R) describing that it reads from the given reader, computes and returns the digest Output<H> and may return std::io::Error, and add a doc comment above pub fn digest_file<H>(path: impl AsRef<Path>) describing that it opens the file at path, wraps it in a BufReader, and returns the computed digest via digest_stream; mention trait bounds (H: Digest + Update) and the possible I/O errors in both comments.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/utils/hash/mod.rs`:
- Around line 14-15: The generic bound on H in the function/impl (the clause
"where H: Digest + Update") is redundant because the Digest trait already
requires Update in digest 0.11; remove the explicit "Update" bound and leave
"where H: Digest" (update any occurrences of that where-clause on the
functions/impls referencing H such as the Digest-related function signatures) to
simplify the constraints.
- Around line 12-30: Add brief doc comments for the public functions to document
their purpose, parameters and return value: add a doc comment above pub fn
digest_stream<H, R>(reader: &mut R) describing that it reads from the given
reader, computes and returns the digest Output<H> and may return std::io::Error,
and add a doc comment above pub fn digest_file<H>(path: impl AsRef<Path>)
describing that it opens the file at path, wraps it in a BufReader, and returns
the computed digest via digest_stream; mention trait bounds (H: Digest + Update)
and the possible I/O errors in both comments.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: fb8162e2-4755-43d8-9cdd-5a2f50e75a55
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (7)
Cargo.tomlsrc/state_manager/utils.rssrc/utils/hash/mod.rssrc/utils/io/writer_checksum.rssrc/utils/mod.rssrc/utils/net/download_file.rssrc/utils/proofs_api/parameters.rs
Codecov Report❌ Patch coverage is Additional details and impacted files
... and 5 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
Summary of changes
Changes introduced in this pull request:
Reference issue to close (if applicable)
Closes
Other information and links
Change checklist
Outside contributions