Skip to content

Commit

Permalink
release 0.6.1:
Browse files Browse the repository at this point in the history
Error now implements `Display` and `Error`
Many schema types now implement `Eq`
Fix broken class test
Bump rust edition to 2021
  • Loading branch information
ilya-zlobintsev committed Nov 12, 2022
1 parent 4653dfb commit f6cc540
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "pciid-parser"
version = "0.6.0"
version = "0.6.1"
authors = ["Ilya Zlobintsev <[email protected]>"]
edition = "2018"
edition = "2021"
license = "MIT"
repository = "https://github.com/ilyazzz/pci-id-parser"
homepage = "https://github.com/ilyazzz/pci-id-parser"
Expand Down
26 changes: 26 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

#[derive(Debug)]
pub enum Error {
FileNotFound,
Expand All @@ -20,6 +22,30 @@ impl From<ureq::Error> 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())
Expand Down
4 changes: 2 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Device>,
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Device {
pub name: String,
pub subdevices: HashMap<SubDeviceId, String>,
Expand Down
15 changes: 15 additions & 0 deletions tests/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
21 changes: 0 additions & 21 deletions tests/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit f6cc540

Please sign in to comment.