Skip to main content

substrate_data/wire/
item_error.rs

1//! [`ItemError`]: an item of a [`Stream`](crate::Stream) that cannot be read.
2
3use std::fmt;
4
5/// An item of a [`Stream`](crate::Stream) that cannot be read, found while
6/// reading the stream's items (decoding a stream checks only its framing).
7///
8/// `index` is the item's place in the stream and `offset` the byte it starts
9/// at, counted from the first item (the stream's count is not in it). When
10/// `index` equals the stream's `len()` there is no item to read there: the
11/// stream has bytes left after its last item, and `offset` is where they begin.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct ItemError {
14    /// The item's place in the stream, from 0.
15    pub index: usize,
16    /// The byte the item starts at, from the first item.
17    pub offset: usize,
18}
19
20impl fmt::Display for ItemError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        write!(
23            f,
24            "stream item {} at byte {} cannot be read",
25            self.index, self.offset
26        )
27    }
28}
29
30impl std::error::Error for ItemError {}