Improve the context and IKM list APIs

This commit is contained in:
Rodolphe Bréard 2024-03-24 09:47:36 +01:00
parent 90c8a2aa87
commit ae19a16531
5 changed files with 83 additions and 16 deletions

View file

@ -1,5 +1,13 @@
use std::num::NonZeroU64;
macro_rules! data_ctx_from_iter {
($self: ident, $ctx: ident) => {
$self {
ctx: $ctx.iter().map(|s| s.to_string()).collect(),
}
};
}
pub struct DataContext {
ctx: Vec<String>,
}
@ -12,12 +20,31 @@ impl DataContext {
impl<const N: usize> From<[&str; N]> for DataContext {
fn from(ctx: [&str; N]) -> Self {
Self {
ctx: ctx.iter().map(|s| s.to_string()).collect(),
}
data_ctx_from_iter!(Self, ctx)
}
}
impl<const N: usize> From<&[&str; N]> for DataContext {
fn from(ctx: &[&str; N]) -> Self {
data_ctx_from_iter!(Self, ctx)
}
}
impl From<&[&str]> for DataContext {
fn from(ctx: &[&str]) -> Self {
data_ctx_from_iter!(Self, ctx)
}
}
macro_rules! key_ctx_from_iter {
($self: ident, $ctx: ident) => {
$self {
ctx: $ctx.iter().map(|s| s.to_string()).collect(),
periodicity: Some(crate::DEFAULT_KEY_CTX_PERIODICITY),
}
};
}
pub struct KeyContext {
pub(crate) ctx: Vec<String>,
pub(crate) periodicity: Option<u64>,
@ -48,3 +75,21 @@ impl KeyContext {
self.periodicity.is_some()
}
}
impl<const N: usize> From<[&str; N]> for KeyContext {
fn from(ctx: [&str; N]) -> Self {
key_ctx_from_iter!(Self, ctx)
}
}
impl<const N: usize> From<&[&str; N]> for KeyContext {
fn from(ctx: &[&str; N]) -> Self {
key_ctx_from_iter!(Self, ctx)
}
}
impl From<&[&str]> for KeyContext {
fn from(ctx: &[&str]) -> Self {
key_ctx_from_iter!(Self, ctx)
}
}