-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing proper errors handling.
Both battery and battery-ffi are now propagating possible errors to callers. Supporting multiple devices for FreeBSD and DragonFlyBSD systems (closes #17) Ignoring Linux devices with scope other than System (closes #18).
- Loading branch information
Showing
54 changed files
with
1,752 additions
and
1,015 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
**/*.rs.bk | ||
Cargo.lock | ||
|
||
*.orig | ||
local_build.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ members = [ | |
|
||
[patch.crates-io] | ||
battery = { path = "./battery" } | ||
battery-ffi = { path = "./battery-ffi" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "battery-ffi" | ||
version = "0.2.0" | ||
version = "0.7.0" | ||
authors = ["svartalf <[email protected]>"] | ||
edition = "2018" | ||
description = "FFI bindings for battery crate" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use std::cell::RefCell; | ||
use std::error::Error; | ||
use std::ptr; | ||
use std::slice; | ||
|
||
thread_local! { | ||
static LAST_ERROR: RefCell<Option<Box<Error>>> = RefCell::new(None); | ||
} | ||
|
||
pub fn set_last_error<E: Error + 'static>(err: E) { | ||
LAST_ERROR.with(|prev| { | ||
*prev.borrow_mut() = Some(Box::new(err)); | ||
}); | ||
} | ||
|
||
pub fn take_last_error() -> Option<Box<Error>> { | ||
LAST_ERROR.with(|prev| prev.borrow_mut().take()) | ||
} | ||
|
||
pub fn clear_last_error() { | ||
let _ = take_last_error(); | ||
} | ||
|
||
/// Checks if there was an error before. | ||
/// | ||
/// # Returns | ||
/// | ||
/// `0` if there was no error, `1` if error had occured. | ||
#[no_mangle] | ||
pub extern "C" fn battery_have_last_error() -> libc::c_int { | ||
LAST_ERROR.with(|prev| match *prev.borrow() { | ||
Some(_) => 1, | ||
None => 0, | ||
}) | ||
} | ||
|
||
/// Gets error message length if any error had occurred. | ||
/// | ||
/// # Returns | ||
/// | ||
/// If there was no error before, returns `0`, | ||
/// otherwise returns message length including trailing `\0`. | ||
#[no_mangle] | ||
pub extern "C" fn battery_last_error_length() -> libc::c_int { | ||
// TODO: Support Windows UTF-16 strings | ||
LAST_ERROR.with(|prev| match *prev.borrow() { | ||
Some(ref err) => err.to_string().len() as libc::c_int + 1, | ||
None => 0, | ||
}) | ||
} | ||
|
||
/// Fills passed buffer with an error message. | ||
/// | ||
/// Buffer length can be get with [battery_last_error_length](fn.battery_last_error_length.html) function. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns `-1` is passed buffer is `NULL` or too small for error message. | ||
/// Returns `0` if there was no error previously. | ||
/// | ||
/// In all other cases returns error message length. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn battery_last_error_message(buffer: *mut libc::c_char, length: libc::c_int) -> libc::c_int { | ||
if buffer.is_null() { | ||
return -1; | ||
} | ||
|
||
let last_error = match take_last_error() { | ||
Some(err) => err, | ||
None => return 0, | ||
}; | ||
|
||
let error_message = last_error.to_string(); | ||
|
||
let buffer = slice::from_raw_parts_mut(buffer as *mut u8, length as usize); | ||
|
||
if error_message.len() >= buffer.len() { | ||
return -1; | ||
} | ||
|
||
ptr::copy_nonoverlapping(error_message.as_ptr(), buffer.as_mut_ptr(), error_message.len()); | ||
|
||
buffer[error_message.len()] = b'\0'; | ||
|
||
error_message.len() as libc::c_int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.