icydb-core 0.70.7

IcyDB — A type-safe, embedded ORM and schema system for the Internet Computer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Module: cursor::error
//! Responsibility: cursor-domain typed error taxonomy and invariant construction helpers.
//! Does not own: planner policy derivation or runtime execution routing semantics.
//! Boundary: classifies continuation token/anchor/order/window failures for cursor consumers.

use crate::{
    db::{
        codec::cursor::CursorDecodeError,
        cursor::{ContinuationSignature, TokenWireError},
    },
    error::InternalError,
    value::Value,
};
use thiserror::Error as ThisError;

///
/// CursorPlanError
///
/// Cursor token and continuation boundary validation failures.
///

#[derive(Debug, ThisError)]
pub enum CursorPlanError {
    /// Cursor token could not be decoded.
    #[error("invalid continuation cursor: {reason}")]
    InvalidContinuationCursor { reason: CursorDecodeError },

    /// Cursor token payload/semantics are invalid after token decode.
    #[error("invalid continuation cursor: {reason}")]
    InvalidContinuationCursorPayload { reason: String },

    /// Cursor plan/runtime contract invariants were violated.
    #[error("{reason}")]
    ContinuationCursorInvariantViolation { reason: String },

    /// Cursor token version is unsupported.
    #[error("unsupported continuation cursor version: {version}")]
    ContinuationCursorVersionMismatch { version: u8 },

    /// Cursor token does not belong to this canonical query shape.
    #[error(
        "continuation cursor does not match query plan signature for '{entity_path}': expected={expected}, actual={actual}"
    )]
    ContinuationCursorSignatureMismatch {
        entity_path: &'static str,
        expected: String,
        actual: String,
    },

    /// Cursor boundary width does not match canonical order width.
    #[error("continuation cursor boundary arity mismatch: expected {expected}, found {found}")]
    ContinuationCursorBoundaryArityMismatch { expected: usize, found: usize },

    /// Cursor window offset does not match the current query window shape.
    #[error(
        "continuation cursor offset mismatch: expected {expected_offset}, found {actual_offset}"
    )]
    ContinuationCursorWindowMismatch {
        expected_offset: u32,
        actual_offset: u32,
    },

    /// Cursor boundary value type mismatch for a non-primary-key ordered field.
    #[error(
        "continuation cursor boundary type mismatch for field '{field}': expected {expected}, found {value:?}"
    )]
    ContinuationCursorBoundaryTypeMismatch {
        field: String,
        expected: String,
        value: Value,
    },

    /// Cursor primary-key boundary does not match the entity key type.
    #[error(
        "continuation cursor primary key type mismatch for '{field}': expected {expected}, found {value:?}"
    )]
    ContinuationCursorPrimaryKeyTypeMismatch {
        field: String,
        expected: String,
        value: Option<Value>,
    },
}

