1use std::collections::HashMap;
4
5use crate::generated::revocation::{Revocation, Revocation_TargetKind};
6
7pub struct RevocationIndex {
8 by_kind: HashMap<Revocation_TargetKind, HashMap<String, Revocation>>,
9}
10
11impl RevocationIndex {
12 pub fn from_slice(revocations: &[Revocation]) -> Self {
13 let mut by_kind: HashMap<Revocation_TargetKind, HashMap<String, Revocation>> =
14 HashMap::new();
15 for r in revocations {
16 let bucket = by_kind.entry(r.target_kind.clone()).or_default();
17 match bucket.get(&r.target_id) {
18 Some(existing) if existing.effective_at <= r.effective_at => {}
19 _ => {
20 bucket.insert(r.target_id.clone(), r.clone());
21 }
22 }
23 }
24 RevocationIndex { by_kind }
25 }
26
27 pub fn is_revoked(&self, id: &str, kind: &Revocation_TargetKind, at: &str) -> bool {
28 let Some(bucket) = self.by_kind.get(kind) else {
29 return false;
30 };
31 let Some(rev) = bucket.get(id) else {
32 return false;
33 };
34 rev.effective_at.as_str() <= at
35 }
36}
37
38impl std::hash::Hash for Revocation_TargetKind {
39 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
40 std::mem::discriminant(self).hash(state);
41 }
42}