1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use crate::device::CURRENT_DEVICE;
use std::fs::File;
use std::io::Read;
fn read_attribute(attr: &str) -> Result<String, String> {
let mut data = String::new();
match File::open(format!(
"/sys/class/power_supply/{0}/{1}",
CURRENT_DEVICE.get_internal_battery_name(),
attr
)) {
Err(e) => Err(format!("Unable to open file: {0}", e)),
Ok(ref mut f) => match f.read_to_string(&mut data).unwrap_or(0) {
0 => Err("Unable to read file".to_owned()),
_ => Ok(data.trim().to_owned()),
},
}
}
pub fn percentage() -> Result<i32, String> {
let curr = read_attribute("capacity")?;
match curr.parse::<i32>() {
Ok(r) => Ok(r),
Err(_) => {
Err("Unable to parse the contents of 'capacity' during a battery query".to_owned())
}
}
}
pub fn human_readable_capacity_level() -> Result<String, String> {
read_attribute("capacity_level")
}
pub fn charge_full() -> Result<i32, String> {
let curr = read_attribute("charge_full")?;
match curr.parse::<i32>() {
Ok(r) => Ok(r),
Err(_) => {
Err("Unable to parse the contents of 'charge_full' during a battery query".to_owned())
}
}
}
pub fn charge_full_design() -> Result<i32, String> {
let curr = read_attribute("charge_full_design")?;
curr.parse::<i32>().map_err(|_| {
"Unable to parse the contents of 'charge_full_design' during a battery query".to_owned()
})
}
pub fn charge() -> Result<i32, String> {
let curr = read_attribute("charge_now")?;
match curr.parse::<i32>() {
Ok(r) => Ok(r),
Err(_) => {
Err("Unable to parse the contents of 'charge_now' during a battery query".to_owned())
}
}
}
pub fn human_readable_charging_status() -> Result<String, String> {
read_attribute("status")
}
pub fn temperature() -> Result<i32, String> {
let curr = read_attribute("temp")?;
match curr.parse::<i32>() {
Ok(r) => Ok(r),
Err(_) => {
Err("Unable to parse the contents of 'current_now' during a battery query".to_owned())
}
}
}
pub fn voltage() -> Result<i32, String> {
let curr = read_attribute("voltage_now")?;
match curr.parse::<i32>() {
Ok(r) => Ok(r),
Err(_) => {
Err("Unable to parse the contents of 'voltage_now' during a battery query".to_owned())
}
}
}
pub fn current() -> Result<i32, String> {
let curr = read_attribute("current_now")?;
match curr.parse::<i32>() {
Ok(r) => Ok(r),
Err(_) => {
Err("Unable to parse the contents of 'current_now' during a battery query".to_owned())
}
}
}