use crate::units::{Error as RawError, Raw, Result as RawSensorResult};
use std::borrow::Cow;
use std::fmt;
use std::ops::{Add, Div, Mul};
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct Current(i32);
impl Current {
pub fn from_milli_amperes(millis: i32) -> Current {
Current(millis)
}
pub fn as_milli_amperes(self) -> i32 {
self.0
}
pub fn from_amperes(joules: impl Into<f64>) -> Current {
Self::from_milli_amperes((joules.into() * 1_000.0) as i32)
}
pub fn as_amperes(self) -> f64 {
f64::from(self.0) / 1_000.0
}
}
impl Raw for Current {
fn from_raw(raw: &str) -> RawSensorResult<Self> {
raw.trim()
.parse::<i32>()
.map(Current::from_milli_amperes)
.map_err(|_| RawError::from(raw))
}
fn to_raw(&self) -> Cow<str> {
Cow::Owned(self.0.to_string())
}
}
impl fmt::Display for Current {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}A", self.as_amperes())
}
}
impl Add for Current {
type Output = Self;
fn add(self, other: Self) -> Self {
Current(self.0 + other.0)
}
}
impl<T: Into<i32>> Mul<T> for Current {
type Output = Self;
fn mul(self, other: T) -> Current {
Current(self.0 * other.into())
}
}
impl<T: Into<i32>> Div<T> for Current {
type Output = Self;
fn div(self, other: T) -> Current {
Current(self.0 / other.into())
}
}