use auths_verifier::core::Attestation;
use auths_verifier::types::CanonicalDid;
use std::collections::BTreeMap;
use super::enriched::EnrichedAttestation;
#[derive(Debug)]
pub struct AttestationGroup {
pub by_device: BTreeMap<String, Vec<Attestation>>,
}
impl AttestationGroup {
pub fn from_list(attestations: Vec<Attestation>) -> Self {
let mut map: BTreeMap<String, Vec<Attestation>> = BTreeMap::new();
for att in attestations {
let key = att.subject.as_str().to_owned();
map.entry(key).or_default().push(att);
}
Self { by_device: map }
}
pub fn device_count(&self) -> usize {
self.by_device.len()
}
pub fn all(&self) -> Vec<&Attestation> {
self.by_device.values().flat_map(|v| v.iter()).collect()
}
pub fn get(&self, device_did_str: &str) -> Option<&Vec<Attestation>> {
self.by_device.get(device_did_str)
}
pub fn latest(&self, device_did: &CanonicalDid) -> Option<&Attestation> {
self.by_device
.get(device_did.as_str())
.and_then(|list| list.last())
}
}
#[derive(Debug)]
pub struct EnrichedAttestationGroup {
pub by_device: BTreeMap<String, Vec<EnrichedAttestation>>,
}
impl EnrichedAttestationGroup {
pub fn from_enriched(attestations: Vec<EnrichedAttestation>) -> Self {
let mut map: BTreeMap<String, Vec<EnrichedAttestation>> = BTreeMap::new();
for att in attestations {
let key = att.attestation.subject.as_str().to_owned();
map.entry(key).or_default().push(att);
}
Self { by_device: map }
}
pub fn device_count(&self) -> usize {
self.by_device.len()
}
pub fn latest(&self, device_did: &CanonicalDid) -> Option<&EnrichedAttestation> {
self.by_device
.get(device_did.as_str())
.and_then(|list| list.last())
}
}