1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#![doc = include_str!("../README.md")]
//! ## Example
//!
//! Consider the following TL-B schema:
//!
//! ```tlb
//! tag$10 query_id:uint64 amount:(VarUInteger 16) = Hello;
//! ```
//!
//! Let's first define a struct `Hello` that holds these parameters:
//!
//! ```rust
//! # use num_bigint::BigUint;
//! struct Hello {
//! pub query_id: u64,
//! pub amount: BigUint,
//! }
//! ```
//!
//! ### **Ser**ialization
//!
//! To be able to **ser**ialize a type to [`BitWriter`](crate::ser::BitWriter),
//! we should implement [`BitPack`](crate::ser::BitPack) on it:
//!
//! ```
//! # use bitvec::{vec::BitVec, order::Msb0};
//! # use num_bigint::BigUint;
//! # use tlbits::{
//! # r#as::{NBits, VarInt},
//! # ser::{BitPack, BitWriter, BitWriterExt, pack},
//! # StringError,
//! # };
//! #
//! # struct Hello {
//! # pub query_id: u64,
//! # pub amount: BigUint,
//! # }
//! impl BitPack for Hello {
//! fn pack<W>(&self, mut writer: W) -> Result<(), W::Error>
//! where W: BitWriter,
//! {
//! writer
//! // tag$10
//! .pack_as::<_, NBits<2>>(0b10)?
//! // query_id:uint64
//! .pack(self.query_id)?
//! // amount:(VarUInteger 16)
//! .pack_as::<_, &VarInt<4>>(&self.amount)?;
//! Ok(())
//! }
//! }
//!
//! # fn main() -> Result<(), StringError> {
//! # let mut writer = BitVec::<u8, Msb0>::new().counted();
//! writer.pack(Hello {
//! query_id: 0,
//! amount: 1_000u64.into(),
//! })?;
//! # Ok(())
//! # }
//! ```
//!
//! ### **De**serialization
//!
//! To be able to **de**serialize a type from [`BitReader`](crate::de::BitReader),
//! we should implement [`BitUnpack`](crate::de::BitUnpack) on it:
//!
//! ```rust
//! # use bitvec::{vec::BitVec, order::Msb0};
//! # use num_bigint::BigUint;
//! # use tlbits::{
//! # r#as::{NBits, VarInt},
//! # de::{BitReaderExt, BitReader, BitUnpack},
//! # Error,
//! # ser::{BitPack, BitWriter, BitWriterExt, pack},
//! # StringError,
//! # };
//! # #[derive(Debug, PartialEq)]
//! # struct Hello {
//! # pub query_id: u64,
//! # pub amount: BigUint,
//! # }
//! # impl BitPack for Hello {
//! # fn pack<W>(&self, mut writer: W) -> Result<(), W::Error>
//! # where W: BitWriter,
//! # {
//! # writer
//! # // tag$10
//! # .pack_as::<_, NBits<2>>(0b10)?
//! # // query_id:uint64
//! # .pack(self.query_id)?
//! # // amount:(VarUInteger 16)
//! # .pack_as::<_, &VarInt<4>>(&self.amount)?;
//! # Ok(())
//! # }
//! # }
//! impl BitUnpack for Hello {
//! fn unpack<R>(mut reader: R) -> Result<Self, R::Error>
//! where R: BitReader,
//! {
//! // tag$10
//! let tag: u8 = reader.unpack_as::<_, NBits<2>>()?;
//! if tag != 0b10 {
//! return Err(Error::custom(format!("unknown tag: {tag:#b}")));
//! }
//! Ok(Self {
//! // query_id:uint64
//! query_id: reader.unpack()?,
//! // amount:(VarUInteger 16)
//! amount: reader.unpack_as::<_, VarInt<4>>()?,
//! })
//! }
//! }
//!
//! # fn main() -> Result<(), StringError> {
//! # let orig = Hello {
//! # query_id: 0,
//! # amount: 1_000u64.into(),
//! # };
//! # let mut writer = BitVec::<u8, Msb0>::new().counted();
//! # writer.pack(&orig)?;
//! # let mut parser = writer.as_bitslice();
//! let hello: Hello = parser.unpack()?;
//! # assert_eq!(hello, orig);
//! # Ok(())
//! # }
//! ```
pub mod adapters;
pub mod r#as;
pub mod de;
mod error;
pub mod integer;
pub mod ser;
pub use self::error::*;
pub use bitvec;
pub use either;
#[cfg(test)]
mod tests;