Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error cleanup #53

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 4 additions & 125 deletions wooting-analog-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub enum DeviceEventType {
}

#[cfg_attr(feature = "serdes", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Clone, Primitive, Error)]
#[derive(Debug, Default, PartialEq, Clone, Primitive, Error)]
#[repr(C)]
pub enum WootingAnalogResult {
#[error("All OK")]
Expand All @@ -241,6 +241,7 @@ pub enum WootingAnalogResult {
#[error("No Plugins were found")]
NoPlugins = -1995isize,
/// The specified function was not found in the library
#[default]
#[error("The specified function was not found in the library")]
FunctionNotFound = -1994isize,
/// No Keycode mapping to HID was found for the given Keycode
Expand All @@ -267,142 +268,20 @@ impl WootingAnalogResult {
}
}

impl Default for WootingAnalogResult {
fn default() -> Self {
WootingAnalogResult::FunctionNotFound
}
}

#[derive(Debug)]
pub struct SDKResult<T>(pub std::result::Result<T, WootingAnalogResult>);

impl<T> Default for SDKResult<T> {
fn default() -> Self {
Err(Default::default()).into()
}
}

impl<T> Deref for SDKResult<T> {
type Target = std::result::Result<T, WootingAnalogResult>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> From<std::result::Result<T, WootingAnalogResult>> for SDKResult<T> {
fn from(ptr: std::result::Result<T, WootingAnalogResult>) -> Self {
SDKResult(ptr)
}
}

impl<T> Into<std::result::Result<T, WootingAnalogResult>> for SDKResult<T> {
fn into(self) -> std::result::Result<T, WootingAnalogResult> {
self.0
}
}

//TODO: Figure out a way to not have to use this for the lib_wrap_option in the sdk
impl<'a> From<FfiStr<'a>> for SDKResult<FfiStr<'a>> {
fn from(res: FfiStr<'a>) -> Self {
Ok(res).into()
}
}

impl From<c_int> for SDKResult<c_int> {
fn from(res: c_int) -> Self {
if res >= 0 {
Ok(res).into()
} else {
Err(WootingAnalogResult::from_i32(res).unwrap_or(WootingAnalogResult::Failure)).into()
}
}
}

impl From<c_int> for SDKResult<u32> {
fn from(res: c_int) -> Self {
if res >= 0 {
Ok(res as u32).into()
} else {
Err(WootingAnalogResult::from_i32(res).unwrap_or(WootingAnalogResult::Failure)).into()
}
}
}
pub type SDKResult<T> = Result<T, WootingAnalogResult>;

impl Into<c_int> for WootingAnalogResult {
fn into(self) -> c_int {
self as c_int
}
}

impl From<u32> for SDKResult<u32> {
fn from(res: u32) -> Self {
Ok(res).into()
}
}

impl Into<i32> for SDKResult<u32> {
fn into(self) -> i32 {
match self.0 {
Ok(v) => v as i32,
Err(e) => e.into(),
}
}
}

impl Into<c_int> for SDKResult<c_int> {
fn into(self) -> c_int {
match self.0 {
Ok(v) => v,
Err(e) => e.into(),
}
}
}

impl From<f32> for SDKResult<f32> {
fn from(res: f32) -> Self {
if res >= 0.0 {
Ok(res).into()
} else {
Err(WootingAnalogResult::from_f32(res).unwrap_or(WootingAnalogResult::Failure)).into()
}
}
}

impl Into<f32> for WootingAnalogResult {
fn into(self) -> f32 {
(self as i32) as f32
}
}

impl Into<f32> for SDKResult<f32> {
fn into(self) -> f32 {
match self.0 {
Ok(v) => v,
Err(e) => e.into(),
}
}
}

impl Into<WootingAnalogResult> for SDKResult<()> {
fn into(self) -> WootingAnalogResult {
match self.0 {
Ok(_) => WootingAnalogResult::Ok,
Err(e) => e,
}
}
}

impl From<WootingAnalogResult> for SDKResult<()> {
fn from(res: WootingAnalogResult) -> Self {
if res.is_ok() {
Ok(()).into()
} else {
Err(res).into()
}
}
}

impl Into<bool> for WootingAnalogResult {
fn into(self) -> bool {
self == WootingAnalogResult::Ok
Expand Down Expand Up @@ -576,4 +455,4 @@ pub enum HIDCodes {
RightShift = 0xe5, //SHIFT_RIGHT
RightAlt = 0xe6, //ALT_RIGHT
RightMeta = 0xe7, //META_RIGHT
}
}
LastExceed marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion wooting-analog-plugin-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ mod ffi {
};
super::generate_device_id(&serial, vendor_id, product_id)
}
}
}
68 changes: 32 additions & 36 deletions wooting-analog-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ trait DeviceImplementation: objekt::Clone + Send {
Ok(len) => {
// If the length is 0 then that means the read timed out, so we shouldn't use it to update values
if len == 0 {
return Ok(None).into();
return Ok(None);
}
}
Err(e) => {
error!("Failed to read buffer: {}", e);

return Err(WootingAnalogResult::DeviceDisconnected).into();
return Err(WootingAnalogResult::DeviceDisconnected);
}
}
//println!("{:?}", buffer);
Expand All @@ -98,7 +98,6 @@ trait DeviceImplementation: objekt::Clone + Send {
})
.collect(),
))
.into()
}

