serde_sibor/lib.rs
1#![forbid(unsafe_code)]
2//! # `serde` implementation for the SIBOR binary format.
3//!
4//! SIBOR is a binary format that is designed to be simple to implement, fast to encode and decode,
5//! and relatively compact. In order to achieve these goals, the number of features is kept to a
6//! minimum, and some types are not supported:
7//!
8//! - SIBOR is not self-describing. The schema must be known in advance.
9//! - SIBOR does not have a concept of "optional" fields. All fields must have a value.
10//! - SIBOR does not support maps. All maps must be encoded as sequences of key-value pairs.
11//! - SIBOR treats all signed integers, unsigned integers, and floats as 64-bit values.
12//! - SIBOR encodes all unsigned integers using a variable-length encoding.
13//! - SIBOR encodes all signed integers using a variable-length zigzag encoding.
14//! - SIBOR encodes all floats using a 64-bit IEEE 754 encoding. The bits are treated as a u64 and encoded using the variable-length encoding.
15//!
16//! SIBOR is meant to be used when you want a quick and dirty way to serialize and deserialize binary data of a known schema.
17//! It does not have any built-in support for schema evolution, so such support must be implemented by the user.
18
19/// Deserialization types and functions.
20pub mod de;
21/// Error types and functions.
22pub mod error;
23/// Serialization types and functions.
24pub mod ser;
25
26/// Tests for the crate.
27#[cfg(test)]
28mod tests;
29
30pub use crate::de::Deserializer;
31pub use crate::error::Error;
32pub use crate::ser::Serializer;
33
34/// Get the number of bytes required to encode a value.
35pub fn encoded_size<V>(v: V) -> crate::error::Result<usize>
36where
37 V: ::serde::Serialize,
38{
39 struct SizeWriter {
40 written: usize,
41 }
42
43 impl ::std::io::Write for SizeWriter {
44 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
45 self.written += buf.len();
46 Ok(buf.len())
47 }
48
49 fn flush(&mut self) -> std::io::Result<()> {
50 Ok(())
51 }
52 }
53
54 let mut size_writer = SizeWriter { written: 0 };
55 let mut ser = crate::ser::Serializer::new(&mut size_writer);
56 v.serialize(&mut ser)?;
57 Ok(size_writer.written)
58}
59
60/// Encode a value into a writer.
61pub fn to_writer<V, W>(v: V, w: W) -> crate::error::Result<()>
62where
63 V: ::serde::Serialize,
64 W: ::std::io::Write,
65{
66 let mut ser = crate::ser::Serializer::new(w);
67 v.serialize(&mut ser)
68}
69
70/// Encode a value into a byte vector.
71pub fn to_bytes<V>(v: V) -> crate::error::Result<Vec<u8>>
72where
73 V: ::serde::Serialize,
74{
75 let mut buf = Vec::<u8>::new();
76 to_writer(v, &mut buf)?;
77 Ok(buf)
78}
79
80/// Decode a value from a reader.
81pub fn from_reader<'de, V, R>(r: R) -> crate::error::Result<V>
82where
83 V: ::serde::de::DeserializeOwned,
84 R: ::std::io::Read,
85{
86 V::deserialize(&mut crate::de::Deserializer::new(r))
87}
88
89/// Decode a value from a byte slice.
90pub fn from_bytes<'de, V>(buf: &'de [u8]) -> crate::error::Result<V>
91where
92 V: ::serde::Deserialize<'de>,
93{
94 V::deserialize(&mut crate::de::Deserializer::new(buf))
95}