use std::path::PathBuf;
use crate::{PciInfo, PciInfoError};
#[cfg(target_os = "linux")]
mod proc_fs;
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum LinuxProcFsPciEnumerator {
Fastest,
HeadersOnly,
SkipNoncommonHeaders,
Exhaustive,
}
impl crate::PciEnumerator for LinuxProcFsPciEnumerator {
fn enumerate_pci(self) -> Result<PciInfo, PciInfoError> {
let (read_headers, read_extended_headers, read_device_file) = self.into_arguments();
let path = PathBuf::from("/proc/bus");
#[cfg(target_os = "linux")]
proc_fs::enumerate_pci(path, read_headers, read_extended_headers, read_device_file)
}
}
impl LinuxProcFsPciEnumerator {
pub fn with_custom_path<P>(self, path: P) -> CustomPathLinuxProcFsPciEnumerator
where
P: AsRef<std::path::Path>,
{
let path = path.as_ref().to_owned();
let (read_headers, read_extended_headers, read_device_file) = self.into_arguments();
CustomPathLinuxProcFsPciEnumerator {
path,
read_device_file,
read_extended_headers,
read_headers,
}
}
fn into_arguments(self) -> (bool, bool, bool) {
match self {
Self::Fastest => (false, false, true),
Self::HeadersOnly => (true, true, false),
Self::SkipNoncommonHeaders => (true, false, true),
Self::Exhaustive => (true, true, true),
}
}
}
pub struct CustomPathLinuxProcFsPciEnumerator {
path: std::path::PathBuf,
read_headers: bool,
read_extended_headers: bool,
read_device_file: bool,
}
impl crate::PciEnumerator for CustomPathLinuxProcFsPciEnumerator {
fn enumerate_pci(self) -> Result<PciInfo, PciInfoError> {
#[cfg(target_os = "linux")]
proc_fs::enumerate_pci(
self.path,
self.read_headers,
self.read_extended_headers,
self.read_device_file,
)
}
}
test_enumerator!(
LinuxProcFsPciEnumeratorFastest,
LinuxProcFsPciEnumerator::Fastest
);
test_enumerator!(
LinuxProcFsPciEnumeratorExhaustive,
LinuxProcFsPciEnumerator::Exhaustive
);
test_enumerator!(
LinuxProcFsPciEnumeratorHeadersOnly,
LinuxProcFsPciEnumerator::HeadersOnly
);
test_enumerator!(
LinuxProcFsPciEnumeratorSkipNoncommonHeaders,
LinuxProcFsPciEnumerator::SkipNoncommonHeaders
);
test_enumerator!(
LinuxProcFsPciEnumeratorExhaustiveAmd64,
LinuxProcFsPciEnumerator::Exhaustive.with_custom_path("test-data/linux/amd64")
);
test_enumerator!(
LinuxProcFsPciEnumeratorExhaustiveAarch64,
LinuxProcFsPciEnumerator::Exhaustive.with_custom_path("test-data/linux/aarch64")
);