/// Get the unique device ID from the given `device_info`
Expand Down Expand Up @@ -276,10 +275,7 @@ impl Device {
return 0;
}

match device_impl
.get_analog_buffer(&device, ANALOG_MAX_SIZE)
.into()
{
match device_impl.get_analog_buffer(&device, ANALOG_MAX_SIZE) {
Ok(data) => {
if let Some(data) = data {
let mut m = t_buffer.lock().unwrap();
Expand Down Expand Up @@ -324,7 +320,7 @@ impl Device {
}

fn read_analog(&mut self, code: u16) -> SDKResult<c_float> {
(*self.buffer.lock().unwrap().get(&code).unwrap_or(&0.0)).into()
Ok(*self.buffer.lock().unwrap().get(&code).unwrap_or(&0.0))
}

fn read_full_buffer(&mut self, _max_length: usize) -> SDKResult<HashMap<c_ushort, c_float>> {
Expand All @@ -342,7 +338,7 @@ impl Device {
//Store the newPressedKeys for the next call
self.pressed_keys = new_pressed_keys;

Ok(buffer).into()
Ok(buffer)
}
}

Expand Down Expand Up @@ -427,7 +423,7 @@ impl WootingPlugin {
}
Err(e) => {
error!("Error opening HID Device: {}", e);
//return WootingAnalogResult::Failure.into();
//return WootingAnalogResult::Failure;
}
}
}
Expand Down Expand Up @@ -456,7 +452,7 @@ impl WootingPlugin {
}
Err(e) => {
error!("Error obtaining HIDAPI: {}", e);
return Err(WootingAnalogResult::Failure).into();
return Err(WootingAnalogResult::Failure);
}
};

Expand Down Expand Up @@ -493,13 +489,13 @@ impl WootingPlugin {
})
});
debug!("Started timer");
Ok(self.devices.lock().unwrap().len() as u32).into()
Ok(self.devices.lock().unwrap().len() as u32)
}
}

impl Plugin for WootingPlugin {
fn name(&mut self) -> SDKResult<&'static str> {
Ok(PLUGIN_NAME).into()
Ok(PLUGIN_NAME)
}

