Skip to content

Commit

Permalink
fix(darwin): allow fallback to non-raw key & permit errors (#41)
Browse files Browse the repository at this point in the history
* fix(darwin): allow fallback to non-raw key & permit errors

* build: bump msrv for rustix

* remove unused `mut` on windows

* also bump MSRV in `README.md`
  • Loading branch information
davidkna committed Aug 5, 2023
1 parent 6fd3ae4 commit 19f12f0
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
matrix:
toolchain:
- stable
- 1.56.0 # MSRV
- 1.63.0 # MSRV
target:
- i686-unknown-linux-gnu
- x86_64-unknown-linux-gnu
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
matrix:
toolchain:
- stable
- 1.56.0 # MSRV
- 1.63.0 # MSRV
target:
- x86_64-unknown-freebsd
- aarch64-unknown-linux-gnu
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ categories = ["os"]
keywords = ["battery", "linux", "macos", "windows", "freebsd"]
license = "ISC"
build = "build.rs"
rust-version = "1.56"
rust-version = "1.63"

[features]
config-schema = ["schemars", "serde"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Latest Version](https://img.shields.io/crates/v/starship-battery.svg)](https://crates.io/crates/starship-battery)
[![Latest Version](https://docs.rs/starship-battery/badge.svg)](https://docs.rs/starship-battery)
[![Build Status](https://github.com/starship/rust-battery/workflows/Continuous%20integration/badge.svg)](https://github.com/starship/rust-battery/actions?workflow=Continuous+integration)
![Minimum rustc version](https://img.shields.io/badge/rustc-1.54+-yellow.svg)
![Minimum rustc version](https://img.shields.io/badge/rustc-1.63+-yellow.svg)
![ISC licensed](https://img.shields.io/badge/license-ISC-blue.svg)

> Rust crate providing cross-platform information about the notebook batteries.
Expand Down Expand Up @@ -44,7 +44,7 @@ might be automatically rejected by Apple based on that fact. Use it on your own

## Install

As a prerequisite, `battery` crate requires at least Rustc version **1.54** or greater.
As a prerequisite, `battery` crate requires at least Rustc version **1.63** or greater.

Add the following line into a `Cargo.toml`:

Expand Down
24 changes: 16 additions & 8 deletions src/platform/darwin/iokit/power_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ static DESIGN_CAPACITY_KEY: &str = "DesignCapacity";
// MaxCapacity and CurrentCapacity returns percentage in M series chips, ranging from 1 to 100.
// Have to use AppleRawMaxCapacity and AppleRawCurrentCapacity to get actual mAh.
// No idea if Intel-chip mac need to be changed as well.
static MAX_CAPACITY_KEY: &str = if cfg!(target_arch = "aarch64") {"AppleRawMaxCapacity"} else {"MaxCapacity"};
static CURRENT_CAPACITY_KEY: &str = if cfg!(target_arch = "aarch64") {"AppleRawCurrentCapacity"} else {"CurrentCapacity"};
static MAX_CAPACITY_KEY_RAW: &str = "AppleRawMaxCapacity";
static CURRENT_CAPACITY_KEY_RAW: &str = "AppleRawCurrentCapacity";
static MAX_CAPACITY_KEY: &str = "MaxCapacity";
static CURRENT_CAPACITY_KEY: &str = "CurrentCapacity";

static TEMPERATURE_KEY: &str = "Temperature";
static CYCLE_COUNT_KEY: &str = "CycleCount";
Expand All @@ -43,8 +45,8 @@ pub struct InstantData {
voltage: ElectricPotential,
amperage: ElectricCurrent,
design_capacity: Option<ElectricCharge>,
max_capacity: ElectricCharge,
current_capacity: ElectricCharge,
max_capacity: Option<ElectricCharge>,
current_capacity: Option<ElectricCharge>,
temperature: Option<ThermodynamicTemperature>,
cycle_count: Option<u32>,
time_remaining: Option<Time>,
Expand All @@ -61,8 +63,14 @@ impl InstantData {
design_capacity: Self::get_u32(props, DESIGN_CAPACITY_KEY)
.ok()
.map(|capacity| milliampere_hour!(capacity)),
max_capacity: milliampere_hour!(Self::get_u32(props, MAX_CAPACITY_KEY)?),
current_capacity: milliampere_hour!(Self::get_u32(props, CURRENT_CAPACITY_KEY)?),
max_capacity: Self::get_u32(props, MAX_CAPACITY_KEY_RAW)
.or_else(|_| Self::get_u32(props, MAX_CAPACITY_KEY))
.ok()
.map(|capacity| milliampere_hour!(capacity)),
current_capacity: Self::get_u32(props, CURRENT_CAPACITY_KEY_RAW)
.or_else(|_| Self::get_u32(props, CURRENT_CAPACITY_KEY))
.ok()
.map(|capacity| milliampere_hour!(capacity)),
temperature: Self::get_i32(props, TEMPERATURE_KEY)
.map(|value| celsius!(value as f32 / 100.0))
.ok(),
Expand Down Expand Up @@ -206,11 +214,11 @@ impl DataSource for PowerSource {
}

fn max_capacity(&self) -> ElectricCharge {
self.data.max_capacity
self.data.max_capacity.unwrap_or_default()
}

fn current_capacity(&self) -> ElectricCharge {
self.data.current_capacity
self.data.current_capacity.unwrap_or_default()
}

fn temperature(&self) -> Option<ThermodynamicTemperature> {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/windows/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl DeviceIterator {
return Err(io::Error::from_raw_os_error(result as i32));
}

let mut pdidd = unsafe {
let pdidd = unsafe {
winbase::LocalAlloc(minwinbase::LPTR, buf_size as basetsd::SIZE_T)
as setupapi::PSP_DEVICE_INTERFACE_DETAIL_DATA_W
};
Expand Down
8 changes: 3 additions & 5 deletions src/types/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use std::str;
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[derive(Default)]
pub enum State {
#[default]
Unknown,
Charging,
Discharging,
Expand Down Expand Up @@ -54,8 +56,4 @@ impl fmt::Display for State {
}
}

impl Default for State {
fn default() -> Self {
State::Unknown
}
}

8 changes: 3 additions & 5 deletions src/types/technology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::Error;
/// Possible battery technologies.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[non_exhaustive]
#[derive(Default)]
pub enum Technology {
#[default]
Unknown,
LithiumIon,
LeadAcid,
Expand Down Expand Up @@ -62,8 +64,4 @@ impl fmt::Display for Technology {
}
}

impl Default for Technology {
fn default() -> Self {
Technology::Unknown
}
}

0 comments on commit 19f12f0

Please sign in to comment.