Create IffData struct

This commit is contained in:
Serial 2021-07-28 13:46:04 -04:00
parent 438467f35e
commit e839a1012a
4 changed files with 32 additions and 37 deletions

View file

@ -1,3 +1,4 @@
use crate::components::logic::iff::IffData;
use crate::{FileProperties, LoftyError, Result};
use std::collections::HashMap;
@ -7,11 +8,6 @@ use std::time::Duration;
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
pub(crate) enum AiffMetadataType {
Id3(Vec<u8>),
TextChunks(HashMap<String, String>),
}
fn verify_aiff<R>(data: &mut R) -> Result<()>
where
R: Read + Seek,
@ -79,10 +75,7 @@ pub(crate) fn read_properties(comm: &mut &[u8], stream_len: u32) -> Result<FileP
))
}
pub(crate) fn read_from<R>(
data: &mut R,
expect_id3: bool,
) -> Result<(Option<AiffMetadataType>, FileProperties)>
pub(crate) fn read_from<R>(data: &mut R) -> Result<IffData>
where
R: Read + Seek,
{
@ -92,7 +85,7 @@ where
let mut stream_len = 0;
let mut metadata = HashMap::<String, String>::new();
let mut id3 = (false, Vec::new());
let mut id3 = Vec::new();
while let (Ok(fourcc), Ok(size)) = (
data.read_u32::<LittleEndian>(),
@ -114,7 +107,7 @@ where
let mut value = vec![0; size as usize];
data.read_exact(&mut value)?;
id3 = (true, value)
id3 = value
},
b"COMM" => {
if comm.is_none() {
@ -154,15 +147,13 @@ where
let properties = read_properties(&mut &*comm.unwrap(), stream_len)?;
let metadata = if id3.0 && expect_id3 {
Some(AiffMetadataType::Id3(id3.1))
} else if expect_id3 && !id3.0 {
None
} else {
Some(AiffMetadataType::TextChunks(metadata))
let metadata = IffData {
properties,
metadata,
id3: (!id3.is_empty()).then(|| id3),
};
Ok((metadata, properties))
Ok(metadata)
}
cfg_if::cfg_if! {

View file

@ -1,4 +1,14 @@
use crate::FileProperties;
use std::collections::HashMap;
#[cfg(any(feature = "format-aiff", feature = "format-id3"))]
pub(crate) mod aiff;
#[cfg(any(feature = "format-riff", feature = "format-id3"))]
pub(crate) mod riff;
pub(crate) struct IffData {
pub properties: FileProperties,
pub metadata: HashMap<String, String>,
pub id3: Option<Vec<u8>>,
}

View file

@ -1,5 +1,4 @@
use crate::components::logic::iff::aiff;
use crate::components::logic::iff::aiff::AiffMetadataType;
use crate::{
Album, AnyTag, AudioTag, AudioTagEdit, AudioTagWrite, FileProperties, Result, TagType, ToAny,
ToAnyTag,
@ -32,16 +31,15 @@ impl AiffTag {
where
R: Read + Seek,
{
let (data, properties) = aiff::read_from(reader, false)?;
let data = aiff::read_from(reader)?;
match data {
Some(AiffMetadataType::TextChunks(data)) => Ok(Self {
inner: AiffInnerTag { data },
properties,
_format: TagType::AiffText,
}),
_ => unreachable!(),
}
Ok(Self {
inner: AiffInnerTag {
data: data.metadata,
},
properties: data.properties,
_format: TagType::AiffText,
})
}
fn get_value(&self, key: &str) -> Option<&str> {

View file

@ -1,5 +1,4 @@
use crate::components::logic::iff;
use crate::components::logic::iff::aiff::AiffMetadataType;
use crate::components::logic::iff::aiff;
use crate::tag::Id3Format;
use crate::{
Album, AnyTag, AudioTag, AudioTagEdit, AudioTagWrite, FileProperties, LoftyError, MimeType,
@ -41,17 +40,14 @@ impl Id3v2Tag {
Id3v2InnerTag::read_from_wav_reader(reader)?,
),
Id3Format::Aiff => {
let (data, properties) = iff::aiff::read_from(reader, true)?;
let data = aiff::read_from(reader)?;
let inner = match data {
Some(AiffMetadataType::Id3(id3_data)) => {
Id3v2InnerTag::read_from(Cursor::new(id3_data))?
},
let inner = match data.id3 {
Some(id3) => Id3v2InnerTag::read_from(Cursor::new(id3))?,
None => Id3v2InnerTag::new(),
_ => unreachable!(),
};
(properties, inner)
(data.properties, inner)
},
};