j2k-types 0.10.0

Shared JPEG 2000 and HTJ2K codec value contracts and dispatch accounting
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Source-preserving failures for the shared encode-stage accelerator SPI.

use alloc::boxed::Box;
use core::error::Error;
use core::fmt;

/// Result returned by fallible encode-stage accelerator hooks.
pub type J2kEncodeStageResult<T> = Result<T, J2kEncodeStageError>;

/// Stable category for an encode-stage accelerator failure.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum J2kEncodeStageErrorKind {
    /// The submitted stage request was malformed.
    InvalidRequest,
    /// The stage shape or capability is unsupported.
    Unsupported,
    /// Checked stage size arithmetic overflowed.
    ArithmeticOverflow,
    /// Simultaneously live host allocations would exceed the shared cap.
    MemoryCapExceeded,
    /// A cap-valid host reservation failed.
    HostAllocationFailed,
    /// A backend or runtime accepted work and failed it.
    Backend,
    /// Backend or adapter state violated an invariant.
    InternalInvariant,
}

/// Failure returned by a shared encode-stage accelerator hook.
///
/// Equality for [`Self::Backend`] is intentionally source-identity based:
/// independently constructed dynamic backend errors are not assumed to have
/// semantic equality merely because their rendered text matches.
#[derive(Debug)]
#[non_exhaustive]
pub enum J2kEncodeStageError {
    /// The submitted stage request was malformed.
    InvalidRequest {
        /// Stable description of the invalid request.
        what: &'static str,
    },
    /// The requested stage shape or capability is unsupported.
    Unsupported {
        /// Stable description of the unsupported request.
        what: &'static str,
    },
    /// Checked size arithmetic overflowed.
    ArithmeticOverflow {
        /// Stage value or phase whose arithmetic overflowed.
        what: &'static str,
    },
    /// Simultaneously live host allocations would exceed the shared cap.
    MemoryCapExceeded {
        /// Stage allocation or phase being checked.
        what: &'static str,
        /// Checked requested live bytes.
        requested: usize,
        /// Maximum permitted live bytes.
        cap: usize,
    },
    /// A cap-valid host allocation failed.
    HostAllocationFailed {
        /// Stage allocation that failed.
        what: &'static str,
        /// Requested allocation bytes.
        bytes: usize,
    },
    /// A backend or runtime accepted work and failed it.
    Backend {
        /// Stable backend name.
        backend: &'static str,
        /// Stable backend operation name.
        operation: &'static str,
        /// Concrete backend or runtime source.
        source: Box<dyn Error + Send + Sync + 'static>,
    },
    /// Backend or adapter state violated an invariant.
    InternalInvariant {
        /// Stable description of the violated invariant.
        what: &'static str,
    },
}

impl J2kEncodeStageError {
    /// Constructs an invalid-request failure.
    pub const fn invalid_request(what: &'static str) -> Self {
        Self::InvalidRequest { what }
    }

    /// Constructs an unsupported-capability failure.
    pub const fn unsupported(what: &'static str) -> Self {
        Self::Unsupported { what }
    }

    /// Constructs an arithmetic-overflow failure.
    pub const fn arithmetic_overflow(what: &'static str) -> Self {
        Self::ArithmeticOverflow { what }
    }

    /// Constructs a shared-cap failure.
    pub const fn memory_cap_exceeded(what: &'static str, requested: usize, cap: usize) -> Self {
        Self::MemoryCapExceeded {
            what,
            requested,
            cap,
        }
    }

    /// Constructs a host-allocation failure without allocating a message.
    pub const fn host_allocation_failed(what: &'static str, bytes: usize) -> Self {
        Self::HostAllocationFailed { what, bytes }
    }

    /// Retains a concrete backend or runtime source.
    pub fn backend(
        backend: &'static str,
        operation: &'static str,
        source: impl Error + Send + Sync + 'static,
    ) -> Self {
        Self::Backend {
            backend,
            operation,
            source: Box::new(source),
        }
    }

    /// Constructs an internal-invariant failure.
    pub const fn internal_invariant(what: &'static str) -> Self {
        Self::InternalInvariant { what }
    }

