zune_bmp/
errors.rs

1/*
2 * Copyright (c) 2023.
3 *
4 * This software is free software;
5 *
6 * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
7 */
8
9use alloc::string::String;
10use core::fmt::{Debug, Formatter};
11
12use zune_core::bytestream::ZByteIoError;
13
14/// BMP errors that can occur during decoding
15#[non_exhaustive]
16pub enum BmpDecoderErrors {
17    /// The file/bytes do not start with `BM`
18    InvalidMagicBytes,
19    /// The output buffer is too small, expected at least
20    /// a size but got another size
21    TooSmallBuffer(usize, usize),
22    /// Generic message
23    GenericStatic(&'static str),
24    /// Generic allocated message
25    Generic(String),
26    /// Too large dimensions for a given width or
27    /// height
28    TooLargeDimensions(&'static str, usize, usize),
29    /// A calculation overflowed
30    OverFlowOccurred,
31    IoErrors(ZByteIoError)
32}
33
34impl Debug for BmpDecoderErrors {
35    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
36        match self {
37            Self::InvalidMagicBytes => {
38                writeln!(f, "Invalid magic bytes, file does not start with BM")
39            }
40            Self::TooSmallBuffer(expected, found) => {
41                writeln!(
42                    f,
43                    "Too small of buffer, expected {} but found {}",
44                    expected, found
45                )
46            }
47            Self::GenericStatic(header) => {
48                writeln!(f, "{}", header)
49            }
50            Self::TooLargeDimensions(dimension, expected, found) => {
51                writeln!(
52                    f,
53                    "Too large dimensions for {dimension} , {found} exceeds {expected}"
54                )
55            }
56            Self::Generic(message) => {
57                writeln!(f, "{}", message)
58            }
59            Self::OverFlowOccurred => {
60                writeln!(f, "Overflow occurred")
61            }
62            Self::IoErrors(err) => {
63                writeln!(f, "{:?}", err)
64            }
65        }
66    }
67}
68
69impl From<ZByteIoError> for BmpDecoderErrors {
70    fn from(value: ZByteIoError) -> Self {
71        BmpDecoderErrors::IoErrors(value)
72    }
73}