1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// This is free and unencumbered software released into the public domain.
use crate::{ID_LEN, Id};
use arrayvec::ArrayString;
/// Options for enumerating the blob IDs in a repository.
///
/// See [`Repository::list`](crate::Repository::list).
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ListOptions {
/// Enumerate only IDs whose hexadecimal encoding begins with this prefix.
pub prefix: Option<ArrayString<{ 2 * ID_LEN }>>,
/// Enumerate only IDs ordered strictly after this one.
///
/// This is an exclusive cursor: passing the last ID of one page as the
/// cursor for the next yields a stable paginated view.
pub after: Option<Id>,
/// Enumerate at most this many IDs.
///
/// Together with [`ListOptions::after`], this bounds one page of a
/// paginated view.
pub limit: Option<usize>,
}
impl ListOptions {
pub fn new() -> Self {
Self::default()
}
/// Restricts enumeration to IDs whose hexadecimal encoding begins with
/// the given prefix.
///
/// # Panics
///
/// Panics if the prefix is longer than a full hexadecimal ID (64 bytes).
pub fn with_prefix(mut self, prefix: &str) -> Self {
self.prefix = Some(ArrayString::from(prefix).expect("prefix no longer than a full ID"));
self
}
/// Restricts enumeration to IDs ordered strictly after the given ID.
pub fn with_after(mut self, id: Id) -> Self {
self.after = Some(id);
self
}
/// Restricts enumeration to at most the given number of IDs.
pub fn with_limit(mut self, limit: usize) -> Self {
self.limit = Some(limit);
self
}
/// Returns `true` if the given ID satisfies these options.
///
/// Note that this checks the prefix filter and cursor only; enforcing
/// [`ListOptions::limit`] is up to the enumerator.
pub fn matches(&self, id: &Id) -> bool {
if let Some(prefix) = &self.prefix {
if !id.to_hex().starts_with(prefix.as_str()) {
return false;
}
}
if let Some(after) = &self.after {
if id <= after {
return false;
}
}
true
}
}