pineapple_core/
error.rs

1// Copyright (c) 2025, Tom Ouellette
2// Licensed under the BSD 3-Clause License
3
4use std::fmt;
5
6#[derive(Debug, Clone)]
7pub enum PineappleError {
8    BoundingBoxError,
9    BufferSizeError,
10    ChannelBoundsError,
11    ConversionError,
12    ImageError(&'static str),
13    ImageReadError,
14    ImageWriteError,
15    ImageFormatError,
16    ImageExtensionError,
17    MaskError(&'static str),
18    MaskFormatError,
19    PolygonsSizeError,
20    PolygonsReadError,
21    PolygonsWriteError,
22    BoxesSizeError,
23    BoxesReadError,
24    BoxesWriteError,
25    NoFileError(String),
26    DirError(String),
27    OtherError(String),
28}
29
30impl fmt::Display for PineappleError {
31    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32        match self {
33            PineappleError::BoundingBoxError => {
34                write!(
35                    f,
36                    "[pineapple::BoundingBoxError] The bounding box is invalid as max_x (max_y) must be greater than min_x (min_y)."
37                )
38            }
39            PineappleError::BufferSizeError => {
40                write!(
41                    f,
42                    "[pineapple::BufferSizeError] The buffer does not match provided size"
43                )
44            }
45            PineappleError::ChannelBoundsError => {
46                write!(
47                    f,
48                    "[pineapple::ChannelBoundsError] The indexed channel is out of bounds."
49                )
50            }
51            PineappleError::ConversionError => {
52                write!(
53                    f,
54                    "[pineapple::ConversionError] Failed to convert value to f32."
55                )
56            }
57            PineappleError::ImageError(message) => {
58                write!(f, "[pineapple::ImageError] Failed to create image. {}", message)
59            }
60            PineappleError::ImageReadError => {
61                write!(f, "[pineapple::ImageReadError] Failed to read image.",)
62            }
63            PineappleError::ImageWriteError => {
64                write!(f, "[pineapple::ImageWriteError] Failed to write image.",)
65            }
66            PineappleError::ImageFormatError => {
67                write!(
68                    f,
69                    "[pineapple::ImageFormatError] Only 1 and 3-channel u8 and u16 images are currently supported."
70                )
71            }
72            PineappleError::ImageExtensionError => {
73                write!(
74                    f,
75                    "[pineapple::ImageExtensionError] Could not detect a valid image extension for input."
76                )
77            }
78            PineappleError::MaskError(message) => {
79                write!(f, "[pineapple::MaskError] Failed to create mask. {}", message)
80            }
81            PineappleError::MaskFormatError => {
82                write!(
83                    f,
84                    "[pineapple::MaskFormatError] Only 1-channel u8 and u16 masks are currently supported."
85                )
86            }
87            PineappleError::PolygonsSizeError => {
88                write!(
89                    f,
90                    "[pineapple::PolygonsSizeError] No polygons with length > 3 were detected in input.",
91                )
92            }
93            PineappleError::PolygonsReadError => {
94                write!(f, "[pineapple::PolygonsReadError] Polygons could not be read.")
95            }
96            PineappleError::PolygonsWriteError => {
97                write!(
98                    f,
99                    "[pineapple::PolygonsWriteError] Failed to successfully write polygons to output."
100                )
101            }
102            PineappleError::BoxesSizeError => {
103                write!(
104                    f,
105                    "[pineapple::BoxesSizeError] Bounding box must satisfy x_min < x_max and y_min < y_max.",
106                )
107            }
108            PineappleError::BoxesReadError => {
109                write!(
110                    f,
111                    "[pineapple::BoxesReadError] Bounding boxes could not be read."
112                )
113            }
114            PineappleError::BoxesWriteError => {
115                write!(
116                    f,
117                    "[pineapple::BoxesWriteError] Failed to successfully write boundng boxes to output."
118                )
119            }
120            PineappleError::NoFileError(message) => {
121                write!(
122                    f,
123                    "[pineapple::NoFileError] File could not be found. {}.",
124                    message
125                )
126            }
127            PineappleError::DirError(message) => {
128                write!(
129                    f,
130                    "[pineapple::DirError] Directory could not be read. {}.",
131                    message
132                )
133            }
134            PineappleError::OtherError(message) => {
135                write!(f, "[pineapple::OtherError] Error: {}.", message)
136            }
137        }
138    }
139}
140
141impl std::error::Error for PineappleError {}