use custom errors in VmcExtMidiNote

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

View File

@ -618,9 +618,9 @@ impl MessageBehavior for VmcExtMidiNote {
args, args,
}; };
} }
fn from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 4 { if msg.args.len() != 4 {
return Err(String::from("arg count invalid")); return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![4], actual: msg.args.len(), addr: msg.addr}));
} }
let active: bool; let active: bool;
let channel: i32; let channel: i32;
@ -633,22 +633,22 @@ impl MessageBehavior for VmcExtMidiNote {
} 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::Int(i) => channel = i, OscType::Int(i) => channel = i,
_ => return Err(String::from("arg type invalid")), _ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: OscType::Int(0), actual: msg.args[1], addr: msg.addr, arg_count: 1})),
} }
match msg.args[2] { match msg.args[2] {
OscType::Int(i) => note = i, OscType::Int(i) => note = 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})),
} }
match msg.args[3] { match msg.args[3] {
OscType::Float(f) => velocity = f, OscType::Float(f) => velocity = 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})),
} }
return Ok(VmcExtMidiNote { return Ok(VmcExtMidiNote {
active, active,