Skip to content

Commit 8fd3e10

Browse files
committedMar 14, 2025
rawfile: Now the API is always faillible
This avoid many cases of panic and return errors instead io now return Result instead of io::Result Signed-off-by: Hubert Figuière <hub@figuiere.net>
1 parent 2b9aaa8 commit 8fd3e10

26 files changed

+679
-580
lines changed
 

‎benches/the_benchmark.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* libopenraw - the_benchmark.rs
44
*
5-
* Copyright (C) 2023 Hubert Figuière
5+
* Copyright (C) 2023-2025 Hubert Figuière
66
*
77
* This library is free software: you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public License
@@ -53,9 +53,10 @@ pub fn ordiag_benchmark(c: &mut Criterion) {
5353
b.iter(|| {
5454
let rawfile = rawfile_from_file(&file, None);
5555
if let Ok(rawfile) = rawfile {
56-
let sizes = rawfile.thumbnail_sizes();
57-
for size in sizes {
58-
let _ = rawfile.thumbnail(*size);
56+
if let Some(sizes) = rawfile.thumbnail_sizes() {
57+
for size in sizes {
58+
let _ = rawfile.thumbnail(*size);
59+
}
5960
}
6061
let _ = rawfile.raw_data(false);
6162
}

‎libopenraw-testing/src/lib.rs

+45-42
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* libopenraw - libopenraw-testing.rs
44
*
5-
* Copyright (C) 2022-2024 Hubert Figuière
5+
* Copyright (C) 2022-2025 Hubert Figuière
66
*
77
* This library is free software: you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public License
@@ -95,17 +95,9 @@ pub struct Results {
9595
default
9696
)]
9797
pub raw_data_active_area: Option<Vec<u32>>,
98-
#[serde(
99-
deserialize_with = "from_list",
100-
serialize_with = "to_list",
101-
default
102-
)]
98+
#[serde(deserialize_with = "from_list", serialize_with = "to_list", default)]
10399
pub raw_data_user_crop: Option<Vec<u32>>,
104-
#[serde(
105-
deserialize_with = "from_list",
106-
serialize_with = "to_list",
107-
default
108-
)]
100+
#[serde(deserialize_with = "from_list", serialize_with = "to_list", default)]
109101
#[serde(skip_serializing_if = "Option::is_none")]
110102
pub raw_data_user_aspect_ratio: Option<Vec<u32>>,
111103
#[serde(skip_serializing_if = "Option::is_none")]
@@ -202,7 +194,7 @@ impl Test {
202194

203195
pub fn make_results(rawfile: &dyn RawFile) -> Results {
204196
let raw_type = Some(rawfile.type_().into());
205-
let raw_type_id = Some(rawfile.type_id().into());
197+
let raw_type_id = rawfile.type_id().map(|id| id.into()).ok();
206198
let exif_make = rawfile
207199
.metadata_value("Exif.Image.Make")
208200
.as_ref()
@@ -222,37 +214,46 @@ pub fn make_results(rawfile: &dyn RawFile) -> Results {
222214
.map(|s| s.to_string_lossy().to_string());
223215

224216
let thumbnail_sizes = rawfile.thumbnail_sizes();
225-
let thumb_num = Some(thumbnail_sizes.len() as u32);
226-
let thumb_sizes = Some(thumbnail_sizes.to_vec());
217+
let thumb_num = thumbnail_sizes.map(|sizes| sizes.len() as u32);
218+
let thumb_sizes = thumbnail_sizes.map(|sizes| sizes.to_vec());
227219

228220
let thumbnails = rawfile.thumbnails();
229-
let thumb_formats = Some(
230-
thumbnails
231-
.thumbnails
232-
.iter()
233-
.map(|t| t.1.data_type.into())
234-
.collect::<Vec<String>>()
235-
.join(" "),
236-
);
237-
let thumb_data_sizes = Some(
238-
thumbnails
239-
.thumbnails
240-
.iter()
241-
.map(|t| t.1.data_size() as u32)
242-
.collect(),
243-
);
244-
let thumb_md5 = Some(
245-
thumbnails
246-
.thumbnails
247-
.iter()
248-
.flat_map(|t| {
249-
rawfile
250-
.thumbnail(t.0)
251-
.ok()
252-
.and_then(|t| t.data8().map(raw_checksum))
253-
})
254-
.collect(),
255-
);
221+
let thumb_formats = thumbnails
222+
.as_ref()
223+
.map(|thumbnails| {
224+
thumbnails
225+
.thumbnails
226+
.iter()
227+
.map(|t| t.1.data_type.into())
228+
.collect::<Vec<String>>()
229+
.join(" ")
230+
})
231+
.ok();
232+
let thumb_data_sizes = thumbnails
233+
.as_ref()
234+
.map(|thumbnails| {
235+
thumbnails
236+
.thumbnails
237+
.iter()
238+
.map(|t| t.1.data_size() as u32)
239+
.collect()
240+
})
241+
.ok();
242+
let thumb_md5 = thumbnails
243+
.as_ref()
244+
.map(|thumbnails| {
245+
thumbnails
246+
.thumbnails
247+
.iter()
248+
.flat_map(|t| {
249+
rawfile
250+
.thumbnail(t.0)
251+
.ok()
252+
.and_then(|t| t.data8().map(raw_checksum))
253+
})
254+
.collect()
255+
})
256+
.ok();
256257

257258
let rawdata = rawfile.raw_data(false);
258259
let rawdata = rawdata.as_ref();
@@ -278,7 +279,9 @@ pub fn make_results(rawfile: &dyn RawFile) -> Results {
278279
.ok();
279280
let raw_min_value = rawdata.map(|rawdata| rawdata.blacks().to_vec()).ok();
280281
let raw_max_value = rawdata.map(|rawdata| rawdata.whites().to_vec()).ok();
281-
let raw_as_shot_neutral = rawdata.ok().and_then(|rawdata| rawdata.as_shot_neutral())
282+
let raw_as_shot_neutral = rawdata
283+
.ok()
284+
.and_then(|rawdata| rawdata.as_shot_neutral())
282285
.map(|as_shot| as_shot.to_vec());
283286
let raw_md5 = rawdata
284287
.ok()

‎src/bin/identify.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* libopenraw - bin/identify.rs
44
*
5-
* Copyright (C) 2022-2023 Hubert Figuière
5+
* Copyright (C) 2022-2025 Hubert Figuière
66
*
77
* This library is free software: you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public License
@@ -63,11 +63,12 @@ pub fn main() {
6363
}
6464
let rawfile = rawfile_from_file(&p, format);
6565
match rawfile {
66-
Ok(ref rawfile) => {
67-
println!("{} {}", name, rawfile.type_id());
68-
}
66+
Ok(ref rawfile) => match rawfile.type_id() {
67+
Ok(type_id) => println!("{} {}", name, type_id),
68+
Err(err) => println!("Error getting type id {name}: {err}"),
69+
},
6970
Err(err) => {
70-
println!("Error {name}: {err}");
71+
println!("Error opening file {name}: {err}");
7172
}
7273
}
7374
}

‎src/bin/metadata.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* libopenraw - bin/metadata.rs
44
*
5-
* Copyright (C) 2023-2024 Hubert Figuière
5+
* Copyright (C) 2023-2025 Hubert Figuière
66
*
77
* This library is free software: you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public License
@@ -64,13 +64,15 @@ fn process_file(p: &str) {
6464
if let Ok(rawfile) = rawfile_from_file(p, None) {
6565
log::info!("Metadata raw file {}", p);
6666

67-
for metadata in rawfile.metadata() {
68-
println!(
69-
"{} ({}) => {:?}",
70-
metadata.0,
71-
type_to_string(metadata.2),
72-
metadata.1.into_string(false)
73-
);
67+
if let Some(iterator) = rawfile.metadata() {
68+
for metadata in iterator {
69+
println!(
70+
"{} ({}) => {:?}",
71+
metadata.0,
72+
type_to_string(metadata.2),
73+
metadata.1.into_string(false)
74+
);
75+
}
7476
}
7577
}
7678
}

‎src/bin/ordiag.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* libopenraw - bin/ordiag.rs
44
*
5-
* Copyright (C) 2022-2024 Hubert Figuière
5+
* Copyright (C) 2022-2025 Hubert Figuière
66
*
77
* This library is free software: you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public License
@@ -274,8 +274,13 @@ fn process_file(
274274

275275
println!("Raw type: {:?}", rawfile.type_());
276276
println!("MIME type: {}", rawfile.mime_type());
277-
println!("Vendor id: {}", rawfile.vendor_id());
278-
println!("Type id: {:?}", rawfile.type_id());
277+
if let Ok(type_id) = rawfile.type_id() {
278+
println!("Vendor id: {}", rawfile.vendor_id().unwrap());
279+
println!("Type id: {:?}", type_id);
280+
} else {
281+
println!("Couldn't guess file type ID");
282+
return;
283+
}
279284
if let Some(make) = rawfile
280285
.metadata_value("Exif.Image.Make")
281286
.as_ref()
@@ -298,25 +303,26 @@ fn process_file(
298303
println!("Unique Camera Model: {:?}", unique);
299304
}
300305

301-
let sizes = rawfile.thumbnail_sizes();
302-
println!("Thumbnail sizes: {:?}", &sizes);
303-
for size in sizes {
304-
let thumb = rawfile.thumbnail(*size);
305-
match thumb {
306-
Ok(ref thumb) => {
307-
println!("\tThumbnail size: {} x {}", thumb.width(), thumb.height());
308-
println!("\tFormat: {:?}", thumb.data_type());
309-
println!(
310-
"\tSize: {} bytes",
311-
thumb.data8().map(|d| d.len()).unwrap_or(0)
312-
);
306+
if let Some(sizes) = rawfile.thumbnail_sizes() {
307+
println!("Thumbnail sizes: {:?}", &sizes);
308+
for size in sizes {
309+
let thumb = rawfile.thumbnail(*size);
310+
match thumb {
311+
Ok(ref thumb) => {
312+
println!("\tThumbnail size: {} x {}", thumb.width(), thumb.height());
313+
println!("\tFormat: {:?}", thumb.data_type());
314+
println!(
315+
"\tSize: {} bytes",
316+
thumb.data8().map(|d| d.len()).unwrap_or(0)
317+
);
313318

314-
if extract_thumbnails {
315-
save_thumbnail(p, thumb);
319+
if extract_thumbnails {
320+
save_thumbnail(p, thumb);
321+
}
322+
}
323+
Err(err) => {
324+
eprintln!("Failed to fetch preview for {size}: {err}");
316325
}
317-
}
318-
Err(err) => {
319-
eprintln!("Failed to fetch preview for {size}: {err}");
320326
}
321327
}
322328
}

‎src/bin/probe.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// SPDX-License-Identifier: LGPL-3.0-or-later
22
/*
3-
* libopenraw - bin/ordiag.rs
3+
* libopenraw - bin/probe.rs
44
*
5-
* Copyright (C) 2024 Hubert Figuière
5+
* Copyright (C) 2024-2025 Hubert Figuière
66
*
77
* This library is free software: you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public License
@@ -62,10 +62,11 @@ fn process_file(p: &str) {
6262
use std::rc::Rc;
6363
Rc::get_mut(rawfile).unwrap().set_probe(true);
6464

65-
let sizes = rawfile.thumbnail_sizes();
66-
for size in sizes {
67-
// is this needed for the probe?
68-
let _ = rawfile.thumbnail(*size);
65+
if let Some(sizes) = rawfile.thumbnail_sizes() {
66+
for size in sizes {
67+
// is this needed for the probe?
68+
let _ = rawfile.thumbnail(*size);
69+
}
6970
}
7071

7172
let _rawdata = rawfile.raw_data(false);

‎src/bin/test-rawfile.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ fn main() {
55
let _ = libopenraw::rawfile_from_file(&args[1], None)
66
.and_then(|rawfile| {
77
let _ = rawfile.type_id();
8-
let sizes = rawfile.thumbnail_sizes();
9-
for size in sizes {
10-
let _ = rawfile.thumbnail(*size)?;
8+
if let Some(sizes) = rawfile.thumbnail_sizes() {
9+
for size in sizes {
10+
let _ = rawfile.thumbnail(*size)?;
11+
}
1112
}
1213

1314
let _ = rawfile.raw_data(false)?;

0 commit comments

Comments
 (0)
Please sign in to comment.