Skip to content

Commit

Permalink
Simplify Bootloader::try_find logic
Browse files Browse the repository at this point in the history
While we cannot filter a &Bootloader iterator as Bootloader does not
imlement Copy, we can filter a Bootloader iterator and make the
selection logic a bit easier to read.
  • Loading branch information
robin-nitrokey authored and nickray committed Jun 3, 2021
1 parent 63355e9 commit e28a491
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/bootloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,13 @@ impl Bootloader {
/// For instance, `write-flash` on the wrong device could wreak havoc.
pub fn try_new(vid: Option<u16>, pid: Option<u16>) -> Option<Self> {
Self::try_find(vid, pid, None)
// // Bootloader is not Copy, so we can't filter
// let mut bootloaders = Self::list();
// let index = bootloaders.iter()
// .position(|bootloader| bootloader.vid == vid && bootloader.pid == pid);
// index.map(|i| bootloaders.remove(i))
}

/// Attempt to find a ROM bootloader with the given UUID (and VID/PID pair).
pub fn try_find(vid: Option<u16>, pid: Option<u16>, uuid: Option<Uuid>) -> Option<Self> {
// Bootloader is not Copy, so we can't filter
let mut bootloaders = Self::list();
let index = bootloaders.iter()
.position(|bootloader| {
Self::list()
.into_iter()
.filter(|bootloader| {
let mut predicate = true;
if vid.is_some() && pid.is_some() {
predicate = bootloader.vid == vid.unwrap() && bootloader.pid == pid.unwrap();
Expand All @@ -88,8 +82,8 @@ impl Bootloader {
}
}
predicate
});
index.map(|i| bootloaders.remove(i))
})
.next()
}

/// Returns a vector of all HID devices that appear to be ROM bootloaders
Expand Down

0 comments on commit e28a491

Please sign in to comment.