1use std::{
4 fmt::{self, Display},
5 io, num,
6};
7
8#[non_exhaustive]
10#[derive(Debug)]
11pub enum MoshError {
12 DecodingError(png::DecodingError),
14 EncodingError(png::EncodingError),
16 InvalidPalette,
18 IoError(io::Error),
20 ConversionError(num::TryFromIntError),
22 RngError(rand::distr::uniform::Error),
24 OutOfMemory,
26}
27
28impl std::error::Error for MoshError {}
29
30impl Display for MoshError {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Self::DecodingError(e) => Display::fmt(e, f),
34 Self::EncodingError(e) => Display::fmt(e, f),
35 Self::InvalidPalette => f.write_str("Invalid image palette"),
36 Self::IoError(e) => Display::fmt(e, f),
37 Self::ConversionError(e) => Display::fmt(e, f),
38 Self::RngError(e) => Display::fmt(e, f),
39 Self::OutOfMemory => f.write_str("Out of memory"),
40 }
41 }
42}
43
44impl From<io::Error> for MoshError {
45 fn from(e: io::Error) -> Self {
46 Self::IoError(e)
47 }
48}
49
50impl From<num::TryFromIntError> for MoshError {
51 fn from(e: num::TryFromIntError) -> Self {
52 Self::ConversionError(e)
53 }
54}
55
56impl From<png::DecodingError> for MoshError {
57 fn from(e: png::DecodingError) -> Self {
58 Self::DecodingError(e)
59 }
60}
61
62impl From<png::EncodingError> for MoshError {
63 fn from(e: png::EncodingError) -> Self {
64 Self::EncodingError(e)
65 }
66}
67
68impl From<rand::distr::uniform::Error> for MoshError {
69 fn from(e: rand::distr::uniform::Error) -> Self {
70 Self::RngError(e)
71 }
72}