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 {
pub fn from_u8(byte: u8) -> Self {
pub fn from_u8(byte: u8) -> Option<Self> {
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,
}
}
}

View File

@@ -131,7 +131,10 @@ impl Mixer {
let mut buf: Vec<u8> = 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"),
}