refactor: reduce quality findings

This commit is contained in:
2026-06-28 15:42:12 +08:00
parent 02716d3ccd
commit 3c00659691
11 changed files with 189 additions and 135 deletions
+11 -8
View File
@@ -29,8 +29,9 @@ impl Hash {
/// 从十六进制字符串解析
pub fn from_hex(s: &str) -> Result<Self, crate::error::CasError> {
let bytes =
hex::decode(s).map_err(|_| crate::error::CasError::InvalidHash(s.to_string()))?;
let decoded =
hex::decode(s).map_err(|_| crate::error::CasError::InvalidHash(s.to_string()));
let bytes = decoded?;
if bytes.len() != 32 {
return Err(crate::error::CasError::InvalidHash(s.to_string()));
@@ -113,6 +114,12 @@ pub async fn compute_file_hash(path: &std::path::Path) -> crate::error::Result<H
mod tests {
use super::*;
fn test_hash_and_hex() -> (Hash, String) {
let hash = compute_hash(b"Test data");
let hex = hash.to_hex();
(hash, hex)
}
#[test]
fn test_compute_hash() {
let data = b"Hello, World!";
@@ -125,9 +132,7 @@ mod tests {
#[test]
fn test_hash_to_string() {
let data = b"Test data";
let hash = compute_hash(data);
let hex = hash.to_hex();
let (_hash, hex) = test_hash_and_hex();
// 应该是 64 个字符的十六进制字符串
assert_eq!(hex.len(), 64);
@@ -135,9 +140,7 @@ mod tests {
#[test]
fn test_hash_from_string() {
let data = b"Test data";
let hash = compute_hash(data);
let hex = hash.to_hex();
let (hash, hex) = test_hash_and_hex();
let parsed = Hash::from_hex(&hex).unwrap();
assert_eq!(hash, parsed);