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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/*! # Deku: Declarative binary reading and writing Deriving a struct or enum with `DekuRead` and `DekuWrite` provides bit-level, symmetric, serialization/deserialization implementations. This allows the developer to focus on building and maintaining how the data is represented and manipulated and not on redundant, error-prone, parsing/writing code. This approach is especially useful when dealing with binary structures such as TLVs or network protocols. Under the hood, it makes use of the [bitvec](https://crates.io/crates/bitvec) crate as the "Reader" and “Writer” For documentation and examples on available `#deku[()]` attributes and features, see [attributes list](attributes) For more examples, see the [examples folder](https://github.com/sharksforarms/deku/tree/master/examples)! ## no_std For use in `no_std` environments, `alloc` is the single feature which is required on deku. # Example Let's read big-endian data into a struct, with fields containing different sizes, modify a value, and write it back ```rust use deku::prelude::*; #[derive(Debug, PartialEq, DekuRead, DekuWrite)] #[deku(endian = "big")] struct DekuTest { #[deku(bits = "4")] field_a: u8, #[deku(bits = "4")] field_b: u8, field_c: u16, } let data: Vec<u8> = vec![0b0110_1001, 0xBE, 0xEF]; let (_rest, mut val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap(); assert_eq!(DekuTest { field_a: 0b0110, field_b: 0b1001, field_c: 0xBEEF, }, val); val.field_c = 0xC0FE; let data_out = val.to_bytes().unwrap(); assert_eq!(vec![0b0110_1001, 0xC0, 0xFE], data_out); ``` # Composing Deku structs/enums can be composed as long as they implement DekuRead / DekuWrite traits ```rust use deku::prelude::*; #[derive(Debug, PartialEq, DekuRead, DekuWrite)] struct DekuTest { header: DekuHeader, data: DekuData, } #[derive(Debug, PartialEq, DekuRead, DekuWrite)] struct DekuHeader(u8); #[derive(Debug, PartialEq, DekuRead, DekuWrite)] struct DekuData(u16); let data: Vec<u8> = vec![0xAA, 0xEF, 0xBE]; let (_rest, mut val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap(); assert_eq!(DekuTest { header: DekuHeader(0xAA), data: DekuData(0xBEEF), }, val); let data_out = val.to_bytes().unwrap(); assert_eq!(data, data_out); ``` # Vec Vec<T> can be used in combination with the [count](attributes#count) attribute (T must implement DekuRead/DekuWrite) [bytes_read](attributes#bytes_read) or [bits_read](attributes#bits_read) can also be used instead of `count` to read a specific size of each. If the length of Vec changes, the original field specified in `count` will not get updated. Calling `.update()` can be used to "update" the field! ```rust use deku::prelude::*; #[derive(Debug, PartialEq, DekuRead, DekuWrite)] struct DekuTest { #[deku(update = "self.data.len()")] count: u8, #[deku(count = "count")] data: Vec<u8>, } let data: Vec<u8> = vec![0x02, 0xBE, 0xEF, 0xFF, 0xFF]; let (_rest, mut val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap(); assert_eq!(DekuTest { count: 0x02, data: vec![0xBE, 0xEF] }, val); let data_out = val.to_bytes().unwrap(); assert_eq!(vec![0x02, 0xBE, 0xEF], data_out); // Pushing an element to data val.data.push(0xAA); assert_eq!(DekuTest { count: 0x02, // Note: this value has not changed data: vec![0xBE, 0xEF, 0xAA] }, val); let data_out = val.to_bytes().unwrap(); // Note: `count` is still 0x02 while 3 bytes got written assert_eq!(vec![0x02, 0xBE, 0xEF, 0xAA], data_out); // Use `update` to update `count` val.update().unwrap(); assert_eq!(DekuTest { count: 0x03, data: vec![0xBE, 0xEF, 0xAA] }, val); ``` # Enums As enums can have multiple variants, each variant must have a way to match on the incoming data. First the "type" is read using the `type`, then is matched against the variants given `id`. What happens after is the same as structs! This is implemented with the [id](/attributes/index.html#id), [id_pat](/attributes/index.html#id_pat) and [type](attributes#type) attributes. See these for more examples. If no `id` is specified, the variant will default to it's discriminant value. If no variant can be matched, a [DekuError::Parse](crate::error::DekuError) error will be returned. Example: ```rust use deku::prelude::*; #[derive(Debug, PartialEq, DekuRead, DekuWrite)] #[deku(type = "u8")] enum DekuTest { #[deku(id = "0x01")] VariantA, #[deku(id = "0x02")] VariantB(u16), } let data: Vec<u8> = vec![0x01, 0x02, 0xEF, 0xBE]; let (rest, val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap(); assert_eq!(DekuTest::VariantA , val); let (rest, val) = DekuTest::from_bytes(rest).unwrap(); assert_eq!(DekuTest::VariantB(0xBEEF) , val); ``` # Context Child parsers can get access to the parent's parsed values using the `ctx` attribute For more information see [ctx attribute](attributes#ctx) Example: ```rust use deku::prelude::*; #[derive(DekuRead, DekuWrite)] #[deku(ctx = "a: u8")] struct Subtype { #[deku(map = "|b: u8| -> Result<_, DekuError> { Ok(b + a) }")] b: u8 } #[derive(DekuRead, DekuWrite)] struct Root { a: u8, #[deku(ctx = "*a")] // `a` is a reference sub: Subtype } let data: Vec<u8> = vec![0x01, 0x02]; let (rest, value) = Root::from_bytes((&data[..], 0)).unwrap(); assert_eq!(value.a, 0x01); assert_eq!(value.sub.b, 0x01 + 0x02) ``` # Internal variables and previously read fields Along similar lines to [Context](#Context) variables, previously read variables are exposed and can be referenced: Example: ```rust # use deku::prelude::*; #[derive(DekuRead)] struct DekuTest { num_items: u8, #[deku(count = "num_items")] items: Vec<u16>, } ``` The following variables are internals which can be used in attributes accepting tokens such as `reader`, `writer`, `map`, `count`, etc. These are provided as a convenience to the user. Always included: - `deku::input: (&[u8], usize)` - The initial input byte slice and bit offset (available when using [from_bytes](crate::DekuContainerRead::from_bytes)) - `deku::input_bits: &BitSlice<Msb0, u8>` - The initial input in bits - `deku::rest: &BitSlice<Msb0, u8>` - Remaining bits to read - `deku::output: &mut BitSlice<Msb0, u8>` - The output bit stream Conditionally included if referenced: - `deku::bit_offset: usize` - Current bit offset from the input - `deku::byte_offset: usize` - Current byte offset from the input Example: ```rust # use deku::prelude::*; #[derive(DekuRead)] #[deku(ctx = "size: u32")] pub struct EncodedString { encoding: u8, #[deku(count = "size as usize - deku::byte_offset")] data: Vec<u8> } ``` */ #![warn(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)] #[cfg(feature = "alloc")] extern crate alloc; #[cfg(feature = "alloc")] use alloc::vec::Vec; use bitvec::prelude::*; pub use deku_derive::*; pub mod attributes; pub mod ctx; pub mod error; mod impls; pub mod prelude; pub use crate::error::DekuError; /// "Reader" trait: read bits and construct type pub trait DekuRead<'a, Ctx = ()> { /// Read bits and construct type /// * **input** - Input as bits /// * **ctx** - A context required by context-sensitive reading. A unit type `()` means no context /// needed. fn read( input: &'a BitSlice<Msb0, u8>, ctx: Ctx, ) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError> where Self: Sized; } /// "Reader" trait: implemented on DekuRead struct and enum containers. A `container` is a type which /// doesn't need any context information. pub trait DekuContainerRead<'a>: DekuRead<'a, ()> { /// Read bytes and construct type /// * **input** - Input as a tuple of (bytes, bit_offset) /// /// Returns a tuple of the remaining data as (bytes, bit_offset) and a constructed value fn from_bytes(input: (&'a [u8], usize)) -> Result<((&'a [u8], usize), Self), DekuError> where Self: Sized; } /// "Writer" trait: write from type to bits pub trait DekuWrite<Ctx = ()> { /// Write type to bits /// * **output** - Sink to store resulting bits /// * **ctx** - A context required by context-sensitive reading. A unit type `()` means no context /// needed. fn write(&self, output: &mut BitVec<Msb0, u8>, ctx: Ctx) -> Result<(), DekuError>; } /// "Writer" trait: implemented on DekuWrite struct and enum containers. A `container` is a type which /// doesn't need any context information. pub trait DekuContainerWrite: DekuWrite<()> { /// Write struct/enum to Vec<u8> fn to_bytes(&self) -> Result<Vec<u8>, DekuError>; /// Write struct/enum to BitVec fn to_bits(&self) -> Result<BitVec<Msb0, u8>, DekuError>; } /// "Updater" trait: apply mutations to a type pub trait DekuUpdate { /// Apply updates fn update(&mut self) -> Result<(), DekuError>; }