Return Iterator instead of Vec for devices
This commit is contained in:
parent
640023e0f8
commit
af79332fb9
@ -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> {
|
||||||
|
19
src/lib.rs
19
src/lib.rs
@ -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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user