zune_psd/
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 core::fmt::{Debug, Formatter};
10
11use crate::constants::{ColorModes, PSD_IDENTIFIER_BE};
12
13/// PSDDecodeErrors that can occur during PSD decoding
14pub enum PSDDecodeErrors {
15    WrongMagicBytes(u32),
16    UnsupportedFileType(u16),
17    UnsupportedChannelCount(u16),
18    UnsupportedBitDepth(u16),
19    UnsupportedColorFormat(Option<ColorModes>),
20    LargeDimensions(usize, usize),
21    ZeroDimensions,
22    UnknownCompression,
23    Generic(&'static str),
24    BadRLE
25}
26
27impl Debug for PSDDecodeErrors {
28    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
29        match self {
30            PSDDecodeErrors::Generic(reason) => {
31                writeln!(f, "{reason}")
32            }
33            PSDDecodeErrors::WrongMagicBytes(bytes) => {
34                writeln!(
35                    f,
36                    "Expected {:?} but found  {:?}, not a PSD image",
37                    PSD_IDENTIFIER_BE.to_be_bytes(),
38                    bytes.to_be_bytes()
39                )
40            }
41            PSDDecodeErrors::UnsupportedFileType(version) => {
42                writeln!(
43                    f,
44                    "Unsupported file version {version:?}, known versions are 1",
45                )
46            }
47            PSDDecodeErrors::UnsupportedChannelCount(channels) => {
48                writeln!(f, "Unsupported channel count {channels:?}")
49            }
50            PSDDecodeErrors::UnsupportedBitDepth(depth) => {
51                writeln!(
52                    f,
53                    "Unsupported bit depth {depth:?}, supported depths are 8 and 16",
54                )
55            }
56            PSDDecodeErrors::UnsupportedColorFormat(color) => {
57                if let Some(color) = color {
58                    writeln!(
59                        f,
60                        "Unsupported color format  {color:?}, supported formats RGB,CMYK and Grayscale currently",
61                    )
62                } else {
63                    writeln!(f, "Unknown color format")
64                }
65            }
66            PSDDecodeErrors::UnknownCompression => {
67                writeln!(f, "Unknown compression format")
68            }
69            PSDDecodeErrors::BadRLE => {
70                writeln!(f, "Bad RLE")
71            }
72            PSDDecodeErrors::LargeDimensions(supported, found) => {
73                writeln!(
74                    f,
75                    "Too large dimensions, supported {supported} but found {found}",
76                )
77            }
78            PSDDecodeErrors::ZeroDimensions => {
79                writeln!(f, "Zero found where not expected")
80            }
81        }
82    }
83}
84
85impl From<&'static str> for PSDDecodeErrors {
86    fn from(r: &'static str) -> Self {
87        Self::Generic(r)
88    }
89}