1use std::error::Error;
2use std::fmt;
3
4use thiserror::Error;
5
6#[non_exhaustive]
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum Unreal4ErrorKind {
10 Empty,
12
13 BadCompression,
15
16 BadData,
18
19 TrailingData,
21
22 TooLarge,
24
25 InvalidLogEntry,
27
28 InvalidXml,
30}
31
32impl fmt::Display for Unreal4ErrorKind {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 match self {
35 Self::Empty => write!(f, "empty crash"),
36 Self::BadCompression => write!(f, "bad compression"),
37 Self::BadData => write!(f, "invalid crash file contents"),
38 Self::TrailingData => write!(f, "unexpected trailing data"),
39 Self::TooLarge => write!(f, "crash file contents are too large"),
40 Self::InvalidLogEntry => write!(f, "invalid log entry"),
41 Self::InvalidXml => write!(f, "invalid xml"),
42 }
43 }
44}
45
46#[derive(Debug, Error)]
48#[error("{kind}")]
49pub struct Unreal4Error {
50 kind: Unreal4ErrorKind,
51 #[source]
52 source: Option<Box<dyn Error + Send + Sync + 'static>>,
53}
54
55impl Unreal4Error {
56 pub(crate) fn new<E>(kind: Unreal4ErrorKind, source: E) -> Self
59 where
60 E: Into<Box<dyn Error + Send + Sync>>,
61 {
62 let source = Some(source.into());
63 Self { kind, source }
64 }
65
66 pub fn kind(&self) -> Unreal4ErrorKind {
68 self.kind
69 }
70}
71
72impl From<Unreal4ErrorKind> for Unreal4Error {
73 fn from(kind: Unreal4ErrorKind) -> Self {
74 Self { kind, source: None }
75 }
76}
77
78impl From<elementtree::Error> for Unreal4Error {
79 fn from(source: elementtree::Error) -> Self {
80 Self::new(Unreal4ErrorKind::InvalidXml, source)
81 }
82}
83
84impl From<scroll::Error> for Unreal4Error {
85 fn from(source: scroll::Error) -> Self {
86 Self::new(Unreal4ErrorKind::BadData, source)
87 }
88}