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
//! Error management.

use std::io;
use thiserror::Error;

pub(crate) type Result<T> = std::result::Result<T, Error>;

/// Super error type for all runefs errors.
#[derive(Error, Debug)]
pub enum Error {
    /// Wrapper for the std::io::Error type.
    #[error(transparent)]
    Io(#[from] io::Error),
    #[error(transparent)]
    Read(#[from] ReadError),
    #[error(transparent)]
    Compression(#[from] CompressionUnsupported),
    /// Clarification error for failed parsers.
    #[error(transparent)]
    Parse(#[from] ParseError),
}

impl From<nom::Err<()>> for Error {
    #[inline]
    fn from(_: nom::Err<()>) -> Self {
        Self::Parse(ParseError::Unknown)
    }
}

#[derive(Error, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum ReadError {
    #[error("index {0} not found")]
    IndexNotFound(u8),
    #[error("index {idx} does not contain archive group {arc}")]
    ArchiveNotFound { idx: u8, arc: u32 },
    #[error("sector archive id was {0} but expected {1}")]
    SectorArchiveMismatch(u32, u32),
    #[error("sector chunk was {0} but expected {1}")]
    SectorChunkMismatch(usize, usize),
    #[error("sector next was {0} but expected {1}")]
    SectorNextMismatch(u32, u32),
    #[error("sector parent index id was {0} but expected {1}")]
    SectorIndexMismatch(u8, u8),
}

#[derive(Error, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[error("unsupported compression type {0}")]
pub struct CompressionUnsupported(pub(crate) u8);

#[derive(Error, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum ParseError {
    #[error("unknown parser error")]
    Unknown,
    #[error("unable to parse archive {0}, unexpected eof")]
    Archive(u32),
    #[error("unable to parse child sector of parent {0}, unexpected eof")]
    Sector(usize),
}