Return Iterator instead of Vec for devices

This commit is contained in:
Arda Xi 2018-12-28 11:37:18 +01:00
parent 640023e0f8
commit af79332fb9
2 changed files with 15 additions and 15 deletions

View File

@ -16,11 +16,12 @@ static USAGE_PAGE: u8 = 0x04;
static USAGE: u8 = 0x08;
static REPORT_SIZE: u8 = 0x74;
pub fn enumerate() -> io::Result<Vec<DeviceInfo>> {
fs::read_dir("/sys/class/hidraw")?
.filter_map(|entry| entry.ok())
.map(|entry| path_to_device(&entry.path()))
.collect()
pub fn enumerate() -> io::Result<impl Iterator<Item = DeviceInfo>> {
fs::read_dir("/sys/class/hidraw").map(|entries| {
entries.filter_map(|entry| entry.ok()).filter_map(|entry| {
path_to_device(&entry.path()).ok()
})
})
}
fn path_to_device(path: &PathBuf) -> io::Result<DeviceInfo> {

View File

@ -10,8 +10,8 @@
//!
//! ```
//! # fn do_fido() -> ctap::FidoResult<()> {
//! let devices = ctap::get_devices()?;
//! let device_info = &devices[0];
//! let mut devices = ctap::get_devices()?;
//! let device_info = &devices.next().unwrap();
//! let mut device = ctap::FidoDevice::new(device_info)?;
//!
//! // This can be omitted if the FIDO device is not configured with a PIN.
@ -74,14 +74,13 @@ pub use self::error::*;
static BROADCAST_CID: [u8; 4] = [0xff, 0xff, 0xff, 0xff];
/// Looks for any connected HID devices and returns those that support FIDO.
pub fn get_devices() -> error::FidoResult<Vec<hid::DeviceInfo>> {
Ok(
hid::enumerate()
.context(FidoErrorKind::Io)?
.into_iter()
.filter(|dev| dev.usage_page == 0xf1d0 && dev.usage == 0x21)
.collect(),
)
pub fn get_devices() -> FidoResult<impl Iterator<Item = hid::DeviceInfo>> {
hid::enumerate()
.context(FidoErrorKind::Io)
.map(|devices| {
devices.filter(|dev| dev.usage_page == 0xf1d0 && dev.usage == 0x21)
})
.map_err(From::from)
}
/// A credential created by a FIDO2 authenticator.