pub mod depth;
pub mod kinds;
mod path;
mod sources;
mod tee;
pub use kinds::{
Float, Int, Kind, KnownLogical, Logical, Physical, TimeUnit, UnknownLogical, UnknownLogicalId,
};
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
pub use path::render;
pub use path::{Path, Step};
pub use sources::{Bytes, Raw};
#[doc(hidden)]
pub mod __private {
pub use super::sources::discard_one;
}
pub use tee::Tee;
pub trait Encrypt {
const DEPTH: Option<usize> = None;
fn visit<S: Sink>(self, sink: S) -> Result<S::Ok, S::Error>;
}
pub trait Error: Sized {
fn source(error: SourceError) -> Self;
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct SourceError(Box<dyn core::error::Error + Send + Sync>);
impl SourceError {
pub fn new(error: impl core::error::Error + Send + Sync + 'static) -> Self {
Self(Box::new(error))
}
pub fn msg(msg: impl Into<String>) -> Self {
Self(msg.into().into())
}
pub fn inner(&self) -> &(dyn core::error::Error + Send + Sync + 'static) {
&*self.0
}
}
pub trait Sink: Sized {
type Ok;
type Error: Error;
type Seq: SeqSink<Ok = Self::Ok, Error = Self::Error>;
type Map: MapSink<Ok = Self::Ok, Error = Self::Error>;
fn scalar(self, ty: Option<Logical>, value: Physical<'_>) -> Result<Self::Ok, Self::Error>;
fn seq(self, len: Option<usize>) -> Result<Self::Seq, Self::Error>;
fn map(self, len: Option<usize>) -> Result<Self::Map, Self::Error>;
fn passthrough<P: Encrypt + Clone>(self, value: P) -> Result<Self::Ok, Self::Error>;
}
pub trait SeqSink: Sized {
type Ok;
type Error: Error;
type Element<'s>: Sink<Error = Self::Error>
where
Self: 's;
fn element(&mut self) -> Result<Self::Element<'_>, Self::Error>;
fn end(self) -> Result<Self::Ok, Self::Error>;
}
pub trait MapSink: Sized {
type Ok;
type Error: Error;
type Entry<'s>: Sink<Error = Self::Error>
where
Self: 's;
fn entry(&mut self, key: &str) -> Result<Self::Entry<'_>, Self::Error>;
fn end(self) -> Result<Self::Ok, Self::Error>;
}
pub fn visit_seq<S, I>(sink: S, len: Option<usize>, items: I) -> Result<S::Ok, S::Error>
where
S: Sink,
I: IntoIterator,
I::Item: Encrypt,
{
let mut seq = sink.seq(len)?;
for item in items {
item.visit(seq.element()?)?;
}
seq.end()
}
pub fn visit_map<'k, S, I, P>(sink: S, len: Option<usize>, entries: I) -> Result<S::Ok, S::Error>
where
S: Sink,
I: IntoIterator<Item = (&'k str, P)>,
P: Encrypt,
{
let mut map = sink.map(len)?;
for (key, value) in entries {
value.visit(map.entry(key)?)?;
}
map.end()
}
#[cfg(test)]
pub(crate) mod record {
use super::*;
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
Scalar(Option<Logical>, Owned),
Seq(Option<usize>),
Element,
EndSeq,
Map(Option<usize>),
Entry(String),
EndMap,
Passthrough,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Owned {
Null,
Bool(bool),
Int(Int),
Float(Float),
Text(String),
Bytes(Vec<u8>),
}
impl From<Physical<'_>> for Owned {
fn from(v: Physical<'_>) -> Self {
match v {
Physical::Null => Owned::Null,
Physical::Bool(b) => Owned::Bool(b),
Physical::Int(i) => Owned::Int(i),
Physical::Float(f) => Owned::Float(f),
Physical::Text(s) => Owned::Text(s.to_owned()),
Physical::Bytes(b) => Owned::Bytes(b.to_vec()),
}
}
}
#[derive(Debug)]
pub struct RecordError(pub String);
impl Error for RecordError {
fn source(error: SourceError) -> Self {
Self(error.to_string())
}
}
pub struct Record<'l>(pub &'l mut Vec<Event>);
impl Sink for Record<'_> {
type Ok = ();
type Error = RecordError;
type Seq = Self;
type Map = Self;
fn scalar(self, ty: Option<Logical>, value: Physical<'_>) -> Result<(), RecordError> {
self.0.push(Event::Scalar(ty, value.into()));
Ok(())
}
fn seq(self, len: Option<usize>) -> Result<Self, RecordError> {
self.0.push(Event::Seq(len));
Ok(self)
}
fn map(self, len: Option<usize>) -> Result<Self, RecordError> {
self.0.push(Event::Map(len));
Ok(self)
}
fn passthrough<P: Encrypt + Clone>(self, value: P) -> Result<(), RecordError> {
self.0.push(Event::Passthrough);
value.visit(Record(self.0))
}
}
impl SeqSink for Record<'_> {
type Ok = ();
type Error = RecordError;
type Element<'s>
= Record<'s>
where
Self: 's;
fn element(&mut self) -> Result<Record<'_>, RecordError> {
self.0.push(Event::Element);
Ok(Record(self.0))
}
fn end(self) -> Result<(), RecordError> {
self.0.push(Event::EndSeq);
Ok(())
}
}
impl MapSink for Record<'_> {
type Ok = ();
type Error = RecordError;
type Entry<'s>
= Record<'s>
where
Self: 's;
fn entry(&mut self, key: &str) -> Result<Record<'_>, RecordError> {
self.0.push(Event::Entry(key.to_owned()));
Ok(Record(self.0))
}
fn end(self) -> Result<(), RecordError> {
self.0.push(Event::EndMap);
Ok(())
}
}
pub fn record<P: Encrypt>(p: P) -> Result<Vec<Event>, RecordError> {
let mut log = Vec::new();
p.visit(Record(&mut log))?;
Ok(log)
}
}