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
/*
This file is part of jpegxl-rs.

jpegxl-rs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

jpegxl-rs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with jpegxl-rs.  If not, see <https://www.gnu.org/licenses/>.
*/

//! Decoder and encoder errors

#[allow(clippy::wildcard_imports)]
use jpegxl_sys::*;
use thiserror::Error;

/// Errors derived from [`JxlDecoderStatus`]
#[derive(Error, Debug)]
pub enum DecodeError {
    /// Cannot create a decoder
    #[error("Cannot create a decoder")]
    CannotCreateDecoder,
    /// Unknown Error
    // TODO: underlying library is working on a way to retrieve error message
    #[error("Unknown decoder error")]
    GenericError(&'static str),
    /// Need more input bytes
    #[error("Doesn't need more input")]
    NeedMoreInput,
    /// Invalid file format
    #[error("The file does not contain a valid codestream or container")]
    InvalidFileFormat,
    /// Unknown status
    #[error("Unknown status: `{0:?}`")]
    UnknownStatus(JxlDecoderStatus),
}

/// Errors derived from [`JxlEncoderStatus`]
#[derive(Error, Debug)]
pub enum EncodeError {
    /// Cannot create a decoder
    #[error("Cannot create an encoder")]
    CannotCreateEncoder,
    /// Unknown Error
    // TODO: underlying library is working on a way to retrieve error message
    #[error("Unknown encoder error")]
    GenericError(&'static str),
    /// Not Supported
    #[error("Encoder does not support (yet)")]
    NotSupported,
    /// Unknown status
    #[error("Unknown status: `{0:?}`")]
    UnknownStatus(JxlEncoderStatus),
}

/// Error mapping from underlying C const to `JxlDecoderStatus` enum
pub(crate) fn check_dec_status(
    status: JxlDecoderStatus,
    msg: &'static str,
) -> Result<(), DecodeError> {
    match status {
        JxlDecoderStatus::Success => Ok(()),
        JxlDecoderStatus::Error => Err(DecodeError::GenericError(msg)),
        _ => Err(DecodeError::UnknownStatus(status)),
    }
}

/// Error mapping from underlying C const to `JxlEncoderStatus` enum
pub(crate) fn check_enc_status(
    status: JxlEncoderStatus,
    msg: &'static str,
) -> Result<(), EncodeError> {
    match status {
        JxlEncoderStatus::Success => Ok(()),
        JxlEncoderStatus::Error => Err(EncodeError::GenericError(msg)),
        JxlEncoderStatus::NotSupported => Err(EncodeError::NotSupported),
        JxlEncoderStatus::NeedMoreOutput => Err(EncodeError::UnknownStatus(status)),
    }
}