use std::collections::BTreeMap;
use super::KeyEvent;
use crate::ids::{LkIdentity, Slot};
use crate::keys::FrameIkm;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum KeyRole {
Previous,
Current,
Pending,
}
#[derive(Debug, Default)]
struct Remote {
keys: BTreeMap<Slot, (FrameIkm, KeyRole)>,
}
#[derive(Debug, Default)]
pub struct KeyRing {
remotes: BTreeMap<LkIdentity, Remote>,
local: Option<(LkIdentity, Slot, FrameIkm)>,
local_previous: Option<Slot>,
reconnect_window: bool,
}
impl KeyRing {
pub fn new() -> KeyRing {
KeyRing::default()
}
pub fn apply(&mut self, ev: &KeyEvent) {
match ev {
KeyEvent::Install {
identity,
slot,
ikm,
} => {
let current = self.local.as_ref().map(|(_, s, _)| *s);
let role = if Some(*slot) == current {
KeyRole::Current
} else {
KeyRole::Pending
};
let r = self.remotes.entry(identity.clone()).or_default();
r.keys.insert(*slot, (ikm.duplicate(), role));
}
KeyEvent::Activate {
identity,
slot,
ikm,
} => {
let old = self.local.as_ref().map(|(_, s, _)| *s);
if old != Some(*slot) {
self.local_previous = old;
}
self.local = Some((identity.clone(), *slot, ikm.duplicate()));
for r in self.remotes.values_mut() {
for (s, (_, role)) in r.keys.iter_mut() {
*role = if *s == *slot {
KeyRole::Current
} else if *role == KeyRole::Current {
KeyRole::Previous
} else {
*role
};
}
}
}
KeyEvent::Retire { identity, slot, .. } => {
if self.local.as_ref().is_some_and(|(id, _, _)| id == identity) {
if self.local_previous == Some(*slot) {
self.local_previous = None;
}
return;
}
if let Some(r) = self.remotes.get_mut(identity) {
r.keys.remove(slot);
}
}
KeyEvent::Forget { identity } => {
self.remotes.remove(identity);
}
KeyEvent::Fence { .. } => {}
KeyEvent::Clear => {
self.remotes.clear();
}
}
}
pub fn local_slot(&self) -> Option<Slot> {
self.local.as_ref().map(|(_, s, _)| *s)
}
pub fn local_identity(&self) -> Option<&LkIdentity> {
self.local.as_ref().map(|(id, _, _)| id)
}
pub fn local_key(&self) -> Option<(Slot, &FrameIkm)> {
self.local.as_ref().map(|(_, s, k)| (*s, k))
}
pub fn current(&self, id: &LkIdentity) -> Option<(Slot, &FrameIkm)> {
if let Some((lid, s, k)) = &self.local
&& lid == id
{
return Some((*s, k));
}
self.remotes.get(id).and_then(|r| {
r.keys
.iter()
.find(|(_, (_, role))| *role == KeyRole::Current)
.map(|(s, (k, _))| (*s, k))
})
}
pub fn get(&self, id: &LkIdentity, slot: Slot) -> Option<&FrameIkm> {
if let Some((lid, s, k)) = &self.local
&& lid == id
&& *s == slot
{
return Some(k);
}
self.remotes
.get(id)
.and_then(|r| r.keys.get(&slot))
.map(|(k, _)| k)
}
pub fn role(&self, id: &LkIdentity, slot: Slot) -> Option<KeyRole> {
self.remotes
.get(id)
.and_then(|r| r.keys.get(&slot))
.map(|(_, role)| *role)
}
pub fn live(&self) -> impl Iterator<Item = (&LkIdentity, Slot, &FrameIkm)> {
self.remotes
.iter()
.flat_map(|(id, r)| r.keys.iter().map(move |(s, (k, _))| (id, *s, k)))
.chain(self.local.iter().map(|(id, s, k)| (id, *s, k)))
}
pub fn repost_keys(&self) -> Vec<(&LkIdentity, Slot, &FrameIkm)> {
let mut out: Vec<(&LkIdentity, Slot, &FrameIkm)> = self
.remotes
.iter()
.flat_map(|(id, r)| {
r.keys
.iter()
.filter(|(_, (_, role))| *role != KeyRole::Previous || self.reconnect_window)
.map(move |(s, (k, _))| (id, *s, k))
})
.collect();
if let Some((id, s, k)) = &self.local {
out.push((id, *s, k));
}
out
}
pub fn set_reconnect_window(&mut self, open: bool) {
self.reconnect_window = open;
}
pub fn identities(&self) -> impl Iterator<Item = &LkIdentity> {
self.remotes.keys()
}
pub fn holds(&self, id: &LkIdentity) -> bool {
self.remotes.get(id).is_some_and(|r| !r.keys.is_empty())
|| self.local.as_ref().is_some_and(|(lid, _, _)| lid == id)
}
pub fn remote_key_count(&self) -> usize {
self.remotes.values().map(|r| r.keys.len()).sum()
}
}