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

Commit

Permalink
define separate methods to get length of id2path and path2id
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <tareknaser360@gmail.com>
  • Loading branch information
tareknaser committed Feb 27, 2024
1 parent 633335a commit 29348fe
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pub struct IndexEntry {
pub struct ResourceIndex {
/// A mapping of resource IDs to their corresponding file paths
#[serde_as(as = "Vec<(_, _)>")]
pub id2path: HashMap<ResourceId, PathBuf>,
id2path: HashMap<ResourceId, PathBuf>,
/// A mapping of file paths to their corresponding index entries
pub path2id: HashMap<PathBuf, IndexEntry>,
path2id: HashMap<PathBuf, IndexEntry>,
/// A mapping of resource IDs to the number of collisions they have
pub collisions: HashMap<ResourceId, usize>,
/// The root path of the index
Expand All @@ -76,10 +76,15 @@ impl ResourceIndex {
/// Returns the number of entries in the index
///
/// Note that the amount of resource can be lower in presence of collisions
pub fn size(&self) -> usize {
pub fn count_files(&self) -> usize {
self.path2id.len()
}

/// Returns the number of resources in the index
pub fn count_resources(&self) -> usize {
self.id2path.len()
}

/// Builds a new resource index from scratch using the root path
///
/// This function recursively scans the directory structure starting from
Expand Down Expand Up @@ -781,7 +786,7 @@ mod tests {
crc32: CRC32_1,
}));
assert_eq!(actual.collisions.len(), 0);
assert_eq!(actual.size(), 1);
assert_eq!(actual.count_files(), 1);
}

#[test]
Expand All @@ -804,7 +809,7 @@ mod tests {
crc32: CRC32_1,
}));
assert_eq!(actual.collisions.len(), 1);
assert_eq!(actual.size(), 2);
assert_eq!(actual.count_files(), 2);
}

#[test]
Expand All @@ -818,7 +823,7 @@ mod tests {
let mut actual = ResourceIndex::build(path.to_owned());

assert_eq!(actual.collisions.len(), 0);
assert_eq!(actual.size(), 2);
assert_eq!(actual.count_files(), 2);

// rename test2.txt to test3.txt
let mut name_from = path.to_owned();
Expand All @@ -833,7 +838,7 @@ mod tests {
.expect("Should update index correctly");

assert_eq!(actual.collisions.len(), 0);
assert_eq!(actual.size(), 2);
assert_eq!(actual.count_files(), 2);
assert_eq!(update.deleted.len(), 1);
assert_eq!(update.added.len(), 1);
}
Expand Down Expand Up @@ -866,7 +871,7 @@ mod tests {
crc32: CRC32_2,
}));
assert_eq!(actual.collisions.len(), 0);
assert_eq!(actual.size(), 2);
assert_eq!(actual.count_files(), 2);
assert_eq!(update.deleted.len(), 0);
assert_eq!(update.added.len(), 1);

Expand Down Expand Up @@ -935,7 +940,7 @@ mod tests {
crc32: CRC32_2,
}));
assert_eq!(index.collisions.len(), 0);
assert_eq!(index.size(), 2);
assert_eq!(index.count_files(), 2);
assert_eq!(update.deleted.len(), 0);
assert_eq!(update.added.len(), 1);

Expand Down Expand Up @@ -1004,7 +1009,7 @@ mod tests {
assert_eq!(actual.path2id.len(), 0);
assert_eq!(actual.id2path.len(), 0);
assert_eq!(actual.collisions.len(), 0);
assert_eq!(actual.size(), 0);
assert_eq!(actual.count_files(), 0);
assert_eq!(update.deleted.len(), 1);
assert_eq!(update.added.len(), 0);

Expand All @@ -1026,7 +1031,7 @@ mod tests {
let mut actual = ResourceIndex::build(path.clone());

assert_eq!(actual.collisions.len(), 0);
assert_eq!(actual.size(), 2);
assert_eq!(actual.count_files(), 2);
#[cfg(target_family = "unix")]
file.set_permissions(Permissions::from_mode(0o222))
.expect("Should be fine");
Expand All @@ -1036,7 +1041,7 @@ mod tests {
.expect("Should update index correctly");

assert_eq!(actual.collisions.len(), 0);
assert_eq!(actual.size(), 2);
assert_eq!(actual.count_files(), 2);
assert_eq!(update.deleted.len(), 0);
assert_eq!(update.added.len(), 0);
}
Expand Down Expand Up @@ -1208,7 +1213,7 @@ mod tests {
crc32: CRC32_1,
}));
assert_eq!(actual.collisions.len(), 0);
assert_eq!(actual.size(), 1);
assert_eq!(actual.count_files(), 1);
}

#[test]
Expand Down

0 comments on commit 29348fe

Please sign in to comment.