rosetta_squint_decode/
error.rs1use thiserror::Error;
2use crate::types::Format;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum DecodeErrorKind {
7 UnsupportedFormat,
8 CorruptInput,
9 Truncated,
10 UnsupportedFeature,
11 ImageTooLarge,
12}
13
14impl DecodeErrorKind {
15 pub fn as_str(&self) -> &'static str {
16 match self {
17 DecodeErrorKind::UnsupportedFormat => "unsupportedFormat",
18 DecodeErrorKind::CorruptInput => "corruptInput",
19 DecodeErrorKind::Truncated => "truncated",
20 DecodeErrorKind::UnsupportedFeature => "unsupportedFeature",
21 DecodeErrorKind::ImageTooLarge => "imageTooLarge",
22 }
23 }
24}
25
26impl std::fmt::Display for DecodeErrorKind {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 f.write_str(self.as_str())
29 }
30}
31
32#[derive(Debug, Clone, Error, PartialEq, Eq)]
34pub struct DecodeError {
35 pub kind: DecodeErrorKind,
36 pub format: Option<Format>,
37 pub detail: String,
38}
39
40impl std::fmt::Display for DecodeError {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 match (&self.format, self.detail.is_empty()) {
43 (Some(fmt), false) => write!(f, "{}[{}]: {}", self.kind, fmt, self.detail),
44 (Some(fmt), true) => write!(f, "{}[{}]", self.kind, fmt),
45 (None, false) => write!(f, "{}: {}", self.kind, self.detail),
46 (None, true) => write!(f, "{}", self.kind),
47 }
48 }
49}
50
51impl DecodeError {
52 pub(crate) fn new(kind: DecodeErrorKind, format: Option<Format>, detail: impl Into<String>) -> Self {
53 DecodeError { kind, format, detail: detail.into() }
54 }
55}