fn initialise(
Expand Down Expand Up @@ -530,20 +526,20 @@ impl Plugin for WootingPlugin {

fn read_analog(&mut self, code: u16, device_id: DeviceID) -> SDKResult<f32> {
if !self.initialised {
return Err(WootingAnalogResult::UnInitialized).into();
return Err(WootingAnalogResult::UnInitialized);
}

if self.devices.lock().unwrap().is_empty() {
return Err(WootingAnalogResult::NoDevices).into();
return Err(WootingAnalogResult::NoDevices);
}

//If the Device ID is 0 we want to go through all the connected devices
//and combine the analog values
if device_id == 0 {
let mut analog: f32 = -1.0;
let mut error: WootingAnalogResult = WootingAnalogResult::Ok;
let mut error = WootingAnalogResult::Ok;
for (_id, device) in self.devices.lock().unwrap().iter_mut() {
match device.read_analog(code).into() {
match device.read_analog(code) {
Ok(val) => {
analog = analog.max(val);
}
Expand All @@ -554,19 +550,19 @@ impl Plugin for WootingPlugin {
}

if analog < 0.0 {
Err(error).into()
Err(error)
} else {
analog.into()
analog
}
} else
//If the device id is not 0, we try and find a connected device with that ID and read from it
{
match self.devices.lock().unwrap().get_mut(&device_id) {
Some(device) => match device.read_analog(code).into() {
Ok(val) => val.into(),
Err(e) => Err(e).into(),
Some(device) => match device.read_analog(code) {
Ok(val) => val,
Err(e) => Err(e),
},
None => Err(WootingAnalogResult::NoDevices).into(),
None => Err(WootingAnalogResult::NoDevices),
}
}
}
Expand All @@ -577,21 +573,21 @@ impl Plugin for WootingPlugin {
device_id: DeviceID,
) -> SDKResult<HashMap<c_ushort, c_float>> {
if !self.initialised {
return Err(WootingAnalogResult::UnInitialized).into();
return Err(WootingAnalogResult::UnInitialized);
}

if self.devices.lock().unwrap().is_empty() {
return Err(WootingAnalogResult::NoDevices).into();
return Err(WootingAnalogResult::NoDevices);
}

//If the Device ID is 0 we want to go through all the connected devices
//and combine the analog values
if device_id == 0 {
let mut analog: HashMap<c_ushort, c_float> = HashMap::new();
let mut any_read = false;
let mut error: WootingAnalogResult = WootingAnalogResult::Ok;
let mut error = WootingAnalogResult::Ok;
for (_id, device) in self.devices.lock().unwrap().iter_mut() {
match device.read_full_buffer(max_length).into() {
match device.read_full_buffer(max_length) {
Ok(val) => {
any_read = true;
analog.extend(val);
Expand All @@ -603,35 +599,35 @@ impl Plugin for WootingPlugin {
}

if !any_read {
Err(error).into()
Err(error)
} else {
Ok(analog).into()
Ok(analog)
}
} else
//If the device id is not 0, we try and find a connected device with that ID and read from it
{
match self.devices.lock().unwrap().get_mut(&device_id) {
Some(device) => match device.read_full_buffer(max_length).into() {
Ok(val) => Ok(val).into(),
Err(e) => Err(e).into(),
Some(device) => match device.read_full_buffer(max_length) {
Ok(val) => Ok(val),
Err(e) => Err(e),
},
None => Err(WootingAnalogResult::NoDevices).into(),
None => Err(WootingAnalogResult::NoDevices),
}
}
}

fn device_info(&mut self) -> SDKResult<Vec<DeviceInfo>> {
if !self.initialised {
return Err(WootingAnalogResult::UnInitialized).into();
return Err(WootingAnalogResult::UnInitialized);
}

let mut devices = vec![];
for (_id, device) in self.devices.lock().unwrap().iter() {
devices.push(device.device_info.clone());
}

Ok(devices).into()
Ok(devices)
}
}

declare_plugin!(WootingPlugin, WootingPlugin::new);
declare_plugin!(WootingPlugin, WootingPlugin::new);
Loading