use crate::*;
use bytemuck::Zeroable;
pub fn get_battery_information(user_index: impl TryInto<u32>, dev_type: impl Into<BatteryDevType>) -> Result<BatteryInformation, Error> {
fn_context!(xinput::get_battery_information => XInputGetBatteryInformation);
#[allow(non_snake_case)] let XInputGetBatteryInformation = imports::XInputGetBatteryInformation.load(core::sync::atomic::Ordering::Relaxed);
let user_index = user_index.try_into().map_err(|_| fn_param_error!(user_index, error::BAD_ARGUMENTS))?;
let mut info = BatteryInformation::zeroed();
let code = unsafe { XInputGetBatteryInformation(user_index, dev_type.into().into(), info.as_mut()) };
check_success!(code)?;
Ok(info)
}
#[test] fn test_valid_params() {
for user_index in 0..4 {
for dev_type in [BatteryDevType::Gamepad, BatteryDevType::Headset] {
if let Err(err) = get_battery_information(user_index, dev_type) {
assert!(matches!(err.kind(), error::DEVICE_NOT_CONNECTED | error::INVALID_FUNCTION | error::CO_E_NOTINITIALIZED));
}
}
}
}
#[test] fn test_bad_battery_type() {
for dev_type in [3, 42, 250].map(BatteryDevType::from_unchecked) {
if let Err(err) = get_battery_information(0, dev_type) {
assert!(matches!(err.kind(), error::DEVICE_NOT_CONNECTED | error::INVALID_FUNCTION | error::CO_E_NOTINITIALIZED), "{err:?}");
}
}
}
#[test] fn test_bad_user_index() {
for user_index in Some(xuser::INDEX_ANY).into_iter().chain(xuser::invalids()) {
let err = get_battery_information(user_index, BatteryDevType::Gamepad).expect_err("expected error for invalid user_index");
assert!(matches!(err.kind(), error::BAD_ARGUMENTS | error::INVALID_FUNCTION | error::CO_E_NOTINITIALIZED), "unexpected error type: {err:?}");
}
}