byte_lamination 0.1.2

Type-readable byte transformation wrappers, with CBOR and BARE serialisation and Zstd compression.
Documentation
//! A framework for 'layered' wrappers for byte transformations.
//!
//! This crate provides a framework for writing nested wrappers that operate on bytes, performing
//! operations like en/decryption, (de)compression and (de)serialisation.
//!
//! The end result might allows you to write something like e.g. `SymLockBox<Zstd<>>`.
//!
//! The crate comes built in with a few wrappers, enableable using Cargo features:
//! - `zstd`: [Zstd compression](compression::zstd)
//! - `cbor`: [CBOR serialisation](serialisation::cbor), compatible with serde.
//! - `bare`: [BARE serialisation](serialisation::bare), compatible with serde.
//!
//! No encryption wrappers have been included yet because it would be irresponsible to include
//! unaudited cryptographic tools.

use std::borrow::Cow;
use std::error::Error;

pub mod compression;
pub mod raw;
pub mod serialisation;

/// Helper trait for when it's possible to construct a lamination without any extra inputs at any
/// steps.
pub trait AutoLaminate<T>: Sized {
    fn laminate(item: T) -> Result<Self, Box<dyn Error>>;
}

/// Helper trait for when it's possible to deconstruct a lamination without any extra inputs at any
/// steps.
pub trait AutoDelaminate<T> {
    fn delaminate(self) -> Result<T, Box<dyn Error>>;
}

/// Helper trait for when it's possible to deconstruct a lamination without any extra inputs at any
/// steps.
/// This variant only borrows the input.
///
/// **EXPERIMENTAL** — may be removed in the future if this does not turn out to be ergonomic.
pub trait AutoDelaminateBorrowed<'a, T> {
    fn delaminate(&'a self) -> Result<T, Box<dyn Error>>;
}

pub trait ByteLamination<'a>: Sized {
    /// Get the raw bytes from this lamination.
    /// If possible, this should return borrowed bytes.
    fn as_cow_bytes(&self) -> Cow<'_, [u8]>;

    fn into_bytes(self) -> Vec<u8>;

    fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>>;
}