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
12/// BMP errors that can occur during decoding
13pub enum BmpDecoderErrors {
14    /// The file/bytes do not start with `BM`
15    InvalidMagicBytes,
16    /// The output buffer is too small, expected at least
17    /// a size but got another size
18    TooSmallBuffer(usize, usize),
19    /// Generic message
20    GenericStatic(&'static str),
21    /// Generic allocated message
22    Generic(String),
23    /// Too large dimensions for a given width or
24    /// height
25    TooLargeDimensions(&'static str, usize, usize),
26    /// A calculation overflowed
27    OverFlowOccurred
28}
29
30impl Debug for BmpDecoderErrors {
31    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
32        match self {
33            Self::InvalidMagicBytes => {
34                writeln!(f, "Invalid magic bytes, file does not start with BM")
35            }
36            Self::TooSmallBuffer(expected, found) => {
37                writeln!(
38                    f,
39                    "Too small of buffer, expected {} but found {}",
40                    expected, found
41                )
42            }
43            Self::GenericStatic(header) => {
44                writeln!(f, "{}", header)
45            }
46            Self::TooLargeDimensions(dimension, expected, found) => {
47                writeln!(
48                    f,
49                    "Too large dimensions for {dimension} , {found} exceeds {expected}"
50                )
51            }
52            Self::Generic(message) => {
53                writeln!(f, "{}", message)
54            }
55            Self::OverFlowOccurred => {
56                writeln!(f, "Overflow occurred")
57            }
58        }
59    }
60}