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