From 70b9a1f0287f2aeda9677110106f43d1048306f3 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 9 Sep 2020 21:24:59 +0200 Subject: 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. --- src/commands.rs | 47 +++++++++++++++++++++++------------------------ 1 file 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 { +/// Find all Nitrokey devices that match the given requirements +fn find_devices(config: &config::Config) -> anyhow::Result> { 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 .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> { + 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> { - 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. -- cgit v1.2.1