Skip to content

Commit f3aae42

Browse files
committed
Add basic error reporting
1 parent 1f110f3 commit f3aae42

File tree

1 file changed

+8
-34
lines changed

1 file changed

+8
-34
lines changed

src/vanilla.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ impl VanillaExtractor {
114114
return Ok(());
115115
}
116116

117-
let result = std::fs::create_dir_all(path);
118-
if result.is_err() {
119-
return Err(ParseError(format!("Failed to create directory structure: {}", result.unwrap_err())));
120-
}
117+
std::fs::create_dir_all(path).or_else(|e| Err(ParseError(format!("Failed to create directory structure: {}", e))))?;
121118

122119
Ok(())
123120
}
@@ -134,9 +131,7 @@ impl VanillaExtractor {
134131
org_path.push(self.data_base_dir.clone());
135132
org_path.push("Org/");
136133

137-
if self.deep_create_dir_if_not_exists(org_path.clone()).is_err() {
138-
return Err(ParseError("Failed to create directory structure.".to_string()));
139-
}
134+
self.deep_create_dir_if_not_exists(org_path.clone())?;
140135

141136
org_path.push(format!("{}.org", org.name));
142137

@@ -169,29 +164,19 @@ impl VanillaExtractor {
169164
let mut data_path = self.root.clone();
170165
data_path.push(self.data_base_dir.clone());
171166

172-
if self.deep_create_dir_if_not_exists(data_path.clone()).is_err() {
173-
return Err(ParseError("Failed to create data directory structure.".to_string()));
174-
}
167+
self.deep_create_dir_if_not_exists(data_path.clone())?;
175168

176169
data_path.push(format!("{}.pbm", bitmap.name));
177170

178-
let file = std::fs::File::create(data_path);
179-
if file.is_err() {
180-
return Err(ParseError("Failed to create bitmap file.".to_string()));
181-
}
182-
183-
let mut file = file.unwrap();
171+
let mut file = std::fs::File::create(data_path).or_else(|e| Err(ParseError(format!("Failed to create bitmap file: {}.", e))))?;
184172

185173
file.write_u8(0x42)?; // B
186174
file.write_u8(0x4D)?; // M
187175
file.write_u32::<LE>(bitmap.bytes.len() as u32 + 0xE)?; // Size of BMP file
188176
file.write_u32::<LE>(0)?; // unused null bytes
189177
file.write_u32::<LE>(0x76)?; // Bitmap data offset (hardcoded for now, might wanna get the actual offset)
190178

191-
let result = file.write_all(&bitmap.bytes);
192-
if result.is_err() {
193-
return Err(ParseError("Failed to write bitmap file.".to_string()));
194-
}
179+
file.write_all(&bitmap.bytes).or_else(|e| Err(ParseError(format!("Failed to write bitmap file: {}.", e))))?;
195180

196181
println!("Extracted bitmap file: {}", bitmap.name);
197182
}
@@ -253,24 +238,13 @@ impl VanillaExtractor {
253238
let mut stage_tbl_path = self.root.clone();
254239
stage_tbl_path.push(self.data_base_dir.clone());
255240

256-
if self.deep_create_dir_if_not_exists(stage_tbl_path.clone()).is_err() {
257-
return Err(ParseError("Failed to create data directory structure.".to_string()));
258-
}
241+
self.deep_create_dir_if_not_exists(stage_tbl_path.clone())?;
259242

260243
stage_tbl_path.push("stage.sect");
261244

262-
let mut stage_tbl_file = match std::fs::File::create(stage_tbl_path) {
263-
Ok(file) => file,
264-
Err(_) => {
265-
return Err(ParseError("Failed to create stage table file.".to_string()));
266-
}
267-
};
268-
269-
let result = stage_tbl_file.write_all(byte_slice);
270-
if result.is_err() {
271-
return Err(ParseError("Failed to write to stage table file.".to_string()));
272-
}
245+
let mut stage_tbl_file = std::fs::File::create(stage_tbl_path).or_else(|e| Err(ParseError(format!("Failed to create stage table file: {}.", e))))?;
273246

247+
stage_tbl_file.write_all(byte_slice).or_else(|e| Err(ParseError(format!("Failed to write to stage table file: {}.", e))))?;
274248
Ok(())
275249
}
276250
}

0 commit comments

Comments
 (0)