From f6cc540a3363b9b8009cee09ba2967dd9143127a Mon Sep 17 00:00:00 2001 From: Ilya Zlobintsev Date: Sat, 12 Nov 2022 12:08:17 +0200 Subject: [PATCH] release 0.6.1: Error now implements `Display` and `Error` Many schema types now implement `Eq` Fix broken class test Bump rust edition to 2021 --- Cargo.toml | 4 ++-- src/error.rs | 26 ++++++++++++++++++++++++++ src/schema.rs | 4 ++-- tests/classes.rs | 15 +++++++++++++++ tests/devices.rs | 21 --------------------- 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e64d899..f5e2791 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "pciid-parser" -version = "0.6.0" +version = "0.6.1" authors = ["Ilya Zlobintsev "] -edition = "2018" +edition = "2021" license = "MIT" repository = "https://github.com/ilyazzz/pci-id-parser" homepage = "https://github.com/ilyazzz/pci-id-parser" diff --git a/src/error.rs b/src/error.rs index 23d5ac8..863eed0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + #[derive(Debug)] pub enum Error { FileNotFound, @@ -20,6 +22,30 @@ impl From for Error { } } +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::FileNotFound => write!(f, "file not found"), + Error::Parse(err) => write!(f, "parsing error: {err}"), + Error::Io(err) => write!(f, "io error: {err}"), + #[cfg(feature = "online")] + Error::Request(err) => write!(f, "network request error: {err}"), + } + } +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Error::FileNotFound => None, + Error::Parse(_) => None, + Error::Io(err) => Some(err), + #[cfg(feature = "online")] + Error::Request(err) => Some(err), + } + } +} + impl Error { pub(crate) fn no_current_vendor() -> Error { Error::Parse("trying to add a device without a vendor".to_owned()) diff --git a/src/schema.rs b/src/schema.rs index 07704a8..1777801 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -8,13 +8,13 @@ pub struct DeviceInfo<'a> { pub subdevice_name: Option<&'a str>, } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub struct Vendor { pub name: String, pub devices: HashMap, } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Device { pub name: String, pub subdevices: HashMap, diff --git a/tests/classes.rs b/tests/classes.rs index f0de918..a16832b 100644 --- a/tests/classes.rs +++ b/tests/classes.rs @@ -33,3 +33,18 @@ fn get_vga_controller_if_prog() { assert_eq!(prog_if_name, "VGA controller"); } + +#[cfg(feature = "online")] +#[test] +fn get_usb_device_if_prog() { + let db = Database::get_online().unwrap(); + + let class = db.classes.get("0c").unwrap(); + assert_eq!(class.name, "Serial bus controller"); + + let subclass = class.subclasses.get("03").unwrap(); + assert_eq!(subclass.name, "USB controller"); + + let prog_if = subclass.prog_ifs.get("fe").unwrap(); + assert_eq!(prog_if, "USB Device"); +} diff --git a/tests/devices.rs b/tests/devices.rs index 900d518..e31c941 100644 --- a/tests/devices.rs +++ b/tests/devices.rs @@ -55,24 +55,3 @@ fn class_not_in_vendors() { assert_eq!(db.vendors.get("c"), None); assert_eq!(db.vendors.get("c 09"), None); } - -#[cfg(feature = "online")] -#[test] -fn parse_incomplete() { - use pciid_parser::schema::DeviceInfo; - - let db = Database::get_online().unwrap(); - - let device_info = db.get_device_info("C 0c", "03", "fe", ""); - let expected_info = DeviceInfo { - vendor_name: Some("Serial bus controller"), - device_name: Some("USB controller"), - subvendor_name: None, - subdevice_name: Some("USB Device"), - }; - - assert_eq!(device_info.vendor_name, expected_info.vendor_name); - assert_eq!(device_info.device_name, expected_info.device_name); - assert_eq!(device_info.subvendor_name, expected_info.subvendor_name); - assert_eq!(device_info.subdevice_name, expected_info.subdevice_name); -}