Add commit count to the cache struct

This commit is contained in:
Valentin Brandl 2019-07-07 13:28:43 +02:00
parent 1f01c3b964
commit bdb10fd54a
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D

View File

@ -9,7 +9,7 @@ use std::{
/// Enum to indicate the state of the cache
pub(crate) enum CacheState<'a> {
/// Current head and cached head are the same
Current(u64),
Current { count: u64, commits: u64 },
/// Cached head is older than current head
Old(Cache<'a>),
/// No cache was found
@ -21,7 +21,10 @@ impl<'a> CacheState<'a> {
if path.as_ref().exists() {
let cache: Cache = serde_json::from_reader(BufReader::new(File::open(path)?))?;
if cache.head == head {
Ok(CacheState::Current(cache.count))
Ok(CacheState::Current {
count: cache.count,
commits: cache.commits,
})
} else {
Ok(CacheState::Old(cache))
}
@ -30,22 +33,31 @@ impl<'a> CacheState<'a> {
}
}
pub(crate) fn calculate_new_cache(self, count: u64, head: Cow<'a, str>) -> Cache {
pub(crate) fn calculate_new_cache(self, count: u64, commits: u64, head: Cow<'a, str>) -> Cache {
match self {
CacheState::Old(mut cache) => {
cache.head = head;
cache.count += count;
cache.commits += commits;
cache
}
CacheState::No | CacheState::Current(_) => Cache { head, count },
CacheState::No | CacheState::Current { .. } => Cache {
head,
count,
commits,
},
}
}
}
#[derive(Serialize, Deserialize)]
pub(crate) struct Cache<'a> {
/// HEAD commit ref
pub head: Cow<'a, str>,
/// HoC value
pub count: u64,
/// Number of commits
pub commits: u64,
}
impl<'a> Cache<'a> {