Skip to main content

j2k_cuda/error/
native_source.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use j2k_native::{DecodeError, DecodeErrorClass};
4
5/// Opaque, CUDA-adapter-owned source for native decoder failures.
6///
7/// The concrete native error remains available through
8/// [`core::error::Error::source`] without becoming part of this crate's public
9/// type signatures. Classify the enclosing [`crate::Error`] through
10/// [`j2k_core::CodecError`].
11#[derive(Debug, PartialEq, Eq)]
12pub struct NativeBackendError {
13    source: DecodeError,
14}
15
16impl NativeBackendError {
17    pub(crate) const fn decode(source: DecodeError) -> Self {
18        Self { source }
19    }
20
21    pub(crate) fn is_decode_truncated(&self) -> bool {
22        matches!(
23            self.source.classify(),
24            DecodeErrorClass::InputTooShort { .. } | DecodeErrorClass::InputTruncatedAt { .. }
25        )
26    }
27
28    pub(crate) fn is_unsupported(&self) -> bool {
29        matches!(self.source.classify(), DecodeErrorClass::Unsupported { .. })
30    }
31}
32
33impl core::fmt::Display for NativeBackendError {
34    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
35        self.source.fmt(f)
36    }
37}
38
39impl core::error::Error for NativeBackendError {
40    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
41        Some(&self.source)
42    }
43}