preserves/implementations/rust/preserves/src/value/packed/mod.rs

55 lines
1.7 KiB
Rust

//! Implements the Preserves [machine-oriented binary
//! syntax](https://preserves.dev/preserves-binary.html).
//!
//! The main entry points for reading are functions [iovalue_from_bytes],
//! [annotated_iovalue_from_bytes], [from_bytes], and [annotated_from_bytes].
//!
//! The main entry points for writing are [PackedWriter::encode_iovalue] and
//! [PackedWriter::encode].
//!
//! # Summary of Binary Syntax
#![doc = include_str!("../../../doc/cheatsheet-binary-plaintext.md")]
pub mod constants;
pub mod reader;
pub mod writer;
pub use reader::PackedReader;
pub use writer::PackedWriter;
use std::io;
use super::{BinarySource, DomainDecode, IOValue, IOValueDomainCodec, NestedValue, Reader};
/// Reads a value from the given byte vector `bs` using the binary encoding, discarding
/// annotations.
pub fn from_bytes<N: NestedValue, Dec: DomainDecode<N::Embedded>>(
bs: &[u8],
decode_embedded: Dec,
) -> io::Result<N> {
super::BytesBinarySource::new(bs)
.packed(decode_embedded)
.demand_next(false)
}
/// Reads an [IOValue] from the given byte vector `bs` using the binary encoding, discarding
/// annotations.
pub fn iovalue_from_bytes(bs: &[u8]) -> io::Result<IOValue> {
from_bytes(bs, IOValueDomainCodec)
}
/// As [from_bytes], but includes annotations.
pub fn annotated_from_bytes<N: NestedValue, Dec: DomainDecode<N::Embedded>>(
bs: &[u8],
decode_embedded: Dec,
) -> io::Result<N> {
super::BytesBinarySource::new(bs)
.packed(decode_embedded)
.demand_next(true)
}
/// As [iovalue_from_bytes], but includes annotations.
pub fn annotated_iovalue_from_bytes(bs: &[u8]) -> io::Result<IOValue> {
annotated_from_bytes(bs, IOValueDomainCodec)
}