Skip to main content

substrate_data/wire/binary/decode/
error.rs

1//! [`DecodeError`]: malformed binary data, and where.
2
3use std::fmt;
4
5/// Malformed binary data.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct DecodeError {
8    /// What's wrong.
9    pub message: String,
10    /// The byte it was found at.
11    pub offset: usize,
12}
13
14impl DecodeError {
15    /// A map's key that was already set, found at `offset`.
16    pub(super) fn duplicate_key(key: &str, offset: usize) -> Self {
17        DecodeError {
18            message: format!("`{key}` is already set in this map"),
19            offset,
20        }
21    }
22}
23
24impl fmt::Display for DecodeError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "byte {}: {}", self.offset, self.message)
27    }
28}
29
30impl std::error::Error for DecodeError {}