Skip to main content

docs_search/
error.rs

1//! Typed, exhaustive errors for index build and decode.
2
3use std::fmt;
4
5/// Everything that can go wrong building or reading an index.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum IndexError {
9    /// The blob does not start with the format magic.
10    InvalidMagic {
11        /// The first bytes found, for diagnosis; never assumed valid UTF-8.
12        found: [u8; 6],
13    },
14    /// The format version is newer than this crate understands.
15    UnsupportedVersion {
16        /// The version read from the header.
17        found: u32,
18        /// The highest version this crate decodes.
19        supported: u32,
20    },
21    /// The blob ends before a section the header promises.
22    Truncated {
23        /// What was being read when the blob ran out.
24        section: &'static str,
25    },
26    /// A section's internal structure is inconsistent (bad offset, bad
27    /// bitmap bytes, overflowing count).
28    Corrupt {
29        /// What was inconsistent, for diagnosis.
30        reason: &'static str,
31    },
32    /// The FST dictionary itself failed to load.
33    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}