From dccb1ccf9ff5c6b5af8fce1fd004b12411e16e78 Mon Sep 17 00:00:00 2001 From: tkl Date: Fri, 25 Jun 2021 05:07:51 +0000 Subject: [PATCH] Initial commit --- .gitignore | 16 ++++++++++ Cargo.toml | 8 +++++ README.md | 32 ++++++++++++++++++++ examples/bat0.rs | 11 +++++++ src/lib.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 examples/bat0.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ca43ae --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# ---> Rust +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e683330 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "linux_battery" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/README.md b/README.md new file mode 100644 index 0000000..c55fde0 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# linux_battery + +A battery parser using linux' `power_supply` class. + +## Build + +### debug + +```shell +cargo build +``` + +### release + +```shell +cargo build --release +``` + +## Documentation + +```shell +cargo doc +``` + +The built documentation can be found at `target/doc/`. + +## Example + +```shell +cargo build --example bat0 +cargo run --example bat0 +``` diff --git a/examples/bat0.rs b/examples/bat0.rs new file mode 100644 index 0000000..7ab2bae --- /dev/null +++ b/examples/bat0.rs @@ -0,0 +1,11 @@ +use linux_battery; + +fn main() { + let mut battery = linux_battery::Battery::new("BAT0"); + let mut msg = format!("Status: {}", battery.state()); + match battery.capacity() { + Some(cap) => msg = format!("{}\tCapacity: {} %", msg, cap), + None => {}, + } + println!("{}", msg); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4a9edda --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,78 @@ +use std::io; +use std::io::Read; + +/// Battery parser using linux' `power_supply` class +#[allow(dead_code)] +pub struct Battery { + state_file: String, + capacity_file: String, +} + +impl Battery { + /// Returns a battery representing object. + /// # Arguments: + /// * `name` - The name of the battery as it is represented in the sys file system. + /// (`/sys/class/power_supply/`) + /// # Example: + /// ``` + /// mod battery; + /// use battery::Battery; + /// let mut battery = Battery::new("BAT0"); + /// ``` + #[allow(dead_code)] + pub fn new(name: &str) -> Battery { + let path: String = format!("/sys/class/power_supply/{}/", name); + Battery { + state_file: format!("{}status", path), + capacity_file: format!("{}capacity", path), + } + } + + /// Get the current state of the battery. + /// Return values: + /// * `"Unknown"` + /// * `"Charging"` + /// * `"Discharging"` + /// * `"Not_Charging"` + /// * `"Full"` + /// # Example: + /// ``` + /// println!("Battery state: {}", battery.state()); + /// ``` + #[allow(dead_code)] + pub fn state(&mut self) -> String { + match Battery::read_file(&self.state_file) { + Ok(status) => String::from(status.trim()), + Err(_) => String::from("Unknown"), + } + } + + /// Get the current capacity of the battery [%]. + /// # Example: + /// ``` + /// match battery.capacity() { + /// Some(cap) => println!("{} %", cap), + /// None => {}, + /// } + /// ``` + #[allow(dead_code)] + pub fn capacity(&mut self) ->Option { + match Battery::read_file(&self.capacity_file) { + Ok(capacity) => Some(capacity.trim().parse::().unwrap_or(0)), + Err(_) => None, + } + } + + fn read_file(name: &str) -> Result { + let f = std::fs::File::open(name); + let mut f = match f { + Ok(file) => file, + Err(e) => return Err(e), + }; + let mut line = String::new(); + match f.read_to_string(&mut line) { + Ok(_) => Ok(line), + Err(e) =>Err(e), + } + } +}