use custom errors in VmcExtKey

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

View File

@ -558,9 +558,9 @@ impl MessageBehavior for VmcExtKey {
args, args,
}; };
} }
fn from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 3 { if msg.args.len() != 3 {
return Err(String::from("arg count invalid")); return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![3], actual: msg.args.len(), addr: msg.addr}));
} }
let active: bool; let active: bool;
let name: String; let name: String;
@ -572,18 +572,18 @@ impl MessageBehavior for VmcExtKey {
} else if i == 0 { } else if i == 0 {
active = false; active = false;
} else { } else {
return Err(String::from("arg value invalid")); return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 0}));
} }
} }
_ => 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})),
} }
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 type 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) => keycode = i, OscType::Int(i) => keycode = 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})),
} }
return Ok(VmcExtKey { return Ok(VmcExtKey {
active, active,