Merge pull request #1 from AuroransSolis/main

Filter out non-AMD cards.
This commit is contained in:
Adrian Woźniak 2021-07-03 09:09:22 +02:00 committed by GitHub
commit b458d1c0e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use crate::{AmdFanError, CONFIG_PATH};
use crate::{AmdFanError, CONFIG_PATH, ROOT_DIR};
use log::LevelFilter;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::Formatter;
@ -24,10 +24,19 @@ impl FromStr for Card {
if value.len() < 5 {
return Err(AmdFanError::InputTooShort);
}
value[4..]
let card = value[4..]
.parse::<u32>()
.map_err(|e| AmdFanError::InvalidSuffix(format!("{:?}", e)))
.map(|n| Card(n))
.map(|n| Card(n))?;
std::fs::read_to_string(format!("{}/{}/device/vendor", ROOT_DIR, card))
.map_err(|_| AmdFanError::FailedReadVendor)
.and_then(|vendor| {
if vendor.trim() == "0x1002" {
Ok(card)
} else {
Err(AmdFanError::NotAmdCard)
}
})
}
}
@ -61,6 +70,13 @@ impl<'de> Deserialize<'de> for Card {
"{:?} must have at least 5 characters",
value
))),
Err(AmdFanError::NotAmdCard) => {
Err(E::custom(format!("{} is not an AMD GPU", value)))
}
Err(AmdFanError::FailedReadVendor) => Err(E::custom(format!(
"Failed to read vendor file for {}",
value
))),
}
}
}

View File

@ -16,6 +16,8 @@ pub enum AmdFanError {
InvalidPrefix,
InputTooShort,
InvalidSuffix(String),
NotAmdCard,
FailedReadVendor,
}
#[derive(Debug)]