custom message parsing errors #1

Merged
cassdlcm merged 22 commits from custom_errors into main 2023-12-31 19:36:26 +01:00
Showing only changes of commit 2ba4cce043 - Show all commits

View File

@ -6,11 +6,54 @@ use rosc::{OscMessage, OscType};
pub trait MessageBehavior {
fn to_osc_message(&self) -> OscMessage;
fn from_osc_message(msg: OscMessage) -> Result<Self, String>
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr>
where
Self: Sized;
}
struct ArgCountErr {
expected_in: Vec<usize>,
actual: usize,
addr: String,
}
struct ArgTypeErr {
expected: OscType,
actual: OscType,
addr: String,
arg_count: usize,
}
struct IntRangeErr {
expected_lower: i32,
expected_upper: i32,
actual: i32,
addr: String,
arg_count: usize,
}
struct IntValErr {
expected_in: Vec<i32>,
actual: i32,
addr: String,
arg_count: usize,
}
struct StrValErr {
expected_in: Vec<String>,
actual: String,
addr: String,
arg_count: usize,
}
enum FromMessageErr {
ArgCount(ArgCountErr),
ArgType(ArgTypeErr),
IntRange(IntRangeErr),
IntVal(IntValErr),
StrVal(StrValErr),
}
struct VmcExtOk {
loaded: i32,
calibration_state: Option<i32>,
@ -33,7 +76,7 @@ impl MessageBehavior for VmcExtOk {
}
return OscMessage { addr, args };
}
fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
let mut result = VmcExtOk {
loaded: -1,
calibration_state: None,
@ -46,11 +89,11 @@ impl MessageBehavior for VmcExtOk {
result.loaded = i;
}
_ => {
return Err(String::from("arg type invalid"));
return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Int(0), actual: msg.args[0],addr: msg.addr.to_owned(), arg_count: 0}));
}
}
} else {
return Err(String::from("arg count invalid"));
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1, 3, 4], actual: msg.args.len(), addr: msg.addr.to_owned()}));
}
if msg.args.len() == 3 || msg.args.len() == 4 {
match msg.args[1] {
@ -58,7 +101,7 @@ impl MessageBehavior for VmcExtOk {
result.calibration_state = Some(i);
}
_ => {
return Err(String::from("arg type invalid"));
return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Int(0), actual: msg.args[1], addr: msg.addr.to_owned(), arg_count: 1}));
}
}
match msg.args[2] {
@ -66,7 +109,7 @@ impl MessageBehavior for VmcExtOk {
result.calibration_mode = Some(i);
}
_ => {
return Err(String::from("arg type invalid"));
return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Int(0), actual: msg.args[2], addr: msg.addr.to_owned(), arg_count: 2}));
}
}
}
@ -76,7 +119,7 @@ impl MessageBehavior for VmcExtOk {
result.tracking_status = Some(i);
}
_ => {
return Err(String::from("arg type invalid"));
return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Int(0), actual: msg.args[3], addr: msg.addr.to_owned(), arg_count: 3}));
}
}
}
@ -95,13 +138,13 @@ impl MessageBehavior for VmcExtT {
args: vec![OscType::from(self.time)],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 1 {
return Err(String::from("arg count invalid"));
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr}));
}
match msg.args[0] {
OscType::Float(f) => return Ok(VmcExtT { time: f }),
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[0], addr: msg.addr, arg_count: 0})),
}
}
}
@ -139,26 +182,26 @@ impl MessageBehavior for VmcExtRootPos {
}
return OscMessage { addr, args };
}
fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
let mut result: VmcExtRootPos;
if msg.args.len() == 8 || msg.args.len() == 14 {
let mut origin = Vector3::new(0.0, 0.0, 0.0);
let name: String;
match &msg.args[0] {
OscType::String(s) => name = s.to_owned(),
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::String(String::new()), actual: msg.args[0], addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Float(f) => origin.x = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[1], addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Float(f) => origin.y = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[2], addr: msg.addr, arg_count: 2})),
}
match msg.args[3] {
OscType::Float(f) => origin.z = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[3], addr: msg.addr, arg_count: 3})),
}
let mut quat = Quaternion {
x: 0.0,
@ -168,19 +211,19 @@ impl MessageBehavior for VmcExtRootPos {
};
match msg.args[4] {
OscType::Float(f) => quat.x = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[4], addr: msg.addr, arg_count: 4})),
}
match msg.args[5] {
OscType::Float(f) => quat.y = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[5], addr: msg.addr, arg_count: 5})),
}
match msg.args[6] {
OscType::Float(f) => quat.z = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[6], addr: msg.addr, arg_count: 6})),
}
match msg.args[7] {
OscType::Float(f) => quat.w = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[7], addr: msg.addr, arg_count: 7})),
}
result = VmcExtRootPos {
name,
@ -189,7 +232,7 @@ impl MessageBehavior for VmcExtRootPos {
mr_offset: None,
};
} else {
return Err(String::from("arg count invalid"));
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![8, 14], actual: msg.args.len(), addr: msg.addr}));
}
if msg.args.len() == 14 {
result.mr_scale = Some(Vector3 {
@ -204,27 +247,27 @@ impl MessageBehavior for VmcExtRootPos {
});
match msg.args[8] {
OscType::Float(f) => result.mr_scale.unwrap().x = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[8], addr: msg.addr, arg_count: 8})),
}
match msg.args[9] {
OscType::Float(f) => result.mr_scale.unwrap().y = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[9], addr: msg.addr, arg_count: 9})),
}
match msg.args[10] {
OscType::Float(f) => result.mr_scale.unwrap().z = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[10], addr: msg.addr, arg_count: 10})),
}
match msg.args[11] {
OscType::Float(f) => result.mr_offset.unwrap().x = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[11], addr: msg.addr, arg_count: 11})),
}
match msg.args[12] {
OscType::Float(f) => result.mr_offset.unwrap().y = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[12], addr: msg.addr, arg_count: 12})),
}
match msg.args[13] {
OscType::Float(f) => result.mr_offset.unwrap().z = f,
_ => return Err(String::from("arg type invalid")),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Float(0.0), actual: msg.args[13], addr: msg.addr, arg_count: 13})),
}
}
return Ok(result);
@ -1310,3 +1353,22 @@ impl MessageBehavior for VmcExtSetEye {
return Ok(VmcExtSetEye { enable, position: Vector3::new(x, y, z)})
}
}
struct VmcExtSetRes {
response: String
}
impl MessageBehavior for VmcExtSetRes {
fn to_osc_message(&self) -> OscMessage {
return OscMessage{addr: String::from("/VMC/Ext/Set/Res"), args: vec![OscType::from(self.response.to_owned())]};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 1 {
return Err(String::from("arg count invalid"));
}
match &msg.args[0] {
OscType::String(s) => return Ok(VmcExtSetRes{response: s.to_owned()}),
_ => return Err(String::from("arg type invalid"))
}
}
}