impl CursorPlanError {
    /// Canonical policy text for missing cursor ORDER BY requirements.
    pub(crate) const fn cursor_requires_order_message() -> &'static str {
        "cursor pagination requires an explicit ordering"
    }

    /// Canonical invariant text for cursor surfaces that require either
    /// explicit scalar ordering or canonical grouped ordering.
    pub(crate) const fn cursor_requires_explicit_or_grouped_ordering_message() -> &'static str {
        "cursor pagination requires explicit or grouped ordering"
    }

    /// Canonical policy text for missing cursor LIMIT requirements.
    pub(crate) const fn cursor_requires_limit_message() -> &'static str {
        "cursor pagination requires a limit"
    }

    /// Canonical payload text for empty cursor ORDER BY specifications.
    pub(crate) const fn cursor_requires_non_empty_order_message() -> &'static str {
        "cursor pagination requires non-empty ordering"
    }

    /// Construct one invalid cursor-token decode error.
    pub(in crate::db) const fn invalid_continuation_cursor(reason: CursorDecodeError) -> Self {
        Self::InvalidContinuationCursor { reason }
    }

    /// Construct the canonical invalid-continuation payload error variant.
    pub(in crate::db) fn invalid_continuation_cursor_payload(reason: impl Into<String>) -> Self {
        Self::InvalidContinuationCursorPayload {
            reason: reason.into(),
        }
    }

    /// Construct one schema-validation payload error for cursor boundaries.
    pub(in crate::db) fn invalid_continuation_cursor_schema(
        reason: impl std::fmt::Display,
    ) -> Self {
        Self::invalid_continuation_cursor_payload(reason.to_string())
    }

    /// Construct one cursor-direction mismatch payload error.
    pub(in crate::db) fn continuation_cursor_direction_mismatch() -> Self {
        Self::invalid_continuation_cursor_payload(
            "continuation cursor direction does not match executable plan direction",
        )
    }

    /// Construct one grouped-cursor direction payload error.
    pub(in crate::db) fn grouped_continuation_cursor_direction_ascending_required() -> Self {
        Self::invalid_continuation_cursor_payload(
            "grouped continuation cursor direction must be ascending",
        )
    }

    /// Construct one unknown ORDER BY field payload error.
    pub(in crate::db) fn continuation_cursor_unknown_order_field(field: &str) -> Self {
        Self::invalid_continuation_cursor_payload(format!("unknown order field '{field}'"))
    }

    /// Construct one deterministic tie-break payload error.
    pub(in crate::db) fn continuation_cursor_primary_key_tie_break_required(
        pk_field: &str,
    ) -> Self {
        Self::invalid_continuation_cursor_payload(format!(
            "order specification must end with primary key '{pk_field}' as deterministic tie-break"
        ))
    }

    /// Construct one anchor decode failure payload error.
    pub(in crate::db) fn index_range_anchor_decode_failed(reason: impl Into<String>) -> Self {
        Self::invalid_continuation_cursor_payload(format!(
            "index-range continuation anchor decode failed: {}",
            reason.into(),
        ))
    }

    /// Construct one canonical-anchor encoding mismatch payload error.
    pub(in crate::db) fn index_range_anchor_canonical_encoding_mismatch() -> Self {
        Self::invalid_continuation_cursor_payload(
            "index-range continuation anchor canonical encoding mismatch",
        )
    }

    /// Construct one anchor index-id mismatch payload error.
    pub(in crate::db) fn index_range_anchor_index_id_mismatch() -> Self {
        Self::invalid_continuation_cursor_payload(
            "index-range continuation anchor index id mismatch",
        )
    }

    /// Construct one anchor key-namespace mismatch payload error.
    pub(in crate::db) fn index_range_anchor_key_namespace_mismatch() -> Self {
        Self::invalid_continuation_cursor_payload(
            "index-range continuation anchor key namespace mismatch",
        )
    }

    /// Construct one anchor component-arity mismatch payload error.
    pub(in crate::db) fn index_range_anchor_component_arity_mismatch() -> Self {
        Self::invalid_continuation_cursor_payload(
            "index-range continuation anchor component arity mismatch",
        )
    }

    /// Construct one out-of-envelope anchor payload error.
    pub(in crate::db) fn index_range_anchor_outside_envelope() -> Self {
        Self::invalid_continuation_cursor_payload(
            "index-range continuation anchor is outside the original range envelope",
        )
    }

    /// Construct one composite-plan anchor rejection payload error.
    pub(in crate::db) fn unexpected_index_range_anchor_for_composite_plan() -> Self {
        Self::invalid_continuation_cursor_payload(
            "unexpected index-range continuation anchor for composite access plan",
        )
    }

    /// Construct one missing semantic-bounds payload error.
    pub(in crate::db) fn index_range_anchor_semantic_bounds_required() -> Self {
        Self::invalid_continuation_cursor_payload(
            "index-range continuation validation is missing semantic bounds payload",
        )
    }

    /// Construct one missing raw anchor payload error.
    pub(in crate::db) fn index_range_anchor_required() -> Self {
        Self::invalid_continuation_cursor_payload(
            "index-range continuation cursor is missing a raw-key anchor",
        )
    }

    /// Construct one non-index-range path anchor rejection payload error.
    pub(in crate::db) fn unexpected_index_range_anchor_for_non_range_path() -> Self {
        Self::invalid_continuation_cursor_payload(
            "unexpected index-range continuation anchor for non-index-range access path",
        )
    }

    /// Construct one anchor-primary-key decode failure payload error.
    pub(in crate::db) fn index_range_anchor_primary_key_decode_failed(
        reason: impl std::fmt::Display,
    ) -> Self {
        Self::invalid_continuation_cursor_payload(format!(
            "index-range continuation anchor primary key decode failed: {reason}",
        ))
    }

    /// Construct one boundary-primary-key decode failure payload error.
    pub(in crate::db) fn index_range_boundary_primary_key_decode_failed(
        reason: impl std::fmt::Display,
    ) -> Self {
        Self::invalid_continuation_cursor_payload(format!(
            "index-range continuation boundary primary key decode failed: {reason}",
        ))
    }

    /// Construct one boundary/anchor mismatch payload error.
    pub(in crate::db) fn index_range_boundary_anchor_mismatch() -> Self {
        Self::invalid_continuation_cursor_payload(
            "index-range continuation boundary/anchor mismatch",
        )
    }

    /// Construct one cursor invariant-violation error variant.
    pub(in crate::db) fn continuation_cursor_invariant(reason: impl Into<String>) -> Self {
        Self::ContinuationCursorInvariantViolation {
            reason: reason.into(),
        }
    }

    /// Construct one invariant error for missing explicit cursor ordering.
    pub(in crate::db) fn cursor_requires_order() -> Self {
        Self::continuation_cursor_invariant(Self::cursor_requires_order_message())
    }

    /// Construct one invariant error for cursor surfaces that require either
    /// explicit scalar ordering or canonical grouped ordering.
    pub(in crate::db) fn cursor_requires_explicit_or_grouped_ordering() -> Self {
        Self::continuation_cursor_invariant(
            Self::cursor_requires_explicit_or_grouped_ordering_message(),
        )
    }

    /// Construct one invariant error for empty cursor ORDER BY specifications.
    pub(in crate::db) fn cursor_requires_non_empty_order() -> Self {
        Self::continuation_cursor_invariant(Self::cursor_requires_non_empty_order_message())
    }

    /// Construct one cursor version mismatch error.
    pub(in crate::db) const fn continuation_cursor_version_mismatch(version: u8) -> Self {
        Self::ContinuationCursorVersionMismatch { version }
    }

    /// Construct one cursor-signature mismatch error for the current entity path.
    pub(in crate::db) fn continuation_cursor_signature_mismatch(
        entity_path: &'static str,
        expected: &ContinuationSignature,
        actual: &ContinuationSignature,
    ) -> Self {
        Self::ContinuationCursorSignatureMismatch {
            entity_path,
            expected: expected.to_string(),
            actual: actual.to_string(),
        }
    }

    /// Construct one cursor boundary arity mismatch error.
    pub(in crate::db) const fn continuation_cursor_boundary_arity_mismatch(
        expected: usize,
        found: usize,
    ) -> Self {
        Self::ContinuationCursorBoundaryArityMismatch { expected, found }
    }

    /// Construct one cursor window mismatch error.
    pub(in crate::db) const fn continuation_cursor_window_mismatch(
        expected_offset: u32,
        actual_offset: u32,
    ) -> Self {
        Self::ContinuationCursorWindowMismatch {
            expected_offset,
            actual_offset,
        }
    }

    /// Construct one non-primary-key boundary type mismatch error.
    pub(in crate::db) fn continuation_cursor_boundary_type_mismatch(
        field: impl Into<String>,
        expected: impl Into<String>,
        value: Value,
    ) -> Self {
        Self::ContinuationCursorBoundaryTypeMismatch {
            field: field.into(),
            expected: expected.into(),
            value,
        }
    }

    /// Construct one primary-key boundary type mismatch error.
    pub(in crate::db) fn continuation_cursor_primary_key_type_mismatch(
        field: impl Into<String>,
        expected: impl Into<String>,
        value: Option<Value>,
    ) -> Self {
        Self::ContinuationCursorPrimaryKeyTypeMismatch {
            field: field.into(),
            expected: expected.into(),
            value,
        }
    }

    /// Map cursor token decode failures into canonical plan-surface cursor errors.
    pub(in crate::db) fn from_token_wire_error(err: TokenWireError) -> Self {
        match err {
            TokenWireError::Encode(message) | TokenWireError::Decode(message) => {
                Self::invalid_continuation_cursor_payload(message)
            }
            TokenWireError::UnsupportedVersion { version } => {
                Self::continuation_cursor_version_mismatch(version)
            }
        }
    }

    /// Map one primary-key cursor decode failure into the executor-facing
    /// internal invariant taxonomy used by storage-key boundary adapters.
    pub(in crate::db) fn into_pk_cursor_decode_internal_error(self) -> InternalError {
        match self {
            Self::InvalidContinuationCursor { reason } => InternalError::cursor_executor_invariant(
                format!("pk cursor decode rejected invalid continuation cursor: {reason}"),
            ),
            Self::InvalidContinuationCursorPayload { reason } => {
                InternalError::cursor_executor_invariant(format!(
                    "pk cursor decode rejected invalid continuation payload: {reason}"
                ))
            }
            Self::ContinuationCursorVersionMismatch { version } => {
                InternalError::cursor_executor_invariant(format!(
                    "pk cursor decode rejected unsupported continuation version: {version}"
                ))
            }
            Self::ContinuationCursorSignatureMismatch { .. } => {
                InternalError::cursor_executor_invariant(
                    "pk cursor decode encountered continuation signature mismatch",
                )
            }
            Self::ContinuationCursorBoundaryArityMismatch { expected, found } => {
                InternalError::cursor_executor_invariant(format!(
                    "pk cursor boundary arity mismatch: expected {expected}, found {found}"
                ))
            }
            Self::ContinuationCursorWindowMismatch {
                expected_offset,
                actual_offset,
            } => InternalError::cursor_executor_invariant(format!(
                "pk cursor window mismatch: expected_offset={expected_offset}, actual_offset={actual_offset}"
            )),
            Self::ContinuationCursorBoundaryTypeMismatch { field, .. } => {
                InternalError::cursor_executor_invariant(format!(
                    "pk cursor boundary type mismatch on field '{field}'"
                ))
            }
            Self::ContinuationCursorPrimaryKeyTypeMismatch { value: None, .. } => {
                InternalError::cursor_executor_invariant("pk cursor slot must be present")
            }
            Self::ContinuationCursorPrimaryKeyTypeMismatch { value: Some(_), .. } => {
                InternalError::cursor_executor_invariant("pk cursor slot type mismatch")
            }
            Self::ContinuationCursorInvariantViolation { reason } => {
                InternalError::cursor_executor_invariant(reason)
            }
        }
    }

    /// Map cursor-plan failures into runtime taxonomy classes.
    ///
    /// Cursor token/version/signature/window/payload mismatches are external
    /// input failures (`Unsupported` at cursor origin). Only explicit
    /// continuation invariant violations remain invariant-class failures.
    pub(crate) fn into_internal_error(self) -> InternalError {
        match self {
            Self::ContinuationCursorInvariantViolation { reason } => {
                InternalError::cursor_executor_invariant(reason)
            }
            Self::InvalidContinuationCursor { .. }
            | Self::InvalidContinuationCursorPayload { .. }
            | Self::ContinuationCursorVersionMismatch { .. }
            | Self::ContinuationCursorSignatureMismatch { .. }
            | Self::ContinuationCursorBoundaryArityMismatch { .. }
            | Self::ContinuationCursorWindowMismatch { .. }
            | Self::ContinuationCursorBoundaryTypeMismatch { .. }
            | Self::ContinuationCursorPrimaryKeyTypeMismatch { .. } => {
                InternalError::cursor_unsupported(self.to_string())
            }
        }
    }
}