pic_scale/
pic_scale_error.rs1use std::error::Error;
30use std::fmt::Display;
31
32#[derive(Copy, Clone, Debug)]
33pub struct PicScaleBufferMismatch {
34 pub expected: usize,
35 pub width: usize,
36 pub height: usize,
37 pub channels: usize,
38 pub slice_len: usize,
39}
40
41#[derive(Debug)]
42pub enum PicScaleError {
43 ZeroImageDimensions,
44 SourceImageIsTooLarge,
45 DestinationImageIsTooLarge,
46 BufferMismatch(PicScaleBufferMismatch),
47 InvalidStride(usize, usize),
48 UnsupportedBitDepth(usize),
49 UnknownResizingFilter,
50}
51
52impl PicScaleError {
53 #[inline]
54 pub fn code(&self) -> usize {
55 match self {
56 PicScaleError::ZeroImageDimensions => 1,
57 PicScaleError::SourceImageIsTooLarge => 2,
58 PicScaleError::DestinationImageIsTooLarge => 3,
59 PicScaleError::BufferMismatch(_) => 4,
60 PicScaleError::InvalidStride(_, _) => 5,
61 PicScaleError::UnsupportedBitDepth(_) => 6,
62 PicScaleError::UnknownResizingFilter => 7,
63 }
64 }
65}
66
67impl Display for PicScaleError {
68 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69 match self {
70 PicScaleError::InvalidStride(min_stride, real_stride) => f.write_fmt(format_args!(
71 "Stride must be at least {}, but received {}",
72 min_stride, real_stride
73 )),
74 PicScaleError::ZeroImageDimensions => {
75 f.write_str("One of image dimensions is 0, this should not happen")
76 }
77 PicScaleError::SourceImageIsTooLarge => {
78 f.write_str("Input image larger than memory capabilities")
79 }
80 PicScaleError::DestinationImageIsTooLarge => {
81 f.write_str("Destination image larger than memory capabilities")
82 }
83 PicScaleError::BufferMismatch(buffer_mismatch) => f.write_fmt(format_args!(
84 "Image buffer len expected to be {} [w({})*h({})*channels({})] but received {}",
85 buffer_mismatch.expected,
86 buffer_mismatch.width,
87 buffer_mismatch.height,
88 buffer_mismatch.channels,
89 buffer_mismatch.slice_len,
90 )),
91 PicScaleError::UnsupportedBitDepth(depth) => f.write_fmt(format_args!(
92 "Bit depth must be in [1, 16] but got {}",
93 depth
94 )),
95 PicScaleError::UnknownResizingFilter => {
96 f.write_str("Unknown resizing filter was requested")
97 }
98 }
99 }
100}
101
102impl Error for PicScaleError {}