use crate::units::{Error as UnitError, Raw, Result as UnitResult};
use std::borrow::Cow;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, Eq, Hash, Ord)]
pub struct Pwm(u8);
impl Pwm {
pub const FULLSPEED: Pwm = Pwm(255);
pub const HALFSPEED: Pwm = Pwm(127);
pub const OFF: Pwm = Pwm(0);
pub fn from_u8(u8: u8) -> Self {
Self(u8)
}
pub fn as_u8(self) -> u8 {
self.0
}
pub fn try_from_percent(percent: impl Into<f64>) -> UnitResult<Self> {
let percent = percent.into();
if percent.is_nan() || !(0.0..=100.0).contains(&percent) {
return Err(UnitError::invalid_value(percent));
}
Ok(Pwm((percent * 2.55) as u8))
}
pub fn as_percent(self) -> f64 {
f64::from(self.0) / 2.55
}
}
impl From<u8> for Pwm {
fn from(value: u8) -> Pwm {
Pwm::from_u8(value)
}
}
impl From<Pwm> for u8 {
fn from(value: Pwm) -> u8 {
value.0
}
}
impl Raw for Pwm {
fn from_raw(raw: &str) -> UnitResult<Self> {
raw.parse::<u8>()
.map(Pwm::from_u8)
.map_err(UnitError::parsing)
}
fn to_raw(&self) -> Cow<'_, str> {
Cow::Owned(self.0.to_string())
}
}
impl fmt::Display for Pwm {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}%", self.as_percent())
}
}
#[allow(missing_docs)]
#[derive(Debug, Default, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum PwmEnable {
FullSpeed,
ManualControl,
#[default]
BiosControl,
}
impl Raw for PwmEnable {
fn from_raw(raw: &str) -> UnitResult<Self> {
match raw {
"0" => Ok(PwmEnable::FullSpeed),
"1" => Ok(PwmEnable::ManualControl),
_ => Ok(PwmEnable::BiosControl),
}
}
fn to_raw(&self) -> Cow<'_, str> {
match self {
PwmEnable::FullSpeed => Cow::from("0"),
PwmEnable::ManualControl => Cow::from("1"),
PwmEnable::BiosControl => Cow::from("2"),
}
}
}
#[allow(missing_docs)]
#[derive(Debug, Default, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum PwmMode {
Dc,
Pwm,
#[default]
Automatic,
}
impl Raw for PwmMode {
fn from_raw(raw: &str) -> UnitResult<Self> {
match raw {
"0" => Ok(PwmMode::Dc),
"1" => Ok(PwmMode::Pwm),
"2" => Ok(PwmMode::Automatic),
raw => Err(UnitError::raw_conversion(raw)),
}
}
fn to_raw(&self) -> Cow<'_, str> {
match self {
PwmMode::Dc => Cow::from("0"),
PwmMode::Pwm => Cow::from("1"),
PwmMode::Automatic => Cow::from("2"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_out_of_bounds() {
assert!(Pwm::try_from_percent(-1.1).is_err());
assert!(Pwm::try_from_percent(0.0).is_ok());
assert!(Pwm::try_from_percent(50.0).is_ok());
assert!(Pwm::try_from_percent(100.0).is_ok());
assert!(Pwm::try_from_percent(100.001).is_err());
assert!(Pwm::try_from_percent(f64::INFINITY).is_err());
assert!(Pwm::try_from_percent(f64::NAN).is_err());
}
}