Skip to content

Commit 797c553

Browse files
authored
Merge pull request #222 from yawara/remove-anyhow
Remove `anyhow` dependency
2 parents 830f801 + 38fb462 commit 797c553

5 files changed

Lines changed: 40 additions & 15 deletions

File tree

cameleon/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ tracing = "0.1.26"
2525
auto_impl = "1.0.1"
2626
cameleon-device = { path = "../device", version = "0.1.14" }
2727
cameleon-genapi = { path = "../genapi", version = "0.1.14" }
28-
anyhow = "1.0.40"
2928

3029
[dev-dependencies]
3130
trybuild = "1.0.42"

cameleon/src/lib.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,27 @@ pub enum CameleonError {
183183
/// A specialized `Result` type for device control.
184184
pub type ControlResult<T> = std::result::Result<T, ControlError>;
185185

186+
/// An error detail for communication with a device.
187+
#[derive(Debug, thiserror::Error)]
188+
pub enum DeviceIoError {
189+
/// Underlying transport error.
190+
#[cfg(feature = "libusb")]
191+
#[error(transparent)]
192+
Transport(#[from] cameleon_device::u3v::Error),
193+
194+
/// Protocol or runtime error represented as a message.
195+
#[error("{0}")]
196+
Message(Cow<'static, str>),
197+
}
198+
199+
impl DeviceIoError {
200+
/// Constructs [`DeviceIoError::Message`].
201+
#[must_use]
202+
pub fn msg(msg: impl Into<Cow<'static, str>>) -> Self {
203+
Self::Message(msg.into())
204+
}
205+
}
206+
186207
/// An error type for device control.
187208
#[derive(Debug, thiserror::Error)]
188209
pub enum ControlError {
@@ -196,7 +217,7 @@ pub enum ControlError {
196217

197218
/// IO error.
198219
#[error("input/output error: {0}")]
199-
Io(anyhow::Error),
220+
Io(DeviceIoError),
200221

201222
/// Timeout has occured when receiving stream payload.
202223
#[error("timeout has occured when receiving stream payload")]
@@ -244,7 +265,7 @@ pub enum StreamError {
244265

245266
/// IO error.
246267
#[error("can't communicate with the device: {0}")]
247-
Io(anyhow::Error),
268+
Io(DeviceIoError),
248269

249270
/// Timeout has occured when receiving stream payload.
250271
#[error("timeout has occured when receiving stream payload")]

cameleon/src/u3v/control_handle.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use tracing::error;
1919

2020
use super::register_map::{self, Abrm, ManifestTable, Sbrm, Sirm};
2121

22-
use crate::{camera::DeviceControl, genapi::CompressionType, ControlError, ControlResult};
22+
use crate::{
23+
camera::DeviceControl, genapi::CompressionType, ControlError, ControlResult, DeviceIoError,
24+
};
2325

2426
/// Initial timeout duration for transaction between device and host.
2527
/// This value is temporarily used until the device's bootstrap register value is read.
@@ -273,7 +275,7 @@ impl ControlHandle {
273275
.unwrap()
274276
.scd_as()?)
275277
} else {
276-
Err(ControlError::Io(anyhow::Error::msg(
278+
Err(ControlError::Io(DeviceIoError::msg(
277279
"the number of times pending was returned exceeds the retry_count.",
278280
)))
279281
}
@@ -282,14 +284,14 @@ impl ControlHandle {
282284
fn verify_ack(&self, ack: &ack::AckPacket) -> ControlResult<()> {
283285
let status = ack.status().kind();
284286
if status != ack::StatusKind::GenCp(ack::GenCpStatus::Success) {
285-
return Err(ControlError::Io(anyhow::Error::msg(format!(
287+
return Err(ControlError::Io(DeviceIoError::msg(format!(
286288
"invalid status: {:?}",
287289
ack.status().kind()
288290
))));
289291
}
290292

291293
if ack.request_id() != self.next_req_id {
292-
return Err(ControlError::Io(anyhow::Error::msg("request id mismatch")));
294+
return Err(ControlError::Io(DeviceIoError::msg("request id mismatch")));
293295
}
294296

295297
Ok(())
@@ -363,7 +365,7 @@ impl DeviceControl for ControlHandle {
363365

364366
if ack.length as usize != chunk_data_len {
365367
let err_msg = "write mem failed: written length mismatch";
366-
return Err(ControlError::Io(anyhow::Error::msg(err_msg)));
368+
return Err(ControlError::Io(DeviceIoError::msg(err_msg)));
367369
}
368370
}
369371

cameleon/src/u3v/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ pub use cameleon_device::u3v::DeviceInfo;
5757
use cameleon_device::u3v;
5858

5959
use super::{
60-
genapi::DefaultGenApiCtxt, CameleonResult, Camera, CameraInfo, ControlError, StreamError,
60+
genapi::DefaultGenApiCtxt, CameleonResult, Camera, CameraInfo, ControlError, DeviceIoError,
61+
StreamError,
6162
};
6263

6364
/// Enumerate all U3V compatible cameras connected to the host.
@@ -112,13 +113,15 @@ impl From<u3v::Error> for ControlError {
112113
match &err {
113114
LibUsb(libusb_error) => match libusb_error {
114115
Io | InvalidParam | Access | Overflow | Pipe | Interrupted | NoMem
115-
| NotSupported | BadDescriptor | Other => ControlError::Io(err.into()),
116+
| NotSupported | BadDescriptor | Other => {
117+
ControlError::Io(DeviceIoError::from(err))
118+
}
116119
Busy => ControlError::Busy,
117120
NoDevice | NotFound => ControlError::Disconnected,
118121
Timeout => ControlError::Timeout,
119122
},
120123

121-
BufferIo(_) | InvalidPacket(_) => ControlError::Io(err.into()),
124+
BufferIo(_) | InvalidPacket(_) => ControlError::Io(DeviceIoError::from(err)),
122125

123126
InvalidDevice => ControlError::InvalidDevice("invalid device".into()),
124127
}
@@ -136,11 +139,11 @@ impl From<u3v::Error> for StreamError {
136139
match &err {
137140
LibUsb(libusb_error) => match libusb_error {
138141
Io | InvalidParam | Access | Overflow | Pipe | Interrupted | NoMem
139-
| NotSupported | BadDescriptor | Busy | Other => Self::Io(err.into()),
142+
| NotSupported | BadDescriptor | Busy | Other => Self::Io(DeviceIoError::from(err)),
140143
NoDevice | NotFound => Self::Disconnected,
141144
Timeout => Self::Timeout,
142145
},
143-
_ => Self::Io(err.into()),
146+
_ => Self::Io(DeviceIoError::from(err)),
144147
}
145148
}
146149
}

cameleon/src/u3v/stream_handle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use tracing::{error, info, warn};
1717
use crate::{
1818
camera::PayloadStream,
1919
payload::{ImageInfo, Payload, PayloadSender, PayloadType},
20-
ControlError, ControlResult, DeviceControl, StreamError, StreamResult,
20+
ControlError, ControlResult, DeviceControl, DeviceIoError, StreamError, StreamResult,
2121
};
2222

2323
use super::register_map::Abrm;
@@ -89,7 +89,7 @@ impl PayloadStream for StreamHandle {
8989
ctrl: &mut dyn DeviceControl,
9090
) -> StreamResult<()> {
9191
self.params = StreamParams::from_control(ctrl).map_err(|e| {
92-
StreamError::Io(anyhow::Error::msg(format!(
92+
StreamError::Io(DeviceIoError::msg(format!(
9393
"failed to setup streaming parameters: {e}"
9494
)))
9595
})?;

0 commit comments

Comments
 (0)