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 USAGE: u8 = 0x08;
static REPORT_SIZE: u8 = 0x74; static REPORT_SIZE: u8 = 0x74;
pub fn enumerate() -> io::Result<Vec<DeviceInfo>> { pub fn enumerate() -> io::Result<impl Iterator<Item = DeviceInfo>> {
fs::read_dir("/sys/class/hidraw")? fs::read_dir("/sys/class/hidraw").map(|entries| {
.filter_map(|entry| entry.ok()) entries.filter_map(|entry| entry.ok()).filter_map(|entry| {
.map(|entry| path_to_device(&entry.path())) path_to_device(&entry.path()).ok()
.collect() })
})
} }
fn path_to_device(path: &PathBuf) -> io::Result<DeviceInfo> { fn path_to_device(path: &PathBuf) -> io::Result<DeviceInfo> {

View File

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