Skip to main content

strata/
error.rs

1use std::io;
2
3#[derive(Debug)]
4pub enum StrataError {
5    Parse(ParseError),
6    Encode(EncodeError),
7    Decode(DecodeError),
8    Io(io::Error),
9    Internal(&'static str),
10}
11
12// Decode errors
13#[derive(Debug, PartialEq, Eq)]
14pub struct DecodeError {
15    pub kind: DecodeErrorKind,
16    pub offset: usize,
17}
18
19#[derive(Debug, PartialEq, Eq)]
20pub enum DecodeErrorKind {
21    InvalidTag(u8),
22    UnexpectedEOF,
23    InvalidVarint,
24    InvalidUtf8,
25    TrailingBytes,
26}
27
28// Parse errors
29#[derive(Debug, PartialEq, Eq, Clone, Copy)]
30pub struct Span {
31    pub offset: usize,
32    pub line: usize,
33    pub column: usize,
34}
35
36#[derive(Debug, PartialEq, Eq)]
37pub struct ParseError {
38    pub kind: ParseErrorKind,
39    pub span: Span,
40}
41
42#[derive(Debug, PartialEq, Eq)]
43pub enum ParseErrorKind {
44    // syntax
45    UnexpectedToken {
46        expected: &'static str,
47        found: &'static str,
48    },
49    MalformedBytesLiteral,
50
51    // semantic
52    IntegerOutOfRange,
53}
54
55// Encode errors
56#[derive(Debug, PartialEq, Eq)]
57pub enum EncodeError {
58    DuplicateKey,
59    // InvalidUtf8 is unreachable in Rust because String
60    // is UTF-8 by construction.
61    // It exists for spec completeness and non-Rust
62    // implementations.
63    InvalidUtf8,
64    InvalidInteger,
65}
66
67impl From<ParseError> for StrataError {
68    fn from(err: ParseError) -> Self {
69        StrataError::Parse(err)
70    }
71}
72
73impl From<EncodeError> for StrataError {
74    fn from(err: EncodeError) -> Self {
75        StrataError::Encode(err)
76    }
77}
78
79impl From<DecodeError> for StrataError {
80    fn from(err: DecodeError) -> Self {
81        StrataError::Decode(err)
82    }
83}
84
85impl From<io::Error> for StrataError {
86    fn from(err: io::Error) -> Self {
87        StrataError::Io(err)
88    }
89}