From 8c77ffbf6e9491edcc8ca6b786037e2b15b37369 Mon Sep 17 00:00:00 2001 From: 409 Date: Sat, 29 Jun 2024 00:11:31 +0200 Subject: [PATCH] feat: handle invalid instructions --- src/instructions.rs | 18 +++++++++--------- src/mixer.rs | 5 ++++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/instructions.rs b/src/instructions.rs index 1aacef1..b6d96cd 100644 --- a/src/instructions.rs +++ b/src/instructions.rs @@ -10,16 +10,16 @@ pub enum MixerInstruction { } impl MixerInstruction { - pub fn from_u8(byte: u8) -> Self { + pub fn from_u8(byte: u8) -> Option { match byte { - 0 => MixerInstruction::SelectNext, - 1 => MixerInstruction::SelectPrevious, - 2 => MixerInstruction::ToggleMuteCurrent, - 3 => MixerInstruction::IncreaseCurrent, - 4 => MixerInstruction::DecreaseCurrent, - 5 => MixerInstruction::GetCurrent, - 6 => MixerInstruction::PlayPauseCurrent, - _ => panic!("Could not parse '{byte}' to MixerInstruction"), + 0 => Some(MixerInstruction::SelectNext), + 1 => Some(MixerInstruction::SelectPrevious), + 2 => Some(MixerInstruction::ToggleMuteCurrent), + 3 => Some(MixerInstruction::IncreaseCurrent), + 4 => Some(MixerInstruction::DecreaseCurrent), + 5 => Some(MixerInstruction::GetCurrent), + 6 => Some(MixerInstruction::PlayPauseCurrent), + _ => None, } } } diff --git a/src/mixer.rs b/src/mixer.rs index 91698e6..9397731 100644 --- a/src/mixer.rs +++ b/src/mixer.rs @@ -131,7 +131,10 @@ impl Mixer { let mut buf: Vec = Vec::with_capacity(1); stream.read_to_end(&mut buf).expect("Error reading stream"); - mixer_tx.send(MixerInstruction::from_u8(buf[0])).unwrap(); + match MixerInstruction::from_u8(buf[0]) { + Some(ix) => mixer_tx.send(ix).unwrap(), + None => println!("Invalid instruction: {}", buf[0]), + } } Err(_) => println!("Stream error"), }