use active_infinity::stream::{decode_stream, StreamDecoder, StreamEncoder};
use active_infinity::{encode, InfinityModule, Inode};
fn main() {
let mut module = InfinityModule::new();
let mut fields = Vec::new();
for index in 0..32 {
let value = module.push(Inode::Int(index));
fields.push((format!("item_{index:02}"), value));
}
let root = module.push(Inode::Table { fields });
module.set_root(Some(root));
let image = encode(&module).expect("encode");
let encoder = StreamEncoder::new(&module, 64).expect("encoder");
let frames = encoder.frames();
let bytes = encoder.to_bytes();
println!("canonical={} bytes frames={}", image.len(), frames.len());
let mut decoder = StreamDecoder::new();
for chunk in bytes.chunks(7) {
decoder.push_bytes(chunk).expect("push");
}
let streamed = decoder.finish().expect("finish");
assert_eq!(streamed.image, image);
let decoded = decode_stream(&bytes).expect("decode_stream");
assert_eq!(decoded.image, image);
println!(
"reassembled={} bytes, hash-stable={}",
streamed.image.len(),
streamed.image == image
);
}