Skip to content

Commit

Permalink
Merge pull request #82 from martinling/anyhow
Browse files Browse the repository at this point in the history
Use the `anyhow` crate for error handling
  • Loading branch information
miek authored Jan 8, 2024
2 parents 08a25ac + b962ef6 commit 72d2041
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 384 deletions.
75 changes: 72 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ num_enum = "0.5.6"
once_cell = "1.5"
pcap-file = "2.0.0"
tempfile = "3.3.0"
thiserror = "1.0.30"
bitfield = "0.13.2"
num-format = "0.4.0"
humansize = "1.1.1"
Expand All @@ -29,6 +28,7 @@ arc-swap = "1.6.0"
lrumap = "0.1.0"
memmap2 = "0.5.8"
page_size = "0.5.0"
anyhow = { version = "1.0.79", features = ["backtrace"] }

[dev-dependencies]
serde = { version = "1.0.136", features = ["derive"] }
Expand Down
38 changes: 13 additions & 25 deletions src/backend/luna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::thread::{spawn, JoinHandle};
use std::sync::mpsc::{channel, Sender, Receiver};
use std::time::Duration;

use anyhow::{Context as ErrorContext, Error, bail};
use num_enum::{FromPrimitive, IntoPrimitive};
use rusb::{
Context,
Expand Down Expand Up @@ -70,20 +71,6 @@ impl State {
}
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Usb(#[from] rusb::Error),
#[error("channel send error")]
ChannelSend,
#[error("worker thread panic")]
ThreadPanic,
#[error("unsupported analyzer version: Gateware version is {0}. \
Supported range is {MIN_SUPPORTED} or higher, \
but not {NOT_SUPPORTED} or higher")]
WrongVersion(Version),
}

/// A Luna device attached to the system.
pub struct LunaDevice {
usb_device: Device<Context>,
Expand All @@ -97,7 +84,7 @@ pub struct LunaHandle {
}

pub struct LunaStream {
receiver: Receiver<Result<Vec<u8>, Error>>,
receiver: Receiver<Result<Vec<u8>, rusb::Error>>,
}

pub struct LunaStop {
Expand Down Expand Up @@ -134,13 +121,14 @@ impl LunaHandle {
fn new(usb_handle: DeviceHandle<Context>) -> Result<Self, Error> {
let version = usb_handle
.device()
.device_descriptor()
.map_err(Error::Usb)?
.device_descriptor()?
.device_version();
if version >= MIN_SUPPORTED && version < NOT_SUPPORTED {
Ok(Self { usb_handle })
} else {
Err(Error::WrongVersion(version))
bail!("Unsupported analyzer version: Gateware version is {version}. \
Supported range is {MIN_SUPPORTED} or higher, \
but not {NOT_SUPPORTED} or higher")
}
}

Expand Down Expand Up @@ -192,13 +180,13 @@ impl LunaHandle {
packet_queue.extend(&buffer[..count]);
while let Some(packet) = packet_queue.next() {
tx.send(Ok(packet))
.or(Err(Error::ChannelSend))?;
.context("Failed sending packet to channel")?;
};
},
Err(rusb::Error::Timeout) => continue,
Err(usb_error) => {
tx.send(Err(Error::from(usb_error)))
.or(Err(Error::ChannelSend))?;
tx.send(Err(usb_error))
.context("Failed sending error to channel")?;
return Err(Error::from(usb_error));
}
}
Expand Down Expand Up @@ -234,17 +222,17 @@ impl LunaHandle {
}

impl LunaStream {
pub fn next(&mut self) -> Option<Result<Vec<u8>, Error>> {
pub fn next(&mut self) -> Option<Result<Vec<u8>, rusb::Error>> {
self.receiver.recv().ok()
}
}

impl LunaStop {
pub fn stop(self) -> Result<(), Error> {
use Error::*;
println!("Requesting capture stop");
self.stop_request.send(()).or(Err(ChannelSend))?;
self.worker.join().or(Err(ThreadPanic))?
self.stop_request.send(()).context("Failed sending stop request")?;
self.worker.join().err().context("Worker thread panic")?;
Ok(())
}
}

Expand Down
Loading

0 comments on commit 72d2041

Please sign in to comment.