From 40a9447fa1e49ddbaac508a00567acbaa3bfd96b Mon Sep 17 00:00:00 2001 From: dvdsk Date: Fri, 5 Apr 2024 13:28:16 +0200 Subject: [PATCH] refactor(decoder/sympthonia) use for loop instead loop + match & escape More readable, prep for using the same lines in new refine_position fn. Since that is already pretty complex this needed to be made simpler. This code retries on all errors not only decode errors. Retries will not work on anything else then a decode error however they also wont cause any problems. Not checking the result does however make the code simpler. --- src/decoder/symphonia.rs | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/decoder/symphonia.rs b/src/decoder/symphonia.rs index 4903fa2..98ec5af 100644 --- a/src/decoder/symphonia.rs +++ b/src/decoder/symphonia.rs @@ -192,23 +192,16 @@ impl Iterator for SymphoniaDecoder { #[inline] fn next(&mut self) -> Option { - if self.current_frame_offset == self.buffer.len() { - let mut decode_errors: usize = 0; - let decoded = loop { - let packet = self.format.next_packet().ok()?; - match self.decoder.decode(&packet) { - Ok(decoded) => break decoded, - Err(Error::DecodeError(_)) => { - decode_errors += 1; - if decode_errors > MAX_DECODE_ERRORS { - return None; - } else { - continue; - } - } - Err(_) => return None, - }; - }; + if self.current_frame_offset >= self.buffer.len() { + let packet = self.format.next_packet().ok()?; + let mut decoded = self.decoder.decode(&packet); + for _ in 0..MAX_DECODE_RETRIES { + if decoded.is_err() { + let packet = self.format.next_packet().ok()?; + decoded = self.decoder.decode(&packet); + } + } + let decoded = decoded.ok()?; self.spec = decoded.spec().to_owned(); self.buffer = SymphoniaDecoder::get_buffer(decoded, &self.spec); self.current_frame_offset = 0;