1use std::fmt;
4
5#[derive(Debug)]
7#[non_exhaustive]
8pub enum IndexError {
9 InvalidMagic {
11 found: [u8; 6],
13 },
14 UnsupportedVersion {
16 found: u32,
18 supported: u32,
20 },
21 Truncated {
23 section: &'static str,
25 },
26 Corrupt {
29 reason: &'static str,
31 },
32 Fst(fst::Error),
34}
35
36impl fmt::Display for IndexError {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 match self {
39 Self::InvalidMagic { found } => {
40 write!(f, "invalid magic: expected \"DSIDX1\", found {found:02x?}")
41 }
42 Self::UnsupportedVersion { found, supported } => {
43 write!(
44 f,
45 "index version {found} unsupported; this crate decodes up to {supported}"
46 )
47 }
48 Self::Truncated { section } => {
49 write!(f, "truncated index: ran out of bytes reading {section}")
50 }
51 Self::Corrupt { reason } => write!(f, "corrupt index: {reason}"),
52 Self::Fst(e) => write!(f, "term dictionary error: {e}"),
53 }
54 }
55}
56
57impl std::error::Error for IndexError {
58 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
59 match self {
60 Self::Fst(e) => Some(e),
61 _ => None,
62 }
63 }
64}
65
66impl From<fst::Error> for IndexError {
67 fn from(e: fst::Error) -> Self {
68 Self::Fst(e)
69 }
70}