    /// Returns the stable failure category.
    pub const fn kind(&self) -> J2kEncodeStageErrorKind {
        match self {
            Self::InvalidRequest { .. } => J2kEncodeStageErrorKind::InvalidRequest,
            Self::Unsupported { .. } => J2kEncodeStageErrorKind::Unsupported,
            Self::ArithmeticOverflow { .. } => J2kEncodeStageErrorKind::ArithmeticOverflow,
            Self::MemoryCapExceeded { .. } => J2kEncodeStageErrorKind::MemoryCapExceeded,
            Self::HostAllocationFailed { .. } => J2kEncodeStageErrorKind::HostAllocationFailed,
            Self::Backend { .. } => J2kEncodeStageErrorKind::Backend,
            Self::InternalInvariant { .. } => J2kEncodeStageErrorKind::InternalInvariant,
        }
    }

    /// Returns stable presentation text for policy logs and legacy diagnostics.
    pub const fn reason(&self) -> &'static str {
        match self {
            Self::InvalidRequest { what }
            | Self::Unsupported { what }
            | Self::ArithmeticOverflow { what }
            | Self::MemoryCapExceeded { what, .. }
            | Self::HostAllocationFailed { what, .. }
            | Self::InternalInvariant { what } => what,
            Self::Backend { operation, .. } => operation,
        }
    }
}

impl fmt::Display for J2kEncodeStageError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidRequest { what } => write!(formatter, "invalid stage request: {what}"),
            Self::Unsupported { what } => write!(formatter, "unsupported stage request: {what}"),
            Self::ArithmeticOverflow { what } => {
                write!(formatter, "stage arithmetic overflow: {what}")
            }
            Self::MemoryCapExceeded {
                what,
                requested,
                cap,
            } => write!(
                formatter,
                "{what} requires {requested} live host bytes, exceeding the {cap}-byte cap"
            ),
            Self::HostAllocationFailed { what, bytes } => {
                write!(
                    formatter,
                    "host allocation failed for {bytes} bytes while allocating {what}"
                )
            }
            Self::Backend {
                backend,
                operation,
                source,
            } => write!(formatter, "{backend} {operation} failed: {source}"),
            Self::InternalInvariant { what } => {
                write!(formatter, "encode-stage invariant failed: {what}")
            }
        }
    }
}

impl Error for J2kEncodeStageError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Backend { source, .. } => Some(source.as_ref()),
            _ => None,
        }
    }
}

impl PartialEq for J2kEncodeStageError {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::InvalidRequest { what: left }, Self::InvalidRequest { what: right })
            | (Self::Unsupported { what: left }, Self::Unsupported { what: right })
            | (Self::ArithmeticOverflow { what: left }, Self::ArithmeticOverflow { what: right })
            | (Self::InternalInvariant { what: left }, Self::InternalInvariant { what: right }) => {
                left == right
            }
            (
                Self::MemoryCapExceeded {
                    what: left_what,
                    requested: left_requested,
                    cap: left_cap,
                },
                Self::MemoryCapExceeded {
                    what: right_what,
                    requested: right_requested,
                    cap: right_cap,
                },
            ) => {
                left_what == right_what
                    && left_requested == right_requested
                    && left_cap == right_cap
            }
            (
                Self::HostAllocationFailed {
                    what: left_what,
                    bytes: left_bytes,
                },
                Self::HostAllocationFailed {
                    what: right_what,
                    bytes: right_bytes,
                },
            ) => left_what == right_what && left_bytes == right_bytes,
            (
                Self::Backend {
                    backend: left_backend,
                    operation: left_operation,
                    source: left_source,
                },
                Self::Backend {
                    backend: right_backend,
                    operation: right_operation,
                    source: right_source,
                },
            ) => {
                left_backend == right_backend
                    && left_operation == right_operation
                    && core::ptr::eq(left_source, right_source)
            }
            _ => false,
        }
    }
}

impl Eq for J2kEncodeStageError {}

#[cfg(test)]
mod tests {
    use alloc::string::ToString;
    use core::{error::Error, fmt};

    use super::{J2kEncodeStageError, J2kEncodeStageErrorKind};

    #[derive(Debug)]
    struct TestBackendError;

