devices/
lib.rs

1#![warn(clippy::pedantic)]
2#![warn(missing_docs)]
3#![allow(clippy::module_name_repetitions)]
4#![allow(clippy::must_use_candidate)]
5#![allow(clippy::redundant_closure_for_method_calls)]
6#![doc = include_str!("../README.md")]
7
8mod error;
9mod info;
10mod path;
11
12use cfg_if::cfg_if;
13pub use error::Error;
14pub use info::DeviceInfo;
15pub use path::DevicePath;
16
17#[cfg(unix)]
18mod linux;
19
20#[cfg(windows)]
21mod win32;
22
23/// Information about system devices.
24pub struct Devices {
25    _marker: (),
26}
27
28impl Devices {
29    /// Retrieve a list of all connected devices.
30    /// # Errors
31    /// If the platform is unsupported or there is an issue retrieving the list of devices, an error is returned.
32    pub fn get() -> Result<Vec<DeviceInfo>, Error> {
33        let mut devices = Self::pci()?;
34        devices.extend(Self::usb()?);
35
36        Ok(devices)
37    }
38
39    /// Retrieve a list of all connected PCI devices.
40    /// # Errors
41    /// If the platform is unsupported or there is an issue retrieving the list of devices, an error is returned.
42    pub fn pci() -> Result<Vec<DeviceInfo>, Error> {
43        cfg_if! {
44            if #[cfg(unix)] {
45                linux::get_pci()
46            } else if #[cfg(windows)] {
47                win32::get_pci()
48            } else {
49                Err(Error::UnsupportedPlatform)
50            }
51        }
52    }
53
54    /// Retrieve a list of all connected USB devices.
55    /// # Errors
56    /// If the platform is unsupported or there is an issue retrieving the list of devices, an error is returned.
57    pub fn usb() -> Result<Vec<DeviceInfo>, Error> {
58        cfg_if! {
59            if #[cfg(unix)] {
60                linux::get_usb()
61            } else if #[cfg(windows)] {
62                win32::get_usb()
63            } else {
64                Err(Error::UnsupportedPlatform)
65            }
66        }
67    }
68}