From 9c38ce66fb740298095e7d908959c522ec927943 Mon Sep 17 00:00:00 2001 From: Quanyi Ma Date: Tue, 4 Jun 2024 00:48:56 +0800 Subject: [PATCH] Refactoring utils of mercury and libra Signed-off-by: Quanyi Ma --- libra/src/utils/client_storage.rs | 6 +++--- libra/src/utils/util.rs | 6 ------ mercury/src/internal/index.rs | 21 +-------------------- mercury/src/lib.rs | 1 + mercury/src/utils.rs | 18 ++++++++++++++++++ 5 files changed, 23 insertions(+), 29 deletions(-) create mode 100644 mercury/src/utils.rs diff --git a/libra/src/utils/client_storage.rs b/libra/src/utils/client_storage.rs index 0c25b8150..5c228390c 100644 --- a/libra/src/utils/client_storage.rs +++ b/libra/src/utils/client_storage.rs @@ -15,9 +15,9 @@ use mercury::internal::pack::Pack; use mercury::errors::GitError; use mercury::hash::SHA1; use mercury::internal::object::types::ObjectType; +use mercury::utils::read_sha1; use crate::command; -use crate::utils::util; #[derive(Default)] pub struct ClientStorage { @@ -259,7 +259,7 @@ impl ClientStorage { let mut objs = Vec::new(); for _ in 0..fanout[255] { let _offset = idx_file.read_u32::()?; - let hash = util::read_sha1(&mut idx_file)?; + let hash = read_sha1(&mut idx_file)?; objs.push(hash); } @@ -283,7 +283,7 @@ impl ClientStorage { idx_file.seek(io::SeekFrom::Start((FANOUT + 24 * start) as u64))?; for _ in start..end { let offset = idx_file.read_u32::()?; - let hash = util::read_sha1(&mut idx_file)?; + let hash = read_sha1(&mut idx_file)?; if &hash == obj_id { return Ok(Some(offset as u64)); diff --git a/libra/src/utils/util.rs b/libra/src/utils/util.rs index 690a25e0a..689939f2d 100644 --- a/libra/src/utils/util.rs +++ b/libra/src/utils/util.rs @@ -326,12 +326,6 @@ pub fn get_repo_name_from_url(mut url: &str) -> Option<&str> { Some(&url[repo_start..repo_end]) } -pub fn read_sha1(file: &mut impl Read) -> io::Result { - let mut buf = [0; 20]; - file.read_exact(&mut buf)?; - Ok(SHA1::from_bytes(&buf)) -} - #[cfg(test)] mod test { use super::*; diff --git a/mercury/src/internal/index.rs b/mercury/src/internal/index.rs index 960ca4430..546d8a6d8 100644 --- a/mercury/src/internal/index.rs +++ b/mercury/src/internal/index.rs @@ -10,6 +10,7 @@ use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; use std::time::{SystemTime, UNIX_EPOCH}; +use crate::utils; use crate::errors::GitError; use crate::hash::SHA1; use crate::internal::pack::wrapper::Wrapper; @@ -432,26 +433,6 @@ impl Index { } } -mod utils { - use std::io; - use std::io::Read; - use crate::hash::SHA1; - - pub const SHA1_SIZE: usize = 20; - - pub fn read_bytes(file: &mut impl Read, len: usize) -> io::Result> { - let mut buf = vec![0; len]; - file.read_exact(&mut buf)?; - Ok(buf) - } - - pub fn read_sha1(file: &mut impl Read) -> io::Result { - let mut buf = [0; 20]; - file.read_exact(&mut buf)?; - Ok(SHA1::from_bytes(&buf)) - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/mercury/src/lib.rs b/mercury/src/lib.rs index 91c896457..644e37373 100644 --- a/mercury/src/lib.rs +++ b/mercury/src/lib.rs @@ -12,6 +12,7 @@ static GLOBAL: MiMalloc = MiMalloc; pub mod internal; pub mod hash; pub mod errors; +pub mod utils; #[cfg(test)] mod tests {} diff --git a/mercury/src/utils.rs b/mercury/src/utils.rs new file mode 100644 index 000000000..1916ce230 --- /dev/null +++ b/mercury/src/utils.rs @@ -0,0 +1,18 @@ +use std::io; +use std::io::Read; + +use crate::hash::SHA1; + +pub const SHA1_SIZE: usize = 20; + +pub fn read_bytes(file: &mut impl Read, len: usize) -> io::Result> { + let mut buf = vec![0; len]; + file.read_exact(&mut buf)?; + Ok(buf) +} + +pub fn read_sha1(file: &mut impl Read) -> io::Result { + let mut buf = [0; 20]; + file.read_exact(&mut buf)?; + Ok(SHA1::from_bytes(&buf)) +} \ No newline at end of file