beve/
lib.rs

1//! BEVE - Binary Efficient Versatile Encoding
2//!
3//! High-performance, tagged binary format designed for scientific computing.
4//! This crate provides a robust, fast, and ergonomic implementation with serde support.
5//!
6//! - Little-endian encoding
7//! - Direct struct serialization via `serde::{Serialize, Deserialize}`
8//! - Typed arrays for numeric, boolean, and string sequences when possible
9//! - Object keys as strings or integer types
10//! - Enum support via BEVE type-tag extension
11//!
12//! Example
13//!
14//! ```rust
15//! use serde::{Serialize, Deserialize};
16//!
17//! #[derive(Serialize, Deserialize, Debug, PartialEq)]
18//! struct Point { x: f64, y: f64 }
19//!
20//! let p = Point { x: 1.0, y: -2.0 };
21//! let bytes = beve::to_vec(&p).unwrap();
22//! let p2: Point = beve::from_slice(&bytes).unwrap();
23//! assert_eq!(p, p2);
24//! ```
25
26mod de;
27mod error;
28mod ext;
29pub mod fast;
30mod header;
31mod json;
32mod ser;
33mod size;
34
35pub use crate::de::{from_slice, Deserializer};
36pub use crate::error::{Error, Result};
37pub use crate::ext::{Complex, ComplexSlice};
38pub use crate::ext::{Matrix, MatrixLayout};
39pub use crate::fast::{
40    to_vec_bool_slice, to_vec_complex32, to_vec_complex32_slice, to_vec_complex64,
41    to_vec_complex64_slice, to_vec_str_slice, to_vec_string_slice, to_vec_typed_slice,
42    write_bool_slice, write_str_slice, write_string_slice, write_typed_slice, BeveTypedSlice,
43};
44pub use crate::json::{
45    beve_slice_to_json, beve_slice_to_json_string, json_slice_to_beve, json_str_to_beve,
46};
47pub use crate::ser::{to_vec, to_vec_with_options, EnumEncoding, Serializer, SerializerOptions};
48
49/// BEVE-specific utilities and helper types.
50pub mod util {
51    pub use crate::ext::MatrixLayout;
52}
53
54use std::io::{Read, Write};
55
56/// Serialize a value to any writer. For unknown-length containers, this uses an internal buffer.
57pub fn to_writer<W: Write, T: serde::Serialize>(mut writer: W, value: &T) -> Result<()> {
58    let bytes = to_vec(value)?;
59    writer
60        .write_all(&bytes)
61        .map_err(|e| Error::MessageOwned(e.to_string()))
62}
63
64/// Serialize a value to any writer with custom options.
65pub fn to_writer_with_options<W: Write, T: serde::Serialize>(
66    mut writer: W,
67    value: &T,
68    opts: SerializerOptions,
69) -> Result<()> {
70    let bytes = to_vec_with_options(value, opts)?;
71    writer
72        .write_all(&bytes)
73        .map_err(|e| Error::MessageOwned(e.to_string()))
74}
75
76/// Deserialize a value by reading all bytes from a reader into a buffer first.
77pub fn from_reader<R: Read, T: serde::de::DeserializeOwned>(mut reader: R) -> Result<T> {
78    let mut buf = Vec::new();
79    reader
80        .read_to_end(&mut buf)
81        .map_err(|e| Error::MessageOwned(e.to_string()))?;
82    from_slice(&buf)
83}