use custom errors in VmcExtCon

This commit is contained in:
Cassandra de la Cruz-Munoz 2023-12-31 11:56:22 -05:00
parent a4b674c3d8
commit 0ce9ee167d
Signed by: cassdlcm
GPG Key ID: BFEBACEA812DDA70

View File

@ -479,9 +479,9 @@ impl MessageBehavior for VmcExtCon {
], ],
}; };
} }
fn from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 7 { if msg.args.len() != 7 {
return Err(String::from("arg count invalid")); return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![7], actual: msg.args.len(), addr: msg.addr}));
} }
let active: i32; let active: i32;
let name: String; let name: String;
@ -490,40 +490,40 @@ impl MessageBehavior for VmcExtCon {
let mut axis = Vector3::new(0.0, 0.0, 0.0); let mut axis = Vector3::new(0.0, 0.0, 0.0);
match msg.args[0] { match msg.args[0] {
OscType::Int(i) => active = i, OscType::Int(i) => active = i,
_ => return Err(String::from("arg type invalid")), _ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Int(0), actual: msg.args[0], addr: msg.addr, arg_count: 0})),
} }
if active > 2 || active < 0 { if active > 2 || active < 0 {
return Err(String::from("arg value invalid")); return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 2, actual: active, addr: msg.addr, arg_count: 0}));
} }
match &msg.args[1] { match &msg.args[1] {
OscType::String(s) => name = s.to_owned(), OscType::String(s) => name = s.to_owned(),
_ => return Err(String::from("arg value invalid")), _ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::String(String::new()), actual: msg.args[1], addr: msg.addr, arg_count: 1})),
} }
match msg.args[2] { match msg.args[2] {
OscType::Int(i) => is_left = i, OscType::Int(i) => is_left = i,
_ => return Err(String::from("arg type invalid")), _ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Int(0), actual: msg.args[2], addr: msg.addr, arg_count: 2})),
} }
if is_left < 0 || is_left > 1 { if is_left < 0 || is_left > 1 {
return Err(String::from("arg value invalid")); return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: active, addr: msg.addr, arg_count: 2}));
} }
match msg.args[3] { match msg.args[3] {
OscType::Int(i) => is_touch = i, OscType::Int(i) => is_touch = i,
_ => return Err(String::from("arg type invalid")), _ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Int(0), actual: msg.args[3], addr: msg.addr, arg_count: 3})),
} }
if is_touch < 0 || is_touch > 1 { if is_touch < 0 || is_touch > 1 {
return Err(String::from("arg value invalid")); return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: active, addr: msg.addr, arg_count: 3}));
} }
match msg.args[4] { match msg.args[4] {
OscType::Float(f) => axis.x = f, OscType::Float(f) => axis.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] { match msg.args[5] {
OscType::Float(f) => axis.y = f, OscType::Float(f) => axis.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] { match msg.args[6] {
OscType::Float(f) => axis.z = f, OscType::Float(f) => axis.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})),
} }
return Ok(VmcExtCon { return Ok(VmcExtCon {
active, active,