Skip to content

Commit 1762c74

Browse files
committed
feat: various improvements to the API
* make `CommitterTimestamp` available as type, making the code using it more descriptive. * add `new()` to `PriorityQueue`
1 parent 5cd7748 commit 1762c74

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

gix-revision/src/graph.rs

+38-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,36 @@ use gix_hash::oid;
33
use smallvec::SmallVec;
44
use std::ops::Index;
55

6+
/// The time in seconds since unix epoch at which a commit was created.
7+
pub type CommitterTimestamp = u64;
8+
9+
impl<'find, T: Default> Graph<'find, T> {
10+
/// Lookup `id` without failing if the commit doesn't exist, and assure that `id` is inserted into our set.
11+
/// If it wasn't, associate it with the default value. Assure `update_data(data)` gets run.
12+
/// Return the commit when done.
13+
/// Note that none of the data updates happen if there was no commit named `id`.
14+
pub fn try_lookup_and_insert(
15+
&mut self,
16+
id: gix_hash::ObjectId,
17+
update_data: impl FnOnce(&mut T),
18+
) -> Result<Option<Commit<'_>>, lookup::Error> {
19+
let res = try_lookup(&id, &mut self.find, self.cache.as_ref(), &mut self.buf)?;
20+
Ok(res.map(|commit| {
21+
match self.set.entry(id) {
22+
gix_hashtable::hash_map::Entry::Vacant(entry) => {
23+
let mut data = T::default();
24+
update_data(&mut data);
25+
entry.insert(data);
26+
}
27+
gix_hashtable::hash_map::Entry::Occupied(mut entry) => {
28+
update_data(entry.get_mut());
29+
}
30+
};
31+
commit
32+
}))
33+
}
34+
}
35+
636
impl<'find, T> Graph<'find, T> {
737
/// Create a new instance with `find` to retrieve commits and optionally `cache` to accelerate commit access.
838
pub fn new<Find, E>(mut find: Find, cache: impl Into<Option<gix_commitgraph::Graph>>) -> Self
@@ -66,7 +96,7 @@ impl<'find, T> Graph<'find, T> {
6696
pub fn insert_parents(
6797
&mut self,
6898
id: &gix_hash::oid,
69-
mut new_parent_data: impl FnMut(gix_hash::ObjectId, u64) -> T,
99+
mut new_parent_data: impl FnMut(gix_hash::ObjectId, CommitterTimestamp) -> T,
70100
mut update_existing: impl FnMut(gix_hash::ObjectId, &mut T),
71101
first_parent: bool,
72102
) -> Result<(), insert_parents::Error> {
@@ -135,10 +165,10 @@ impl<'a, 'find, T> Index<&'a gix_hash::oid> for Graph<'find, T> {
135165
pub mod lookup {
136166
/// The error returned by [`try_lookup()`][crate::Graph::try_lookup()].
137167
#[derive(Debug, thiserror::Error)]
138-
#[allow(missing_docs)]
139-
pub enum Error {
140-
#[error("There was an error looking up a commit")]
141-
Find(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
168+
#[error("There was an error looking up a commit")]
169+
pub struct Error {
170+
#[from]
171+
source: Box<dyn std::error::Error + Send + Sync + 'static>,
142172
}
143173

144174
///
@@ -186,7 +216,7 @@ pub struct Commit<'graph> {
186216
///
187217
pub mod commit {
188218
use super::Commit;
189-
use crate::graph::Either;
219+
use crate::graph::{CommitterTimestamp, Either};
190220

191221
impl<'graph> Commit<'graph> {
192222
/// Return an iterator over the parents of this commit.
@@ -202,13 +232,13 @@ pub mod commit {
202232
///
203233
/// This is the single-most important date for determining recency of commits.
204234
/// Note that this can only fail if the commit is backed by the object database *and* parsing fails.
205-
pub fn committer_timestamp(&self) -> Result<u64, gix_object::decode::Error> {
235+
pub fn committer_timestamp(&self) -> Result<CommitterTimestamp, gix_object::decode::Error> {
206236
Ok(match &self.backing {
207237
Either::Left(buf) => {
208238
gix_object::CommitRefIter::from_bytes(buf)
209239
.committer()?
210240
.time
211-
.seconds_since_unix_epoch as u64
241+
.seconds_since_unix_epoch as CommitterTimestamp
212242
}
213243
Either::Right((cache, pos)) => cache.commit_at(*pos).committer_timestamp(),
214244
})

gix-revision/src/queue.rs

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ impl<K: Ord, T> Ord for Item<K, T> {
2828
}
2929

3030
impl<K: Ord, T> PriorityQueue<K, T> {
31+
/// Create a new instance.
32+
pub fn new() -> Self {
33+
PriorityQueue(Default::default())
34+
}
3135
/// Insert `value` so that it is ordered according to `key`.
3236
pub fn insert(&mut self, key: K, value: T) {
3337
self.0.push(Item { key, value });

0 commit comments

Comments
 (0)