embeddenator_io/lib.rs
1//! # embeddenator-io
2//!
3//! I/O utilities and serialization for Embeddenator.
4//!
5//! Extracted from embeddenator core as part of Phase 2A component decomposition.
6//!
7//! ## Features
8//!
9//! - **Serialization**: Bincode and JSON support for efficient data encoding
10//! - **Buffering**: Optimized buffered I/O with configurable buffer sizes
11//! - **Streaming**: Memory-efficient streaming I/O for large files
12//! - **Envelope Format**: Compressed binary envelope format with multiple codecs
13//! - **Async Support**: Optional async I/O with tokio (enable `async` feature)
14//!
15//! ## Examples
16//!
17//! ### Serialization
18//! ```
19//! use embeddenator_io::{to_bincode, from_bincode};
20//! use serde::{Serialize, Deserialize};
21//!
22//! #[derive(Serialize, Deserialize, PartialEq, Debug)]
23//! struct Data { value: u32 }
24//!
25//! let data = Data { value: 42 };
26//! let bytes = to_bincode(&data).unwrap();
27//! let decoded: Data = from_bincode(&bytes).unwrap();
28//! assert_eq!(data, decoded);
29//! ```
30//!
31//! ### Streaming
32//! ```no_run
33//! use embeddenator_io::stream_read_file;
34//!
35//! let mut total = 0;
36//! stream_read_file("large_file.bin", |chunk| {
37//! total += chunk.len();
38//! Ok(())
39//! }).unwrap();
40//! ```
41//!
42//! ### Envelope Format
43//! ```
44//! use embeddenator_io::{PayloadKind, BinaryWriteOptions, wrap_or_legacy, unwrap_auto};
45//!
46//! let data = b"Hello, world!";
47//! let opts = BinaryWriteOptions::default();
48//! let wrapped = wrap_or_legacy(PayloadKind::EngramBincode, opts, data).unwrap();
49//! let unwrapped = unwrap_auto(PayloadKind::EngramBincode, &wrapped).unwrap();
50//! assert_eq!(data, unwrapped.as_slice());
51//! ```
52
53pub mod io;
54pub use io::*;
55
56// Re-export commonly used types
57pub use buffer::{
58 buffered_reader, buffered_writer, copy_buffered, read_chunks, write_chunks, ChunkStream,
59 DEFAULT_BUFFER_SIZE, LARGE_BUFFER_SIZE, SMALL_BUFFER_SIZE,
60};
61pub use serialize::{
62 from_bincode, from_json, read_bincode_file, read_json_file, to_bincode, to_json,
63 to_json_pretty, write_bincode_file, write_json_file,
64};
65pub use stream::{stream_read_file, stream_write_file, StreamReader, StreamWriter};
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn component_loads() {
73 // Verify core types are accessible
74 let _ = PayloadKind::EngramBincode;
75 let _ = CompressionCodec::None;
76 let opts = BinaryWriteOptions::default();
77 assert_eq!(opts.codec, CompressionCodec::None);
78 }
79
80 #[test]
81 fn test_serialization_integration() {
82 use serde::{Deserialize, Serialize};
83
84 #[derive(Serialize, Deserialize, PartialEq, Debug)]
85 struct TestData {
86 id: u32,
87 name: String,
88 }
89
90 let data = TestData {
91 id: 123,
92 name: "test".to_string(),
93 };
94
95 // Test bincode
96 let bytes = to_bincode(&data).unwrap();
97 let decoded: TestData = from_bincode(&bytes).unwrap();
98 assert_eq!(data, decoded);
99
100 // Test JSON
101 let json = to_json(&data).unwrap();
102 let decoded: TestData = from_json(&json).unwrap();
103 assert_eq!(data.id, decoded.id);
104 }
105
106 #[test]
107 fn test_envelope_integration() {
108 let data = b"Test data for envelope";
109 let opts = BinaryWriteOptions::default();
110
111 // Wrap and unwrap
112 let wrapped = wrap_or_legacy(PayloadKind::EngramBincode, opts, data).unwrap();
113 let unwrapped = unwrap_auto(PayloadKind::EngramBincode, &wrapped).unwrap();
114
115 assert_eq!(data, unwrapped.as_slice());
116 }
117}