    impl fmt::Display for TestBackendError {
        fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("backend fixture")
        }
    }

    impl Error for TestBackendError {}

    #[test]
    fn every_stage_category_has_stable_kind_and_reason() {
        let cases = [
            (
                J2kEncodeStageError::invalid_request("invalid"),
                J2kEncodeStageErrorKind::InvalidRequest,
                "invalid",
            ),
            (
                J2kEncodeStageError::unsupported("unsupported"),
                J2kEncodeStageErrorKind::Unsupported,
                "unsupported",
            ),
            (
                J2kEncodeStageError::arithmetic_overflow("overflow"),
                J2kEncodeStageErrorKind::ArithmeticOverflow,
                "overflow",
            ),
            (
                J2kEncodeStageError::memory_cap_exceeded("cap", 9, 8),
                J2kEncodeStageErrorKind::MemoryCapExceeded,
                "cap",
            ),
            (
                J2kEncodeStageError::host_allocation_failed("allocation", 9),
                J2kEncodeStageErrorKind::HostAllocationFailed,
                "allocation",
            ),
            (
                J2kEncodeStageError::internal_invariant("invariant"),
                J2kEncodeStageErrorKind::InternalInvariant,
                "invariant",
            ),
        ];
        for (error, kind, reason) in cases {
            assert_eq!(error.kind(), kind);
            assert_eq!(error.reason(), reason);
            assert!(Error::source(&error).is_none());
        }
    }

    #[test]
    fn backend_failure_retains_concrete_source() {
        let error = J2kEncodeStageError::backend("test", "dispatch", TestBackendError);
        assert_eq!(error.kind(), J2kEncodeStageErrorKind::Backend);
        assert_eq!(error.reason(), "dispatch");
        assert!(Error::source(&error)
            .and_then(|source| source.downcast_ref::<TestBackendError>())
            .is_some());
    }

    #[test]
    fn every_stage_category_has_stable_display_text() {
        let cases = [
            (
                J2kEncodeStageError::invalid_request("dimensions"),
                "invalid stage request: dimensions",
            ),
            (
                J2kEncodeStageError::unsupported("sampling"),
                "unsupported stage request: sampling",
            ),
            (
                J2kEncodeStageError::arithmetic_overflow("coefficient count"),
                "stage arithmetic overflow: coefficient count",
            ),
            (
                J2kEncodeStageError::memory_cap_exceeded("workspace", 9, 8),
                "workspace requires 9 live host bytes, exceeding the 8-byte cap",
            ),
            (
                J2kEncodeStageError::host_allocation_failed("workspace", 9),
                "host allocation failed for 9 bytes while allocating workspace",
            ),
            (
                J2kEncodeStageError::internal_invariant("result count"),
                "encode-stage invariant failed: result count",
            ),
        ];

        for (error, expected) in cases {
            assert_eq!(error.to_string(), expected);
        }

        let backend = J2kEncodeStageError::backend("cuda", "dispatch", TestBackendError);
        assert_eq!(backend.to_string(), "cuda dispatch failed: backend fixture");
    }

    #[test]
    fn equality_compares_typed_payloads_and_backend_source_identity() {
        assert_eq!(
            J2kEncodeStageError::invalid_request("dimensions"),
            J2kEncodeStageError::invalid_request("dimensions")
        );
        assert_ne!(
            J2kEncodeStageError::invalid_request("dimensions"),
            J2kEncodeStageError::unsupported("dimensions")
        );
        assert_eq!(
            J2kEncodeStageError::memory_cap_exceeded("workspace", 9, 8),
            J2kEncodeStageError::memory_cap_exceeded("workspace", 9, 8)
        );
        assert_ne!(
            J2kEncodeStageError::memory_cap_exceeded("workspace", 9, 8),
            J2kEncodeStageError::memory_cap_exceeded("workspace", 10, 8)
        );
        assert_eq!(
            J2kEncodeStageError::host_allocation_failed("workspace", 9),
            J2kEncodeStageError::host_allocation_failed("workspace", 9)
        );
        assert_ne!(
            J2kEncodeStageError::host_allocation_failed("workspace", 9),
            J2kEncodeStageError::host_allocation_failed("workspace", 10)
        );

        let backend = J2kEncodeStageError::backend("cuda", "dispatch", TestBackendError);
        assert_eq!(backend, backend);
        assert_ne!(
            backend,
            J2kEncodeStageError::backend("cuda", "dispatch", TestBackendError)
        );
    }
}