Skip to content

Commit

Permalink
fix: linux compile error
Browse files Browse the repository at this point in the history
  • Loading branch information
Velfi authored and valpackett committed Jun 17, 2022
1 parent 8ea1b14 commit cd940e3
Showing 1 changed file with 45 additions and 36 deletions.
81 changes: 45 additions & 36 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ fn read_file(path: &str) -> io::Result<String> {
fn value_from_file<T: str::FromStr>(path: &str) -> io::Result<T> {
read_file(path)?
.trim_end_matches('\n')
.parse().map_err(|_| io::Error::new(io::ErrorKind::Other,
format!("File: \"{}\" doesn't contain an int value", &path)))
.parse()
.map_err(|_| {
io::Error::new(
io::ErrorKind::Other,
format!("File: \"{}\" doesn't contain an int value", &path),
)
})
}

fn capacity(charge_full: i32, charge_now: i32) -> f32 {
Expand All @@ -39,9 +44,10 @@ fn time(on_ac: bool, charge_full: i32, charge_now: i32, current_now: i32) -> Dur
if current_now != 0 {
if on_ac {
// Charge time
Duration::from_secs(charge_full.saturating_sub(charge_now).abs() as u64 * 3600u64 / current_now as u64)
}
else {
Duration::from_secs(
charge_full.saturating_sub(charge_now).abs() as u64 * 3600u64 / current_now as u64,
)
} else {
// Discharge time
Duration::from_secs(charge_now as u64 * 3600u64 / current_now as u64)
}
Expand Down Expand Up @@ -564,9 +570,13 @@ impl Platform for PlatformImpl {
.strip_prefix("btime ")
.expect("line starts with 'btime '");
timestamp_str
.parse::<u32>()
.and_then(|timestamp| OffsetDateTime::from_unix_timestamp(timestamp))
.parse::<i64>()
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err.to_string()))
.and_then(|timestamp| {
OffsetDateTime::from_unix_timestamp(timestamp).map_err(|err| {
io::Error::new(io::ErrorKind::InvalidData, err.to_string())
})
})
})
})
}
Expand All @@ -580,13 +590,16 @@ impl Platform for PlatformImpl {
for e in entries {
let p = e.unwrap().path();
let s = p.to_str().unwrap();
if value_from_file::<String>(&(s.to_string() + "/type")).map(|t| t == "Battery").unwrap_or(false) {
if value_from_file::<String>(&(s.to_string() + "/type"))
.map(|t| t == "Battery")
.unwrap_or(false)
{
full += value_from_file::<i32>(&(s.to_string() + "/energy_full"))
.or_else(|_| value_from_file::<i32>(&(s.to_string() + "/charge_full")))?;
.or_else(|_| value_from_file::<i32>(&(s.to_string() + "/charge_full")))?;
now += value_from_file::<i32>(&(s.to_string() + "/energy_now"))
.or_else(|_| value_from_file::<i32>(&(s.to_string() + "/charge_now")))?;
.or_else(|_| value_from_file::<i32>(&(s.to_string() + "/charge_now")))?;
current += value_from_file::<i32>(&(s.to_string() + "/power_now"))
.or_else(|_| value_from_file::<i32>(&(s.to_string() + "/current_now")))?;
.or_else(|_| value_from_file::<i32>(&(s.to_string() + "/current_now")))?;
}
}
if full != 0 {
Expand All @@ -596,7 +609,10 @@ impl Platform for PlatformImpl {
remaining_time: time(on_ac, full, now, current),
})
} else {
Err(io::Error::new(io::ErrorKind::Other, "Missing battery information"))
Err(io::Error::new(
io::ErrorKind::Other,
"Missing battery information",
))
}
}

Expand All @@ -607,7 +623,10 @@ impl Platform for PlatformImpl {
for e in entries {
let p = e.unwrap().path();
let s = p.to_str().unwrap();
if value_from_file::<String>(&(s.to_string() + "/type")).map(|t| t == "Mains").unwrap_or(false) {
if value_from_file::<String>(&(s.to_string() + "/type"))
.map(|t| t == "Mains")
.unwrap_or(false)
{
on_ac |= value_from_file::<i32>(&(s.to_string() + "/online")).map(|v| v == 1)?
}
}
Expand Down Expand Up @@ -689,7 +708,10 @@ impl Platform for PlatformImpl {
.or(read_file("/sys/class/hwmon/hwmon0/temp1_input"))
.and_then(|data| match data.trim().parse::<f32>() {
Ok(x) => Ok(x),
Err(_) => Err(io::Error::new(io::ErrorKind::Other, "Could not parse float")),
Err(_) => Err(io::Error::new(
io::ErrorKind::Other,
"Could not parse float",
)),
})
.map(|num| num / 1000.0)
}
Expand Down Expand Up @@ -759,25 +781,16 @@ impl PlatformMemory {
fn to_memory(self) -> Memory {
let meminfo = &self.meminfo;
Memory {
total: meminfo
.get("MemTotal").copied()
.unwrap_or(ByteSize::b(0)),
total: meminfo.get("MemTotal").copied().unwrap_or(ByteSize::b(0)),
free: saturating_sub_bytes(
meminfo
.get("MemFree").copied()
.unwrap_or(ByteSize::b(0))
+ meminfo
.get("Buffers").copied()
.unwrap_or(ByteSize::b(0))
+ meminfo
.get("Cached").copied()
.unwrap_or(ByteSize::b(0))
meminfo.get("MemFree").copied().unwrap_or(ByteSize::b(0))
+ meminfo.get("Buffers").copied().unwrap_or(ByteSize::b(0))
+ meminfo.get("Cached").copied().unwrap_or(ByteSize::b(0))
+ meminfo
.get("SReclaimable").copied()
.get("SReclaimable")
.copied()
.unwrap_or(ByteSize::b(0)),
meminfo
.get("Shmem").copied()
.unwrap_or(ByteSize::b(0)),
meminfo.get("Shmem").copied().unwrap_or(ByteSize::b(0)),
),
platform_memory: self,
}
Expand All @@ -787,12 +800,8 @@ impl PlatformMemory {
fn to_swap(self) -> Swap {
let meminfo = &self.meminfo;
Swap {
total: meminfo
.get("SwapTotal").copied()
.unwrap_or(ByteSize::b(0)),
free: meminfo
.get("SwapFree").copied()
.unwrap_or(ByteSize::b(0)),
total: meminfo.get("SwapTotal").copied().unwrap_or(ByteSize::b(0)),
free: meminfo.get("SwapFree").copied().unwrap_or(ByteSize::b(0)),
platform_swap: self,
}
}
Expand Down

0 comments on commit cd940e3

Please sign in to comment.