From 511aa597b2b6873a127cd2856e4bd54753d338d8 Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Sat, 17 Apr 2021 17:09:32 -0400 Subject: [PATCH] Would make sense to stop reading when the end is reached :) Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com> --- src/components/logic/read.rs | 18 ++++++++++++------ src/components/tags/wav_tag.rs | 6 +++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/components/logic/read.rs b/src/components/logic/read.rs index 58345437..e6fd50c0 100644 --- a/src/components/logic/read.rs +++ b/src/components/logic/read.rs @@ -50,7 +50,9 @@ where let chunk_len = list.len(); let mut metadata: HashMap = HashMap::with_capacity(chunk_len as usize); - for _ in 0..chunk_len { + let mut reading = true; + + while reading { let fourcc = cursor.read_u32::()? as u32; let size = cursor.read_u32::()? as u32; @@ -61,14 +63,18 @@ where let val = std::str::from_utf8(&*buf)?; metadata.insert(key, val.trim_matches(char::from(0)).to_string()); - - // Skip null byte - if size as usize % 2 != 0 { - cursor.set_position(cursor.position() + 1) - } }, None => cursor.set_position(cursor.position() + u64::from(size)), } + + // Skip null byte + if size as usize % 2 != 0 { + cursor.set_position(cursor.position() + 1) + } + + if cursor.position() >= cursor.get_ref().len() as u64 { + reading = false + } } Ok(Some(metadata)) diff --git a/src/components/tags/wav_tag.rs b/src/components/tags/wav_tag.rs index 451e0fae..338440a5 100644 --- a/src/components/tags/wav_tag.rs +++ b/src/components/tags/wav_tag.rs @@ -1,6 +1,6 @@ use crate::{ - impl_tag, traits::ReadPath, Album, AnyTag, AudioTag, AudioTagEdit, AudioTagWrite, - Picture, Result, TagType, ToAny, ToAnyTag, + impl_tag, traits::ReadPath, Album, AnyTag, AudioTag, AudioTagEdit, AudioTagWrite, Picture, + Result, TagType, ToAny, ToAnyTag, components::logic }; use std::{collections::HashMap, fs::File, path::Path}; @@ -15,7 +15,7 @@ impl ReadPath for WavInnerTag { P: AsRef, Self: Sized, { - let data = crate::components::logic::read::wav(File::open(path)?)?; + let data = logic::read::wav(File::open(path)?)?; Ok(Self { data }) }