feat: handle invalid instructions

This commit is contained in:
2024-06-29 00:11:31 +02:00
parent 1f48737b2b
commit 8c77ffbf6e
2 changed files with 13 additions and 10 deletions

View File

@@ -10,16 +10,16 @@ pub enum MixerInstruction {
} }
impl MixerInstruction { impl MixerInstruction {
pub fn from_u8(byte: u8) -> Self { pub fn from_u8(byte: u8) -> Option<Self> {
match byte { match byte {
0 => MixerInstruction::SelectNext, 0 => Some(MixerInstruction::SelectNext),
1 => MixerInstruction::SelectPrevious, 1 => Some(MixerInstruction::SelectPrevious),
2 => MixerInstruction::ToggleMuteCurrent, 2 => Some(MixerInstruction::ToggleMuteCurrent),
3 => MixerInstruction::IncreaseCurrent, 3 => Some(MixerInstruction::IncreaseCurrent),
4 => MixerInstruction::DecreaseCurrent, 4 => Some(MixerInstruction::DecreaseCurrent),
5 => MixerInstruction::GetCurrent, 5 => Some(MixerInstruction::GetCurrent),
6 => MixerInstruction::PlayPauseCurrent, 6 => Some(MixerInstruction::PlayPauseCurrent),
_ => panic!("Could not parse '{byte}' to MixerInstruction"), _ => None,
} }
} }
} }

View File

@@ -131,7 +131,10 @@ impl Mixer {
let mut buf: Vec<u8> = Vec::with_capacity(1); let mut buf: Vec<u8> = Vec::with_capacity(1);
stream.read_to_end(&mut buf).expect("Error reading stream"); 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"), Err(_) => println!("Stream error"),
} }