gift/
error.rs

1// error.rs
2//
3// Copyright (c) 2019-2023  Douglas Lau
4//
5use std::fmt;
6use std::io;
7use std::num::TryFromIntError;
8
9/// Errors encountered while decoding or encoding
10#[derive(Debug)]
11pub enum Error {
12    /// A wrapped I/O error.
13    Io(io::Error),
14    /// Integer out of bounds.
15    TryFromInt(TryFromIntError),
16    /// [Header](block/struct.Header.html) block malformed or missing.
17    MalformedHeader,
18    /// GIF version not supported (87a or 89a only).
19    UnsupportedVersion([u8; 3]),
20    /// Invalid [Block](block/enum.Block.html) code (signature).
21    InvalidBlockCode,
22    /// [Block](block/enum.Block.html)s arranged in invalid sequence.
23    InvalidBlockSequence,
24    /// [GraphicControl](block/struct.GraphicControl.html) block has invalid
25    /// length.
26    MalformedGraphicControlExtension,
27    /// File ends with incomplete block.
28    UnexpectedEndOfFile,
29    /// Compressed LZW data invalid or corrupt
30    InvalidLzwData,
31    /// Image larger than specified by
32    /// [max_image_sz](struct.Decoder.html#method.max_image_sz).
33    TooLargeImage,
34    /// [ImageData](block/struct.ImageData.html) block is incomplete.
35    IncompleteImageData,
36    /// Frame location / size larger than sreen size.
37    InvalidFrameDimensions,
38    /// Missing color table for a frame.
39    MissingColorTable,
40    /// Invalid color index in a frame.
41    InvalidColorIndex,
42    /// Invalid Raster dimensions
43    InvalidRasterDimensions,
44}
45
46/// Gift result type
47pub type Result<T> = std::result::Result<T, Error>;
48
49impl fmt::Display for Error {
50    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
51        match self {
52            Error::Io(err) => err.fmt(fmt),
53            Error::TryFromInt(err) => err.fmt(fmt),
54            _ => fmt::Debug::fmt(self, fmt),
55        }
56    }
57}
58
59impl std::error::Error for Error {
60    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
61        match *self {
62            Error::Io(ref err) => Some(err),
63            Error::TryFromInt(ref err) => Some(err),
64            _ => None,
65        }
66    }
67}
68
69impl From<io::Error> for Error {
70    fn from(err: io::Error) -> Self {
71        Error::Io(err)
72    }
73}
74
75impl From<TryFromIntError> for Error {
76    fn from(err: TryFromIntError) -> Self {
77        Error::TryFromInt(err)
78    }
79}