Skip to content

Commit

Permalink
improve consistency of style
Browse files Browse the repository at this point in the history
  • Loading branch information
Qyriad committed May 3, 2022
1 parent 4ad4572 commit 5c91526
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
10 changes: 10 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# We don't actually want rustfmt to do all the formatting, as there isn't enough granularity.
# Therefore, any options that follow this one are symbolic as guidelines, rather than rules
# for rustfmt.
#disable_all_formatting = true

brace_style = "AlwaysNextLine"
control_brace_style = "AlwaysSameLine"
binop_separator = "Back"
match_block_trailing_comma = true
overflow_delimited_expr = true
18 changes: 12 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use thiserror::Error;
pub enum BmputilError
{
#[error("Access denied when attempting to {operation} to {context}")]
PermissionsError {
PermissionsError
{
#[source]
source: rusb::Error,

Expand All @@ -19,7 +20,8 @@ pub enum BmputilError
},

#[error("Blackmagic Probe device found disconnected when attempting to {operation} to {context}")]
DeviceDisconnectDuringOperationError {
DeviceDisconnectDuringOperationError
{
#[source]
source: rusb::Error,

Expand All @@ -32,7 +34,8 @@ pub enum BmputilError

#[allow(dead_code)] // FIXME: this will presumably be used once we have more dynamic detection of re-enumeration.
#[error("Blackmagic Probe device did not re-enumerate after requesting to switch to DFU mode")]
DeviceReconfigureError {
DeviceReconfigureError
{
/// Source is optional because there may be no libusb error, if detecting connection is
/// done through e.g. checking device lists.
#[source]
Expand All @@ -41,7 +44,8 @@ pub enum BmputilError

#[allow(dead_code)] // FIXME: this will presumably be used once we, well, actually implement the post-flash check.
#[error("Blackmagic Probe device did not re-enumerate after flashing firmware; firmware may be invalid?")]
DeviceRebootError {
DeviceRebootError
{
#[source]
source: Option<rusb::Error>,
},
Expand All @@ -51,7 +55,8 @@ pub enum BmputilError
"Blackmagic Probe device returned bad data ({invalid_thing}) during configuration.\
This generally shouldn't be possible. Maybe cable is bad, or OS is messing with things?"
)]
DeviceSeemsInvalidError {
DeviceSeemsInvalidError
{
#[source]
source: Option<anyhow::Error>,
invalid_thing: String,
Expand All @@ -63,7 +68,8 @@ pub enum BmputilError


#[macro_export]
macro_rules! log_and_return {
macro_rules! log_and_return
{
($err:expr) => {
let err = $err;
log::error!("{}", err);
Expand Down
17 changes: 10 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type DfuDevice = DfuSync<DfuLibusb<rusb::Context>, DfuLibusbError>;


fn device_matches_vid_pid<ContextT>(device: &rusb::Device<ContextT>, vid: Vid, pid: Pid) -> bool
where ContextT: UsbContext,
where
ContextT: UsbContext,
{
let dev_descriptor = device.device_descriptor()
.expect(libusb_cannot_fail!("libusb_get_device_descriptor()"));
Expand Down Expand Up @@ -58,8 +59,6 @@ fn detach_device(device: rusb::Device<rusb::Context>) -> Result<(), BmputilError

// USB configuration descriptors are 1-indexed, as 0 is considered
// to be "unconfigured".
//device.config_descriptor(1)
//.expect("Device seems to not have any configuration descriptors! This shouldn't be possible!")
match device.config_descriptor(1) {
Ok(d) => d,
Err(e) => {
Expand All @@ -76,10 +75,14 @@ fn detach_device(device: rusb::Device<rusb::Context>) -> Result<(), BmputilError
};

// Get the descriptor for the DFU interface on the Blackmagic Probe.
let dfu_interface_descriptor = configuration.interfaces()
.map(|interface| interface.descriptors()
.next().unwrap() // Unwrap fine as we've already established there is at least one interface.
)
let dfu_interface_descriptor = configuration
.interfaces()
.map(|interface| {
interface
.descriptors()
.next()
.unwrap() // Unwrap fine as we've already established there is at least one interface.
})
.find(interface_descriptor_is_dfu)
.ok_or_else(|| BmputilError::DeviceSeemsInvalidError {
source: None,
Expand Down
14 changes: 9 additions & 5 deletions src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ pub enum DescriptorConvertError
"bLength field ({provided_length}) in provided data does not match the correct value\
({correct_length}) for this descriptor type"
)]
LengthFieldMismatch {
LengthFieldMismatch
{
provided_length: u8,
correct_length: u8,
},
Expand All @@ -147,7 +148,8 @@ pub enum DescriptorConvertError
"bDescriptorType field ({provided_type}) in provided data does not match the correct\
value ({correct_type}) for this descriptor type"
)]
DescriptorTypeMismatch {
DescriptorTypeMismatch
{
provided_type: u8,
correct_type: u8,
},
Expand Down Expand Up @@ -221,9 +223,11 @@ pub(crate) const CHECKED_LIBUSB_VERSION: &str = "1.0.26";
macro_rules! libusb_cannot_fail
{
($funcname:literal) => {
format!("As of libusb {}, {} cannot fail. This should be unreachable, unless libusb has updated.",
format!(
"As of libusb {}, {} cannot fail. This should be unreachable, unless libusb has updated.",
crate::usb::CHECKED_LIBUSB_VERSION,
$funcname,
).as_str()
}
)
.as_str()
};
}

0 comments on commit 5c91526

Please sign in to comment.