Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
rename ResourceIdCrc32 to ResourceId
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <tareknaser360@gmail.com>
  • Loading branch information
tareknaser committed Mar 20, 2024
1 parent c205208 commit 6aa9d17
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/resource/crc32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ const BUFFER_CAPACITY: usize = 512 * KILOBYTE as usize;
Deserialize,
Serialize,
)]
pub struct ResourceIdCrc32 {
pub struct ResourceId {
pub data_size: u64,
pub hash: u32,
}

impl Display for ResourceIdCrc32 {
impl Display for ResourceId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", self.data_size, self.hash)
}
}

impl FromStr for ResourceIdCrc32 {
impl FromStr for ResourceId {
type Err = ArklibError;

fn from_str(s: &str) -> Result<Self> {
let (l, r) = s.split_once('-').ok_or(ArklibError::Parse)?;
let data_size: u64 = l.parse().map_err(|_| ArklibError::Parse)?;
let hash: u32 = r.parse().map_err(|_| ArklibError::Parse)?;

Ok(ResourceIdCrc32 { data_size, hash })
Ok(ResourceId { data_size, hash })
}
}

impl ResourceIdTrait<'_> for ResourceIdCrc32 {
impl ResourceIdTrait<'_> for ResourceId {
type HashType = u32;

fn get_hash(&self) -> Self::HashType {
Expand All @@ -73,15 +73,15 @@ impl ResourceIdTrait<'_> for ResourceIdCrc32 {
.open(file_path.as_ref())?;

let mut reader = BufReader::with_capacity(BUFFER_CAPACITY, source);
ResourceIdCrc32::compute_reader(data_size, &mut reader)
ResourceId::compute_reader(data_size, &mut reader)
}

fn compute_bytes(bytes: &[u8]) -> Result<Self> {
let data_size = bytes.len().try_into().map_err(|_| {
ArklibError::Other(anyhow!("Can't convert usize to u64"))
})?; //.unwrap();
let mut reader = BufReader::with_capacity(BUFFER_CAPACITY, bytes);
ResourceIdCrc32::compute_reader(data_size, &mut reader)
ResourceId::compute_reader(data_size, &mut reader)
}

fn compute_reader<R: Read>(
Expand Down Expand Up @@ -115,7 +115,7 @@ impl ResourceIdTrait<'_> for ResourceIdCrc32 {
log::trace!("[compute] checksum: {:#02x}", hash);
assert_eq!(std::convert::Into::<u64>::into(bytes_read), data_size);

Ok(ResourceIdCrc32 { data_size, hash })
Ok(ResourceId { data_size, hash })
}
}

Expand All @@ -139,23 +139,23 @@ mod tests {
})
.len();

let id1 = ResourceIdCrc32::compute(data_size, file_path).unwrap();
let id1 = ResourceId::compute(data_size, file_path).unwrap();
assert_eq!(id1.get_hash(), 0x342a3d4a);
assert_eq!(id1.data_size, 128760);

let raw_bytes = fs::read(file_path).unwrap();
let id2 = ResourceIdCrc32::compute_bytes(raw_bytes.as_slice()).unwrap();
let id2 = ResourceId::compute_bytes(raw_bytes.as_slice()).unwrap();
assert_eq!(id2.get_hash(), 0x342a3d4a);
assert_eq!(id2.data_size, 128760);
}

#[test]
fn resource_id_order() {
let id1 = ResourceIdCrc32 {
let id1 = ResourceId {
data_size: 1,
hash: 2,
};
let id2 = ResourceIdCrc32 {
let id2 = ResourceId {
data_size: 2,
hash: 1,
};
Expand Down
2 changes: 1 addition & 1 deletion src/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::Result;

mod crc32;

pub use crc32::ResourceIdCrc32 as ResourceId;
pub use crc32::ResourceId;

/// This trait defines a generic type representing a resource identifier.
///
Expand Down

0 comments on commit 6aa9d17

Please sign in to comment.