goud_engine/assets/loader/
error.rs1use std::error::Error;
4use std::fmt;
5use std::path::Path;
6
7#[derive(Debug, Clone)]
9pub enum AssetLoadError {
10 NotFound {
12 path: String,
14 },
15
16 IoError {
18 path: String,
20 message: String,
22 },
23
24 DecodeFailed(
26 String,
28 ),
29
30 UnsupportedFormat {
32 extension: String,
34 },
35
36 DependencyFailed {
38 asset_path: String,
40 dependency_path: String,
42 message: String,
44 },
45
46 Custom(
48 String,
50 ),
51}
52
53impl AssetLoadError {
54 pub fn not_found(path: impl AsRef<Path>) -> Self {
56 Self::NotFound {
57 path: path.as_ref().display().to_string(),
58 }
59 }
60
61 pub fn io_error(path: impl AsRef<Path>, error: impl Error) -> Self {
63 Self::IoError {
64 path: path.as_ref().display().to_string(),
65 message: error.to_string(),
66 }
67 }
68
69 pub fn decode_failed(message: impl Into<String>) -> Self {
71 Self::DecodeFailed(message.into())
72 }
73
74 pub fn unsupported_format(extension: impl Into<String>) -> Self {
76 Self::UnsupportedFormat {
77 extension: extension.into(),
78 }
79 }
80
81 pub fn dependency_failed(
83 asset_path: impl Into<String>,
84 dependency_path: impl Into<String>,
85 message: impl Into<String>,
86 ) -> Self {
87 Self::DependencyFailed {
88 asset_path: asset_path.into(),
89 dependency_path: dependency_path.into(),
90 message: message.into(),
91 }
92 }
93
94 pub fn custom(message: impl Into<String>) -> Self {
96 Self::Custom(message.into())
97 }
98
99 pub fn is_not_found(&self) -> bool {
101 matches!(self, Self::NotFound { .. })
102 }
103
104 pub fn is_io_error(&self) -> bool {
106 matches!(self, Self::IoError { .. })
107 }
108
109 pub fn is_decode_failed(&self) -> bool {
111 matches!(self, Self::DecodeFailed(_))
112 }
113
114 pub fn is_unsupported_format(&self) -> bool {
116 matches!(self, Self::UnsupportedFormat { .. })
117 }
118
119 pub fn is_dependency_failed(&self) -> bool {
121 matches!(self, Self::DependencyFailed { .. })
122 }
123
124 pub fn is_custom(&self) -> bool {
126 matches!(self, Self::Custom(_))
127 }
128}
129
130impl fmt::Display for AssetLoadError {
131 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132 match self {
133 Self::NotFound { path } => write!(f, "Asset not found: {}", path),
134 Self::IoError { path, message } => {
135 write!(f, "I/O error loading asset '{}': {}", path, message)
136 }
137 Self::DecodeFailed(msg) => write!(f, "Failed to decode asset: {}", msg),
138 Self::UnsupportedFormat { extension } => {
139 write!(f, "Unsupported asset format: '.{}'", extension)
140 }
141 Self::DependencyFailed {
142 asset_path,
143 dependency_path,
144 message,
145 } => write!(
146 f,
147 "Dependency '{}' of asset '{}' failed to load: {}",
148 dependency_path, asset_path, message
149 ),
150 Self::Custom(msg) => write!(f, "Asset loading error: {}", msg),
151 }
152 }
153}
154
155impl Error for AssetLoadError {}