Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c029893

Browse files
committed
rust: replace io.Error with custom Error type based on thiserror
1 parent 44c2122 commit c029893

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

wrappers/rust/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ image = ["dep:image"]
2020
bundled = []
2121

2222
[dependencies]
23-
paste = "1.0.14"
23+
thiserror = "1.0"
24+
paste = "1.0"
2425
flagset = "0.4.4"
2526
image = {version = "0.24.8", optional = true}
2627

2728
[dev-dependencies]
28-
cfg-if = "1.0.0"
29-
anyhow = "1.0.79"
29+
cfg-if = "1.0"
30+
anyhow = "1.0"
3031
image = {version = "0.24.8"}
3132

3233
[build-dependencies]

wrappers/rust/src/lib.rs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,40 @@ use bindings::*;
2121

2222
use flagset::{flags, FlagSet};
2323
use paste::paste;
24-
use std::ffi::{c_char, c_int, c_uint, c_void, CStr, CString};
24+
use std::ffi::{c_char, c_int, c_uint, c_void, CStr, CString, NulError};
2525
use std::fmt::{Display, Formatter};
26-
use std::io::ErrorKind;
2726
use std::marker::PhantomData;
2827
use std::mem::transmute;
2928
use std::rc::Rc;
29+
use thiserror::Error;
30+
31+
#[derive(Error, Debug)]
32+
pub enum Error {
33+
#[error("{0}")]
34+
InvalidInput(String),
35+
36+
#[error("NulError from CString::new")]
37+
NulError(#[from] NulError),
38+
//
39+
// #[error("data store disconnected")]
40+
// IOError(#[from] std::io::Error),
41+
// #[error("the data for key `{0}` is not available")]
42+
// Redaction(String),
43+
// #[error("invalid header (expected {expected:?}, found {found:?})")]
44+
// InvalidHeader {
45+
// expected: String,
46+
// found: String,
47+
// },
48+
// #[error("unknown data store error")]
49+
// Unknown,
50+
}
3051

31-
pub type Error = std::io::Error;
52+
// see https://github.com/dtolnay/thiserror/issues/62
53+
impl From<std::convert::Infallible> for Error {
54+
fn from(_: std::convert::Infallible) -> Self {
55+
unreachable!()
56+
}
57+
}
3258

3359
fn c2r_str(str: *mut c_char) -> String {
3460
let mut res = String::new();
@@ -51,15 +77,15 @@ fn c2r_vec(buf: *mut u8, len: c_int) -> Vec<u8> {
5177
fn last_error() -> Error {
5278
match unsafe { zxing_LastErrorMsg().as_mut() } {
5379
None => panic!("Internal error: zxing_LastErrorMsg() returned NULL"),
54-
Some(error) => Error::new(ErrorKind::InvalidInput, c2r_str(error)),
80+
Some(error) => Error::InvalidInput(c2r_str(error)),
5581
}
5682
}
5783

5884
macro_rules! last_error_or {
5985
($expr:expr) => {
6086
match unsafe { zxing_LastErrorMsg().as_mut() } {
6187
None => Ok($expr),
62-
Some(error) => Err(Error::new(ErrorKind::InvalidInput, c2r_str(error))),
88+
Some(error) => Err(Error::InvalidInput(c2r_str(error))),
6389
}
6490
};
6591
}
@@ -134,8 +160,7 @@ impl<'a> From<&'a ImageView<'a>> for ImageView<'a> {
134160

135161
impl<'a> ImageView<'a> {
136162
fn try_into_int<T: TryInto<c_int>>(val: T) -> Result<c_int, Error> {
137-
val.try_into()
138-
.map_err(|_| Error::new(ErrorKind::InvalidInput, "Could not convert Integer into c_int."))
163+
val.try_into().map_err(|_| Error::InvalidInput("Could not convert Integer into c_int.".to_string()))
139164
}
140165

141166
/// Constructs an ImageView from a raw pointer and the width/height (in pixels)
@@ -223,7 +248,7 @@ impl<'a> TryFrom<&'a image::DynamicImage> for ImageView<'a> {
223248
};
224249
match format {
225250
Some(format) => Ok(ImageView::from_slice(img.as_bytes(), img.width(), img.height(), format)?),
226-
None => Err(Error::new(ErrorKind::InvalidInput, "Invalid image format (must be either luma8|rgb8|rgba8)")),
251+
None => Err(Error::InvalidInput("Invalid image format (must be either luma8|rgb8|rgba8)".to_string())),
227252
}
228253
}
229254
}
@@ -362,10 +387,13 @@ pub fn barcode_formats_from_string(str: impl AsRef<str>) -> Result<BarcodeFormat
362387
}
363388
}
364389

365-
pub fn read_barcodes<'a>(image: impl TryInto<ImageView<'a>>, opts: impl AsRef<ReaderOptions>) -> Result<Vec<Barcode>, Error> {
366-
let iv_: ImageView = image
367-
.try_into()
368-
.map_err(|_| Error::new(ErrorKind::InvalidInput, "Failed to image.try_into::<ImageView>()"))?;
390+
pub fn read_barcodes<'a, IV, RO>(image: IV, opts: RO) -> Result<Vec<Barcode>, Error>
391+
where
392+
IV: TryInto<ImageView<'a>>,
393+
IV::Error: Into<Error>,
394+
RO: AsRef<ReaderOptions>,
395+
{
396+
let iv_: ImageView = image.try_into().map_err(Into::into)?;
369397
unsafe {
370398
let results = zxing_ReadBarcodes((iv_.0).0, opts.as_ref().0);
371399
if !results.is_null() {

0 commit comments

Comments
 (0)