identity_jose/jwk/
key_set.rsuse core::iter::FromIterator;
use core::ops::Index;
use core::ops::IndexMut;
use core::slice::Iter;
use core::slice::SliceIndex;
use zeroize::Zeroize;
use crate::jwk::Jwk;
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[repr(transparent)]
pub struct JwkSet {
keys: Vec<Jwk>,
}
impl JwkSet {
pub const fn new() -> Self {
Self { keys: Vec::new() }
}
pub fn len(&self) -> usize {
self.keys.len()
}
pub fn is_empty(&self) -> bool {
self.keys.is_empty()
}
pub fn as_slice(&self) -> &[Jwk] {
&self.keys
}
pub fn iter(&self) -> Iter<'_, Jwk> {
self.keys.iter()
}
pub fn get(&self, kid: &str) -> Vec<&Jwk> {
self
.keys
.iter()
.filter(|key| matches!(key.kid(), Some(value) if value == kid))
.collect()
}
pub fn add(&mut self, key: impl Into<Jwk>) {
self.keys.push(key.into());
}
pub fn del(&mut self, index: usize) -> bool {
if index < self.keys.len() {
self.keys.remove(index);
true
} else {
false
}
}
pub fn pop(&mut self) -> Option<Jwk> {
self.keys.pop()
}
}
impl FromIterator<Jwk> for JwkSet {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = Jwk>,
{
Self {
keys: Vec::from_iter(iter),
}
}
}
impl<I> Index<I> for JwkSet
where
I: SliceIndex<[Jwk]>,
{
type Output = I::Output;
fn index(&self, index: I) -> &Self::Output {
Index::index(&*self.keys, index)
}
}
impl<I> IndexMut<I> for JwkSet
where
I: SliceIndex<[Jwk]>,
{
fn index_mut(&mut self, index: I) -> &mut Self::Output {
IndexMut::index_mut(&mut *self.keys, index)
}
}
impl Zeroize for JwkSet {
fn zeroize(&mut self) {
self.keys.zeroize();
}
}
impl Drop for JwkSet {
fn drop(&mut self) {
self.zeroize();
}
}