use anyhow::Result;
use bincode_next::{Decode, Encode, config::standard, decode_from_slice, encode_to_vec};
use bon::Builder;
use getset::CopyGetters;
pub(crate) mod agent;
pub const MAX_MESSAGE_SIZE: usize = 1024 * 1024;
pub fn encode<E: Encode>(message: E) -> Result<Vec<u8>> {
Ok(encode_to_vec(
message,
standard().with_limit::<MAX_MESSAGE_SIZE>(),
)?)
}
pub fn decode<D: Decode<()>>(bytes: &[u8]) -> Result<D> {
let (message, _len) = decode_from_slice(bytes, standard().with_limit::<MAX_MESSAGE_SIZE>())?;
Ok(message)
}
#[derive(Builder, Clone, Copy, CopyGetters, Debug, Decode, Encode)]
#[getset(get_copy = "pub")]
pub struct Init {
#[builder(default = 5)]
num_shares: u8,
#[builder(default = 3)]
threshold: u8,
}
#[derive(Builder, Clone, Debug, Decode, Encode)]
pub struct Share {
#[builder(into)]
share: String,
}
impl Share {
#[must_use]
pub fn share(&self) -> &str {
&self.share
}
}
#[derive(Builder, Clone, Debug, Decode, Encode)]
pub struct Shares {
#[builder(into)]
shares: Vec<String>,
}
impl Shares {
#[must_use]
pub fn shares(&self) -> &[String] {
&self.shares
}
}
#[derive(Builder, Clone, Debug, Decode, Encode)]
pub struct Store {
#[builder(into)]
key: String,
#[builder(into)]
value: String,
#[builder(default)]
force: bool,
}
impl Store {
#[must_use]
pub fn key(&self) -> &str {
&self.key
}
#[must_use]
pub fn value(&self) -> &str {
&self.value
}
#[must_use]
pub fn force(&self) -> bool {
self.force
}
#[must_use]
pub fn into_parts(self) -> (String, String, bool) {
(self.key, self.value, self.force)
}
}
#[derive(Clone, Copy, Debug, Decode, Default, Encode, Eq, PartialEq)]
pub enum UnlockTimeout {
#[default]
Default,
Seconds(u64),
Forever,
}
pub const MAX_UNLOCK_SECONDS: u64 = 24 * 60 * 60;
#[derive(Clone, Debug, Decode, Encode)]
pub enum Action {
Unlock(UnlockTimeout),
Lock,
Share(Share),
GenShares(u8, u8),
Store(Store),
Read(String),
Delete(String),
GetThreshold,
FindKey(String),
}
#[derive(Clone, Debug, Decode, Encode)]
pub enum Response {
Error(String),
Success,
UnlockFailed,
Shares(Shares),
AlreadyInitialiazed,
Threshold(u8),
Value(Option<Vec<u8>>),
KeyNotFound,
KeyExists,
Matches(Vec<String>),
}