From 682591278f8e68b6bf06dacd4857cd7ee7c5ffa9 Mon Sep 17 00:00:00 2001 From: Cassandra de la Cruz-Munoz Date: Mon, 1 Jan 2024 13:30:27 -0500 Subject: [PATCH] type aliasing and create a few more constructors --- src/lib.rs | 117 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 37 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2d723ab..82661ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,6 +112,10 @@ pub enum MessageCreationErr { OptionalArgs(CreateOptionalArgsErr), } +type MsgNewResult = Result; + +type TraitMsgNewResult = Result, MessageCreationErr>; + #[derive(Debug)] pub struct VmcExtOk { loaded: i32, @@ -202,7 +206,7 @@ impl VmcMessage for VmcExtOk { } impl VmcExtOk { - pub fn new(loaded: i32, calibration_state: Option, calibration_mode: Option, tracking_status: Option) -> Result { + pub fn new(loaded: i32, calibration_state: Option, calibration_mode: Option, tracking_status: Option) -> MsgNewResult { if loaded < 0 || loaded > 1 { return Err(MessageCreationErr::IntRange(CreateIntRangeErr { expected_lower: 0, expected_upper: 1, actual: loaded, calling_func: String::from("VmcExtOk::new"), arg_name: String::from("loaded")})); } @@ -235,12 +239,12 @@ impl VmcExtOk { } return Ok(Self { loaded, calibration_state: None, calibration_mode: None, tracking_status: None}) } - pub fn new_vmc_message(loaded: i32, calibration_state: Option, calibration_mode: Option, tracking_status: Option) -> Result, MessageCreationErr> { + pub fn new_vmc_message(loaded: i32, calibration_state: Option, calibration_mode: Option, tracking_status: Option) -> TraitMsgNewResult { let msg = Self::new(loaded, calibration_state, calibration_mode, tracking_status); - if msg.is_ok() { - return Ok(Box::new(msg.ok().unwrap())); + match msg { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val) } - return Err(msg.err().unwrap()); } } @@ -273,15 +277,15 @@ impl VmcMessage for VmcExtT { } impl VmcExtT { - pub fn new(time: f32) -> Result { + pub fn new(time: f32) -> MsgNewResult { return Ok(Self { time }); } - pub fn new_vmc_message(time: f32) -> Result, MessageCreationErr> { + pub fn new_vmc_message(time: f32) -> TraitMsgNewResult { let msg = Self::new(time); - if msg.is_ok() { - return Ok(Box::new(msg.ok().unwrap())); + match msg { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val) } - return Err(msg.err().unwrap()); } } @@ -415,7 +419,7 @@ impl VmcMessage for VmcExtRootPos { } impl VmcExtRootPos { - pub fn new(name: String, transform: Transform3D, mr_scale: Option, mr_offset: Option) -> Result { + pub fn new(name: String, transform: Transform3D, mr_scale: Option, mr_offset: Option) -> MsgNewResult { if mr_scale.is_some() && mr_offset.is_none() { return Err(MessageCreationErr::OptionalArgs(CreateOptionalArgsErr { expected_sets: vec![String::from("None"), String::from("mr_scale, mr_offset")], actual: String::from("mr_scale"), calling_func: String::from("VmcExtRootPos::new")})); } @@ -424,12 +428,12 @@ impl VmcExtRootPos { } return Ok(Self { name, transform, mr_scale, mr_offset}); } - pub fn new_vmc_message(name: String, transform: Transform3D, mr_scale: Option, mr_offset: Option) -> Result, MessageCreationErr> { + pub fn new_vmc_message(name: String, transform: Transform3D, mr_scale: Option, mr_offset: Option) -> TraitMsgNewResult { let message = Self::new(name, transform, mr_scale, mr_offset); - if message.is_ok() { - return Ok(Box::new(message.ok().unwrap())); + match message { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val) } - return Err(message.err().unwrap()); } } @@ -511,15 +515,15 @@ impl VmcMessage for VmcExtBonePos { } impl VmcExtBonePos { - pub fn new(name: String, transform: Transform3D) -> Result { + pub fn new(name: String, transform: Transform3D) -> MsgNewResult { return Ok(Self{name, transform}); } - pub fn new_vmc_message(name: String, transform: Transform3D) -> Result, MessageCreationErr> { + pub fn new_vmc_message(name: String, transform: Transform3D) -> TraitMsgNewResult { let message = Self::new(name, transform); - if message.is_ok() { - return Ok(Box::new(message.ok().unwrap())); + match message { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val) } - return Err(message.err().unwrap()); } } @@ -561,15 +565,15 @@ impl VmcMessage for VmcExtBlendVal { } impl VmcExtBlendVal { - pub fn new(name: String, value: f32) -> Result { + pub fn new(name: String, value: f32) -> MsgNewResult { return Ok(Self{name, value}); } - pub fn new_vmc_message(name: String, value: f32) -> Result, MessageCreationErr> { + pub fn new_vmc_message(name: String, value: f32) -> TraitMsgNewResult { let message = Self::new(name, value); - if message.is_ok() { - return Ok(Box::new(message.ok().unwrap())); + match message { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val) } - return Err(message.err().unwrap()); } } @@ -657,12 +661,12 @@ impl VmcExtCam { pub fn new(name: String, transform: Transform3D, fov: f32) ->Result { return Ok(Self{name, transform, fov}) } - pub fn new_vmc_message(name: String, transform: Transform3D, fov: f32) -> Result, MessageCreationErr> { + pub fn new_vmc_message(name: String, transform: Transform3D, fov: f32) -> TraitMsgNewResult { let message = Self::new(name, transform, fov); - if message.is_ok() { - return Ok(Box::new(message.ok().unwrap())); + match message { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val) } - return Err(message.err().unwrap()); } } @@ -767,23 +771,23 @@ impl VmcMessage for VmcExtCon { } impl VmcExtCon { - pub fn new(active: i32, name: String, is_left: bool, is_touch: bool, axis: Vector3) -> Result { + pub fn new(active: i32, name: String, is_left: bool, is_touch: bool, axis: Vector3) -> MsgNewResult { if active > 2 || active < 0 { return Err(MessageCreationErr::IntRange(CreateIntRangeErr { expected_lower: 0, expected_upper: 2, actual: active, calling_func: String::from("VmcExtCon::new"), arg_name: String::from("active")})) } return Ok(Self{ active, name, is_left, is_touch, axis }); } - pub fn new_vmc_message(active: i32, name: String, is_left: bool, is_touch: bool, axis: Vector3) -> Result, MessageCreationErr> { + pub fn new_vmc_message(active: i32, name: String, is_left: bool, is_touch: bool, axis: Vector3) -> TraitMsgNewResult { let message = Self::new(active, name, is_left, is_touch, axis); - if message.is_ok() { - return Ok(Box::new(message.ok().unwrap())); + match message { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val) } - return Err(message.err().unwrap()); } } #[derive(Debug)] -struct VmcExtKey { +pub struct VmcExtKey { active: bool, name: String, keycode: i32, @@ -844,8 +848,21 @@ impl VmcMessage for VmcExtKey { } } +impl VmcExtKey { + pub fn new(active: bool, name: String, keycode: i32) -> MsgNewResult { + return Ok(Self { active, name, keycode}) + } + pub fn new_vmc_message(active: bool, name: String, keycode: i32) -> TraitMsgNewResult { + let message = Self::new(active, name, keycode); + match message { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val) + } + } +} + #[derive(Debug)] -struct VmcExtMidiNote { +pub struct VmcExtMidiNote { active: bool, channel: i32, note: i32, @@ -914,8 +931,21 @@ impl VmcMessage for VmcExtMidiNote { } } +impl VmcExtMidiNote { + pub fn new(active: bool, channel: i32, note: i32, velocity: f32) -> MsgNewResult { + return Ok(Self { active, channel, note, velocity}) + } + pub fn new_vmc_message(active: bool, channel: i32, note: i32, velocity: f32) -> TraitMsgNewResult { + let message = Self::new(active, channel, note, velocity); + match message { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val) + } + } +} + #[derive(Debug)] -struct VmcExtMidiCcVal { +pub struct VmcExtMidiCcVal { knob: i32, value: f32, } @@ -948,6 +978,19 @@ impl VmcMessage for VmcExtMidiCcVal { } } +impl VmcExtMidiCcVal { + pub fn new(knob: i32, value: f32) -> MsgNewResult { + return Ok(Self { knob, value}); + } + pub fn new_vmc_message(knob: i32, value: f32) -> TraitMsgNewResult { + let message = Self::new(knob, value); + match message { + Ok(val) => return Ok(Box::new(val)), + Err(val) => return Err(val), + } + } +} + #[derive(Debug)] struct VmcExtMidiCcBit { knob: i32,