Skip to main content

mlt_core/
errors.rs

1use std::convert::Infallible;
2use std::num::TryFromIntError;
3
4use num_enum::TryFromPrimitiveError;
5
6use crate::decoder::{
7    GeometryType, LogicalEncoding, LogicalTechnique, PhysicalEncoding, StreamType,
8};
9use crate::utils::AsUsize;
10
11pub type MltResult<T> = Result<T, MltError>;
12pub(crate) type MltRefResult<'a, T> = Result<(&'a [u8], T), MltError>;
13
14#[derive(Debug, thiserror::Error)]
15#[non_exhaustive]
16pub enum MltError {
17    #[error("cannot decode {0} as {1}")]
18    DataWidthMismatch(&'static str, &'static str),
19    #[error("dictionary index {0} out of bounds (len={1})")]
20    DictIndexOutOfBounds(u32, usize),
21    #[error("duplicate value found where unique required")]
22    DuplicateValue,
23    #[error("Integer overflow")]
24    IntegerOverflow,
25    #[error("missing geometry column in feature table")]
26    MissingGeometry,
27    #[error("missing string stream: {0}")]
28    MissingStringStream(&'static str),
29    #[error("multiple geometry columns found (only one allowed)")]
30    MultipleGeometryColumns,
31    #[error("multiple ID columns found (only one allowed)")]
32    MultipleIdColumns,
33    #[error("varint uses more bytes than necessary (non-canonical encoding)")]
34    NonCanonicalVarInt,
35    #[error("{0} is not decoded")]
36    NotDecoded(&'static str),
37    #[error("decoded data is not in encoded form")]
38    NotEncoded,
39    #[error("error parsing column type: code={0}")]
40    ParsingColumnType(u8),
41    #[error("error parsing logical technique: code={0}")]
42    ParsingLogicalTechnique(u8),
43    #[error("error parsing physical encoding: code={0}")]
44    ParsingPhysicalEncoding(u8),
45    #[error("error parsing stream type: code={0}")]
46    ParsingStreamType(u8),
47    #[error("found {0} bytes after the expected end of layer")]
48    TrailingLayerData(usize),
49    #[error("unexpected end of input (unable to take {0} bytes)")]
50    UnableToTake(u32),
51    #[error("unexpected stream type {0:?}")]
52    UnexpectedStreamType(StreamType),
53    #[error("unexpected stream type {0:?}, expected {1} for {2}")]
54    UnexpectedStreamType2(StreamType, &'static str, &'static str),
55    #[error("unsupported logical encoding {0:?} for {1}")]
56    UnsupportedLogicalEncoding(LogicalEncoding, &'static str),
57    #[error("invalid combination of logical encodings: {0:?} + {1:?}")]
58    InvalidLogicalEncodings(LogicalTechnique, LogicalTechnique),
59    #[error("layer has zero size")]
60    ZeroLayerSize,
61    #[error("The encoder used to optimise data is incompatible")]
62    BadEncoderDataCombination,
63    #[error("StagedLayer::encode_explicit requires Encoder.explicit to be Some(_)")]
64    MissingExplicitEncoder,
65
66    // Wire/codec decoding (bytes → primitives)
67    #[error("buffer underflow: needed {0} bytes, but only {1} remain")]
68    BufferUnderflow(u32, usize),
69    #[error("FastPFor decode failed: expected={0} got={1}")]
70    FastPforDecode(u32, usize),
71    #[error("invalid RLE run length (cannot convert to usize): value={0}")]
72    RleRunLenInvalid(i128),
73
74    // Structural constraints (lengths, counts, shapes)
75    #[error("geometry requires at least 1 stream, got 0")]
76    GeometryWithoutStreams,
77    #[error("FastPFor data byte length expected multiple of 4, got {0}")]
78    InvalidFastPforByteLength(usize),
79    #[error("vec2 delta stream size expected to be non-empty and multiple of 2, got {0}")]
80    InvalidPairStreamSize(usize),
81    #[error("decodable stream size expected {1}, got {0}")]
82    InvalidDecodingStreamSize(usize, usize),
83    #[error("IDs missing for encoding (expected Some IDs, got None)")]
84    IdsMissingForEncoding,
85    #[error("missing struct encoder for struct")]
86    MissingStructEncoderForStruct,
87    #[error("previous decode/parsing attempt failed")]
88    PriorParseFailure,
89    #[error("presence stream has {0} bits set but {1} values provided")]
90    PresenceValueCountMismatch(usize, usize),
91    #[error("MVT parse error: {0}")]
92    MvtParse(String),
93    #[error("need to encode before being able to write")]
94    NeedsEncodingBeforeWriting,
95    #[error("memory limit exceeded: limit={limit}, used={used}, requested={requested}")]
96    MemoryLimitExceeded {
97        limit: u32,
98        used: u32,
99        requested: u32,
100    },
101    #[error("not implemented: {0}")]
102    NotImplemented(&'static str),
103    #[error("unsupported property value and encoder combination: {0:?} + {1:?}")]
104    UnsupportedPropertyEncoderCombination(&'static str, &'static str),
105    #[error("mixed property types are not allowed in column {0} ({1})")]
106    MixedPropertyTypes(usize, String),
107    #[error("shared dictionary requires at least 2 streams, got {0}")]
108    SharedDictRequiresStreams(usize),
109    #[error("unsupported string stream count (expected between 2 and 5): {0}")]
110    UnsupportedStringStreamCount(usize),
111    #[error("Structs are not allowed to be optional")]
112    TriedToEncodeOptionalStruct,
113    #[error(
114        "encoding instruction count mismatch: expected {input_len} instructions for {input_len} properties, got {config_len}"
115    )]
116    EncodingInstructionCountMismatch { input_len: usize, config_len: usize },
117    #[error("struct child data streams expected exactly 1 value, got {0}")]
118    UnexpectedStructChildCount(u32),
119    // Note that {expected}+1 is allowed for the legacy Java encoder bug
120    #[error("SharedDict stream count is {actual}, expected {expected}")]
121    InvalidSharedDictStreamCount { actual: u32, expected: u32 },
122    #[error("unsupported physical encoding: {0}")]
123    UnsupportedPhysicalEncoding(&'static str),
124    #[error("unsupported physical encoding: {0:?} for {1}")]
125    UnsupportedPhysicalEncodingForType(PhysicalEncoding, &'static str),
126    #[error(
127        "Extent {extent} cannot be encoded to morton due to morton allowing max. 16 bits, but {required_bits} would be required"
128    )]
129    VertexMortonNotCompatibleWithExtent { extent: u32, required_bits: u32 },
130    #[error("Morton stream uses {0} bits, but at most 16 bits are supported")]
131    InvalidMortonBits(u32),
132
133    // Geometry decode errors (field = variable name, geom_type for context)
134    #[error("MVT error: {0}")]
135    BadMvtGeometry(&'static str),
136    #[error("geometry[{0}]: index out of bounds")]
137    GeometryIndexOutOfBounds(usize),
138    #[error("geometry[{index}]: {field}[{idx}] out of bounds (len={len})")]
139    GeometryOutOfBounds {
140        index: usize,
141        field: &'static str,
142        idx: usize,
143        len: usize,
144    },
145    #[error("geometry[{index}]: vertex {vertex} out of bounds (count={count})")]
146    GeometryVertexOutOfBounds {
147        index: usize,
148        vertex: usize,
149        count: usize,
150    },
151    #[error("geometry[{0}]: {1} requires geometry_offsets")]
152    NoGeometryOffsets(usize, GeometryType),
153    #[error("geometry[{0}]: {1} requires part_offsets")]
154    NoPartOffsets(usize, GeometryType),
155    #[error("geometry[{0}]: {1} requires ring_offsets")]
156    NoRingOffsets(usize, GeometryType),
157    #[error("geometry[{0}]: unexpected offset combination for {1}")]
158    UnexpectedOffsetCombination(usize, GeometryType),
159
160    #[error("FastPFor error: {0}")]
161    FastPfor(#[from] fastpfor::FastPForError),
162    #[error(transparent)]
163    Io(#[from] std::io::Error),
164    #[error("Serde JSON error: {0}")]
165    SerdeJsonError(#[from] serde_json::Error),
166    #[error("integer conversion error: {0}")]
167    TryFromIntError(#[from] TryFromIntError),
168    #[error("num_enum conversion error: {0}")]
169    TryFromPrimitive(#[from] TryFromPrimitiveError<GeometryType>),
170    #[error("UTF-8 decode error: {0}")]
171    Utf8(#[from] std::str::Utf8Error),
172    #[error("UTF-8 decode error: {0}")]
173    FromUtf8(#[from] std::string::FromUtf8Error),
174}
175
176impl From<Infallible> for MltError {
177    fn from(_: Infallible) -> Self {
178        unreachable!()
179    }
180}
181
182impl From<MltError> for std::io::Error {
183    fn from(value: MltError) -> Self {
184        match value {
185            MltError::Io(e) => e,
186            other => Self::other(other),
187        }
188    }
189}
190
191pub(crate) trait AsMltError<T> {
192    fn or_overflow(&self) -> MltResult<T>;
193}
194
195impl<T: Copy> AsMltError<T> for Option<T> {
196    #[inline]
197    fn or_overflow(&self) -> MltResult<T> {
198        self.ok_or(MltError::IntegerOverflow)
199    }
200}
201
202impl AsMltError<u32> for Result<u32, TryFromIntError> {
203    #[inline]
204    fn or_overflow(&self) -> MltResult<u32> {
205        self.map_err(|_| MltError::IntegerOverflow)
206    }
207}
208
209#[inline]
210pub(crate) fn fail_if_invalid_stream_size<T: AsUsize>(actual: T, expected: T) -> MltResult<()> {
211    if actual == expected {
212        Ok(())
213    } else {
214        Err(MltError::InvalidDecodingStreamSize(
215            actual.as_usize(),
216            expected.as_usize(),
217        ))
218    }
219}