Skip to main content

j2k_jpeg_cuda/
error.rs

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