Trait libmacchina::traits::BatteryReadout [−][src]
pub trait BatteryReadout {
fn new() -> Self;
fn percentage(&self) -> Result<u8, ReadoutError> { ... }
fn status(&self) -> Result<BatteryState, ReadoutError> { ... }
fn health(&self) -> Result<u64, ReadoutError> { ... }
}
Expand description
This trait provides the necessary functions for querying battery statistics from the host
computer. A desktop computer might not be able to provide values such as percentage
and
status
, which means a ReadoutError
can be returned.
Example
use libmacchina::traits::BatteryReadout;
use libmacchina::traits::ReadoutError;
use libmacchina::traits::BatteryState;
//You can add fields to this struct which will then need to be initialized in the
//BatteryReadout::new() function.
pub struct MacOSBatteryReadout;
impl BatteryReadout for MacOSBatteryReadout {
fn new() -> Self {
MacOSBatteryReadout {}
}
fn percentage(&self) -> Result<u8, ReadoutError> {
//get the battery percentage somehow...
Ok(100u8) //always fully charged
}
fn status(&self) -> Result<BatteryState, ReadoutError> {
//check if battery is being charged...
Ok(BatteryState::Charging) //always charging.
}
}
Required methods
Provided methods
fn percentage(&self) -> Result<u8, ReadoutError>
fn percentage(&self) -> Result<u8, ReadoutError>
This function is used for querying the current battery percentage. The expected value is
a u8 in the range of 0
to 100
.
fn status(&self) -> Result<BatteryState, ReadoutError>
fn status(&self) -> Result<BatteryState, ReadoutError>
This function is used for querying the current battery charging state. If the battery is
currently being charged, we expect a return value of BatteryState::Charging
, otherwise
BatteryState::Discharging
.
fn health(&self) -> Result<u64, ReadoutError>
fn health(&self) -> Result<u64, ReadoutError>
This function is used for querying the current battery’s health in percentage.