Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the anyhow crate for error handling #82

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools
build:
os: ubuntu-22.04
tools:
python: "3.12"

# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/source/conf.py

# Build PDF for docs
formats:
- pdf

python:
install:
- requirements: docs/requirements.txt
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
Loading