tree_buf/internal/
error.rs

1use std::fmt::{Debug, Display, Formatter};
2
3#[cfg(feature = "decode")]
4#[derive(Debug, PartialEq, Eq, Clone)]
5pub enum DecodeError {
6    SchemaMismatch,
7    // Had these broken up at one point into different kinds of errors,
8    // like reading past the end of the file or having an invalid type id.
9    // In practice, a corrupt file will just trigger one of the problem at random
10    // so it's not useful information. Removing the variants makes it so that at
11    // least for now we can avoid boxing.
12    InvalidFormat,
13}
14
15use coercible_errors::coercible_errors;
16coercible_errors!(DecodeError);
17
18#[cfg(feature = "decode")]
19impl Display for DecodeError {
20    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
21        match self {
22            DecodeError::SchemaMismatch => f.write_str("The expected schema did not match that in the document."),
23            DecodeError::InvalidFormat => f.write_str("The format was not a valid Tree-Buf"),
24        }
25    }
26}
27
28#[cfg(feature = "decode")]
29impl std::error::Error for DecodeError {}
30
31#[cfg(feature = "decode")]
32impl From<std::str::Utf8Error> for DecodeError {
33    fn from(_value: std::str::Utf8Error) -> Self {
34        DecodeError::InvalidFormat
35    }
36}