aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-09-09 21:24:59 +0200
committerRobin Krahl <robin.krahl@ireas.org>2020-09-09 21:55:23 +0200
commit70b9a1f0287f2aeda9677110106f43d1048306f3 (patch)
tree72c17539802d8355f5458201a77d97ae1baa62f5
parent80fbab0f4dcdd6e172fab00794fb943bbe81fa1e (diff)
downloadnitrocli-70b9a1f0287f2aeda9677110106f43d1048306f3.tar.gz
nitrocli-70b9a1f0287f2aeda9677110106f43d1048306f3.tar.bz2
Refactor find_device into find_devices
This patch refactors the find_device function that finds a Nitrokey device that matches the given filter or returns an error if there are no or multiple matching Nitrokey devices into the find_devices function that returns a vector with all matching Nitrokey devices. The check for exactly one matching device is now performed in the connect function.
-rw-r--r--src/commands.rs47
1 files changed, 23 insertions, 24 deletions
diff --git a/src/commands.rs b/src/commands.rs
index 23f43f3..597fcd8 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -65,11 +65,11 @@ fn format_filter(config: &config::Config) -> String {
}
}
-/// Find a Nitrokey device that matches the given requirements
-fn find_device(config: &config::Config) -> anyhow::Result<nitrokey::DeviceInfo> {
+/// Find all Nitrokey devices that match the given requirements
+fn find_devices(config: &config::Config) -> anyhow::Result<Vec<nitrokey::DeviceInfo>> {
let devices = nitrokey::list_devices().context("Failed to enumerate Nitrokey devices")?;
let nkmodel = config.model.map(nitrokey::Model::from);
- let mut iter = devices
+ let matching_devices = devices
.into_iter()
.filter(|device| nkmodel.is_none() || device.model == nkmodel)
.filter(|device| {
@@ -79,35 +79,34 @@ fn find_device(config: &config::Config) -> anyhow::Result<nitrokey::DeviceInfo>
.map(|sn| config.serial_numbers.contains(&sn))
.unwrap_or_default()
})
- .filter(|device| config.usb_path.is_none() || config.usb_path.as_ref() == Some(&device.path));
+ .filter(|device| config.usb_path.is_none() || config.usb_path.as_ref() == Some(&device.path))
+ .collect();
+ Ok(matching_devices)
+}
- let device = iter
- .next()
- .with_context(|| format!("Nitrokey device not found{}", format_filter(config)))?;
+/// Connect to a Nitrokey device that matches the given requirements
+fn connect<'mgr>(
+ manager: &'mgr mut nitrokey::Manager,
+ config: &config::Config,
+) -> anyhow::Result<nitrokey::DeviceWrapper<'mgr>> {
+ let device_infos = find_devices(config)?;
anyhow::ensure!(
- iter.next().is_none(),
+ device_infos.len() < 2,
"Multiple Nitrokey devices found{}. Use the --model, --serial-number, and --usb-path options \
to select one",
format_filter(config)
);
- Ok(device)
-}
-/// Connect to a Nitrokey device that matches the given requirements
-fn connect<'mgr>(
- manager: &'mgr mut nitrokey::Manager,
- config: &config::Config,
-) -> anyhow::Result<nitrokey::DeviceWrapper<'mgr>> {
- let device_info = find_device(config)?;
- manager
- .connect_path(device_info.path.deref())
- .with_context(|| {
- format!(
- "Failed to connect to Nitrokey device at path {}",
- device_info.path
- )
- })
+ let device = device_infos
+ .first()
+ .with_context(|| format!("Nitrokey device not found{}", format_filter(config)))?;
+ manager.connect_path(device.path.deref()).with_context(|| {
+ format!(
+ "Failed to connect to Nitrokey device at path {}",
+ device.path
+ )
+ })
}
/// Connect to any Nitrokey device and do something with it.