irox_imagery/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors
//

use irox_tools::impl_from_error;
use std::fmt::{Display, Formatter};

#[derive(Debug, Clone)]
pub enum ImageErrorType {
    BitsError,
    BadMagic,
    BadByteOrder,
    ParseError,
}
impl<T> From<ImageErrorType> for Result<T, ImageError> {
    fn from(ty: ImageErrorType) -> Self {
        Err(ImageError {
            msg: match ty {
                ImageErrorType::BadMagic => "Bad Magic Value".to_string(),
                ImageErrorType::BadByteOrder => "Bad Byte Order Value".to_string(),
                ImageErrorType::BitsError => "Bits Error".to_string(),
                ImageErrorType::ParseError => "Parse Error".to_string(),
            },
            error_type: ty,
        })
    }
}
#[derive(Debug, Clone)]
pub struct ImageError {
    msg: String,
    error_type: ImageErrorType,
}

impl Display for ImageError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "ImageError({:?}): {}", self.error_type, self.msg)
    }
}

impl core::error::Error for ImageError {}

impl ImageError {
    pub fn bad_magic() -> ImageError {
        ImageError {
            error_type: ImageErrorType::BadMagic,
            msg: "Bad magic number".to_string(),
        }
    }
    pub fn bad_type(ty: u16) -> ImageError {
        ImageError {
            error_type: ImageErrorType::ParseError,
            msg: format!("Bad Type: {ty}"),
        }
    }
    pub fn not_enough_values() -> ImageError {
        ImageError {
            error_type: ImageErrorType::ParseError,
            msg: "Not enough values".to_string(),
        }
    }
}

impl_from_error!(ImageError, irox_bits::BitsError, ImageErrorType::BitsError);