Skip to main content

j2k_cuda/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use j2k::J2kError;
4use j2k_core::{BackendRequest, BufferError, CodecError};
5
6/// Error returned by the CUDA JPEG 2000 adapter.
7#[derive(Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum Error {
10    /// CPU JPEG 2000 decode failed.
11    #[error(transparent)]
12    Decode(#[from] J2kError),
13    /// Caller-owned output buffers were invalid.
14    #[error(transparent)]
15    Buffer(#[from] BufferError),
16    /// Backend request is unsupported by this adapter.
17    #[error("backend request {request:?} is not supported by j2k-cuda")]
18    UnsupportedBackend {
19        /// Requested backend.
20        request: BackendRequest,
21    },
22    /// CUDA request is unsupported by the strict CUDA adapter contract.
23    #[error("unsupported CUDA request: {reason}")]
24    UnsupportedCudaRequest {
25        /// Human-readable rejection reason.
26        reason: &'static str,
27    },
28    /// CUDA runtime or device is unavailable.
29    #[error("CUDA is unavailable on this host")]
30    CudaUnavailable,
31    #[cfg(feature = "cuda-runtime")]
32    /// CUDA runtime returned an error.
33    #[error("CUDA runtime error: {message}")]
34    CudaRuntime {
35        /// Runtime error message.
36        message: String,
37    },
38}
39
40impl CodecError for Error {
41    fn is_truncated(&self) -> bool {
42        matches!(self, Self::Decode(inner) if inner.is_truncated())
43    }
44
45    fn is_not_implemented(&self) -> bool {
46        matches!(self, Self::Decode(inner) if inner.is_not_implemented())
47    }
48
49    fn is_unsupported(&self) -> bool {
50        matches!(
51            self,
52            Self::UnsupportedBackend { .. }
53                | Self::UnsupportedCudaRequest { .. }
54                | Self::CudaUnavailable
55        ) || matches!(self, Self::Decode(inner) if inner.is_unsupported())
56    }
57
58    fn is_buffer_error(&self) -> bool {
59        matches!(self, Self::Buffer(_))
60            || matches!(self, Self::Decode(inner) if inner.is_buffer_error())
61    }
62}