Skip to main content

lite_strtab/
error.rs

1//! Error types for string pool construction and internal validation.
2
3/// Result type used by this crate.
4pub type Result<T> = core::result::Result<T, Error>;
5
6/// Errors produced by the public construction API.
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8pub enum Error {
9    /// The number of strings exceeded what can be indexed by the configured
10    /// string ID type.
11    #[error(
12        "cannot store {strings} strings: id type '{id_type}' is too small; use a larger StringId type"
13    )]
14    TooManyStrings {
15        /// Attempted number of strings.
16        strings: usize,
17        /// ID type used by the pool/builder.
18        id_type: &'static str,
19    },
20    /// The total byte length exceeded what can be represented by the chosen
21    /// offset type.
22    #[error(
23        "cannot store {bytes} bytes of string data: offset type '{offset_type}' is too small; use a larger offset type"
24    )]
25    TooManyBytesForOffsetType {
26        /// Attempted byte length.
27        bytes: usize,
28        /// Offset type used by the pool/builder.
29        offset_type: &'static str,
30    },
31}
32
33#[cfg(any(debug_assertions, test))]
34pub(crate) type ValidationResult<T> = core::result::Result<T, ValidationError>;
35
36#[cfg(any(debug_assertions, test))]
37#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
38pub(crate) enum ValidationError {
39    #[error("invalid string table: {strings} strings do not fit in id type '{id_type}'")]
40    TooManyStrings {
41        strings: usize,
42        id_type: &'static str,
43    },
44    #[error("invalid string table: {bytes} bytes do not fit in offset type '{offset_type}'")]
45    TooManyBytesForOffsetType {
46        bytes: usize,
47        offset_type: &'static str,
48    },
49    #[error("invalid string table: offsets must end with a sentinel equal to total byte length")]
50    MissingSentinelOffset,
51    #[error("invalid string table: final offset is {found}, but byte length is {expected}")]
52    LastOffsetMismatch { found: usize, expected: usize },
53    #[error("invalid string table: offset[{index}] = {offset} is out of bounds (byte length {bytes_len})")]
54    OffsetOutOfBounds {
55        index: usize,
56        offset: usize,
57        bytes_len: usize,
58    },
59    #[error(
60        "invalid string table: offsets must be non-decreasing; offset[{index}] = {current}, previous = {previous}"
61    )]
62    OffsetsNotMonotonic {
63        index: usize,
64        previous: usize,
65        current: usize,
66    },
67    #[error("invalid string table: bytes for string index {index} are not valid UTF-8")]
68    InvalidUtf8 { index: usize },
69    #[error("invalid string table: string index {index} in null-padded mode has no trailing byte")]
70    NullPaddedStringMissingTerminatorByte { index: usize },
71    #[error(
72        "invalid string table: string index {index} in null-padded mode must end with a NUL byte"
73    )]
74    NullPaddedStringMissingTrailingNul { index: usize },
75}