use std::num::{NonZeroU16, NonZeroUsize};
use vitaminc_context::Context;
use vitaminc_ore::{order, Float, Int, Logical, OrderPlaintext, OrderTarget, Physical};
use vitaminc_protected::Protected;
use vitaminc_random::SafeRand;
pub(crate) fn key() -> Protected<[u8; 16]> {
use rand::TryRng;
let mut rng = SafeRand::from_entropy().expect("the OS random number generator");
let mut key = [0u8; 16];
match rng.try_fill_bytes(&mut key) {
Ok(()) => {}
Err(never) => match never {},
}
Protected::new(key)
}
pub(crate) fn fixed_key(byte: u8) -> Protected<[u8; 16]> {
Protected::new([byte; 16])
}
pub(crate) fn salt(bytes: &[u8]) -> Context<'static> {
Context::from_encoded(bytes.to_vec())
}
pub(crate) const fn ceiling(n: usize) -> NonZeroUsize {
NonZeroUsize::new(n).unwrap()
}
pub(crate) const fn fixed_padding(n: u16) -> crate::scheme::chained::Padding {
crate::scheme::chained::Padding::Fixed {
max_bytes: NonZeroU16::new(n).unwrap(),
}
}
pub(crate) fn plaintext(
target: OrderTarget,
ty: Option<Logical>,
value: Physical<'_>,
) -> OrderPlaintext {
order(target, ty, value).expect("the value encodes as its target")
}
pub(crate) fn u64_pt(x: u64) -> OrderPlaintext {
plaintext(OrderTarget::U64, None, Physical::Int(Int::U64(x)))
}
pub(crate) fn u32_pt(x: u32) -> OrderPlaintext {
plaintext(OrderTarget::U32, None, Physical::Int(Int::U32(x)))
}
pub(crate) fn u128_pt(x: u128) -> OrderPlaintext {
plaintext(OrderTarget::U128, None, Physical::Int(Int::U128(x)))
}
pub(crate) fn i128_pt(x: i128) -> OrderPlaintext {
plaintext(OrderTarget::I128, None, Physical::Int(Int::I128(x)))
}
pub(crate) fn f64_pt(x: f64) -> OrderPlaintext {
plaintext(OrderTarget::F64, None, Physical::Float(Float::F64(x)))
}
pub(crate) fn date_pt(days: i32) -> OrderPlaintext {
plaintext(
OrderTarget::Date,
Some(Logical::Date),
Physical::Int(Int::I32(days)),
)
}
pub(crate) fn timestamp_pt(nanos: i64) -> OrderPlaintext {
use vitaminc_ore::TimeUnit;
plaintext(
OrderTarget::Timestamp,
Some(Logical::Timestamp(TimeUnit::Nanos)),
Physical::Int(Int::I64(nanos)),
)
}
pub(crate) fn text_pt(s: &str) -> OrderPlaintext {
plaintext(OrderTarget::Text, None, Physical::Text(s))
}
pub(crate) fn bytes_pt(b: &[u8]) -> OrderPlaintext {
plaintext(OrderTarget::Bytes, None, Physical::Bytes(b))
}