Remove unwraps; Stop taking references to Copy types

This commit is contained in:
Serial 2022-02-19 10:14:14 -05:00
parent 55cc5e5264
commit e01d875e05
No known key found for this signature in database
GPG key ID: DA95198DC17C4568
26 changed files with 102 additions and 99 deletions

View file

@ -24,7 +24,7 @@ fn main() {
let tag_type = tag.tag_type();
println!("{}: {:?}", num, tag_type);
available_tag_types.push(*tag_type);
available_tag_types.push(tag_type);
}
let mut to_remove = None;

View file

@ -98,7 +98,7 @@ impl AudioFile for ApeFile {
false
}
fn contains_tag_type(&self, tag_type: &TagType) -> bool {
fn contains_tag_type(&self, tag_type: TagType) -> bool {
match tag_type {
#[cfg(feature = "ape")]
TagType::Ape => self.ape_tag.is_some(),

View file

@ -137,7 +137,7 @@ pub(crate) fn utf16_decode(reader: &[u8], endianness: fn([u8; 2]) -> u16) -> Res
.chunks_exact(2)
.map_while(|c| match c {
[0, 0] => None,
_ => Some(endianness(c.try_into().unwrap())),
_ => Some(endianness(c.try_into().unwrap())), // Infallible
})
.collect();

View file

@ -73,7 +73,7 @@ impl AudioFile for AiffFile {
false
}
fn contains_tag_type(&self, tag_type: &TagType) -> bool {
fn contains_tag_type(&self, tag_type: TagType) -> bool {
match tag_type {
#[cfg(feature = "id3v2")]
TagType::Id3v2 => self.id3v2_tag.is_some(),

View file

@ -23,7 +23,9 @@ where
return Err(LoftyError::new(ErrorKind::UnknownFormat));
}
Ok(u32::from_be_bytes(id[4..8].try_into().unwrap()))
Ok(u32::from_be_bytes(
id[4..8].try_into().unwrap(), // Infallible
))
}
pub(crate) fn read_from<R>(data: &mut R, read_properties: bool) -> Result<AiffFile>
@ -120,15 +122,10 @@ where
}
}
let properties = if read_properties {
if comm.is_none() {
return Err(FileDecodingError::new(
FileType::AIFF,
"File does not contain a \"COMM\" chunk",
)
.into());
}
let properties;
if read_properties {
match comm {
Some(comm) => {
if stream_len == 0 {
return Err(FileDecodingError::new(
FileType::AIFF,
@ -137,13 +134,22 @@ where
.into());
}
super::properties::read_properties(
&mut &*comm.unwrap(),
properties = super::properties::read_properties(
&mut &*comm,
stream_len,
data.seek(SeekFrom::Current(0))?,
)?
)?;
},
None => {
return Err(FileDecodingError::new(
FileType::AIFF,
"File does not contain a \"COMM\" chunk",
)
.into());
},
}
} else {
FileProperties::default()
properties = FileProperties::default();
};
Ok(AiffFile {

View file

@ -383,7 +383,7 @@ where
chunks_remove.sort_unstable();
chunks_remove.reverse();
let first = chunks_remove.pop().unwrap();
let first = chunks_remove.pop().unwrap(); // Infallible
for (s, e) in &chunks_remove {
file_bytes.drain(*s as usize..*e as usize);

View file

@ -77,7 +77,7 @@ impl AudioFile for WavFile {
false
}
fn contains_tag_type(&self, tag_type: &TagType) -> bool {
fn contains_tag_type(&self, tag_type: TagType) -> bool {
match tag_type {
#[cfg(feature = "id3v2")]
TagType::Id3v2 => self.id3v2_tag.is_some(),

View file

@ -31,7 +31,9 @@ where
);
}
Ok(u32::from_le_bytes(id[4..8].try_into().unwrap()))
Ok(u32::from_le_bytes(
id[4..8].try_into().unwrap(), // Infallible
))
}
pub(crate) fn read_from<R>(data: &mut R, read_properties: bool) -> Result<WavFile>

View file

@ -76,11 +76,11 @@
//! let tagged_file = read_from_path("tests/files/assets/minimal/full_test.mp3", false)?;
//!
//! // Get the primary tag (ID3v2 in this case)
//! let id3v2 = tagged_file.primary_tag().unwrap();
//! let id3v2 = tagged_file.primary_tag();
//!
//! // If the primary tag doesn't exist, or the tag types
//! // don't matter, the first tag can be retrieved
//! let unknown_first_tag = tagged_file.first_tag().unwrap();
//! let unknown_first_tag = tagged_file.first_tag();
//! # Ok(())
//! # }
//! ```
@ -102,8 +102,8 @@
//! assert_eq!(mpeg_file.properties().channels(), 2);
//!
//! // Here we have a file with multiple tags
//! assert!(mpeg_file.contains_tag_type(&TagType::Id3v2));
//! assert!(mpeg_file.contains_tag_type(&TagType::Ape));
//! assert!(mpeg_file.contains_tag_type(TagType::Id3v2));
//! assert!(mpeg_file.contains_tag_type(TagType::Ape));
//! # Ok(())
//! # }
//! ```

View file

@ -85,7 +85,7 @@ impl AudioFile for Mp3File {
false
}
fn contains_tag_type(&self, tag_type: &TagType) -> bool {
fn contains_tag_type(&self, tag_type: TagType) -> bool {
match tag_type {
#[cfg(feature = "ape")]
TagType::Ape => self.ape_tag.is_some(),

View file

@ -83,7 +83,7 @@ where
},
// Tags might be followed by junk bytes before the first MP3 frame begins
_ => {
// seek back the length of the temporary header buffer, to include them
// Seek back the length of the temporary header buffer, to include them
// in the frame sync search
#[allow(clippy::neg_multiply)]
let start_of_search_area = reader.seek(SeekFrom::Current(-1 * header.len() as i64))?;
@ -102,7 +102,8 @@ where
// We have found the first frame
break;
}
// the search for sync bits was unsuccessful
// The search for sync bits was unsuccessful
return Err(FileDecodingError::new(
FileType::MP3,
"File contains an invalid frame",

View file

@ -89,9 +89,9 @@ impl AudioFile for Mp4File {
}
#[allow(unreachable_code, unused_variables)]
fn contains_tag_type(&self, tag_type: &TagType) -> bool {
fn contains_tag_type(&self, tag_type: TagType) -> bool {
#[cfg(feature = "mp4_ilst")]
return tag_type == &TagType::Mp4Ilst && self.ilst.is_some();
return tag_type == TagType::Mp4Ilst && self.ilst.is_some();
false
}

View file

@ -23,22 +23,22 @@ impl Moov {
where
R: Read + Seek,
{
let mut moov = (false, None);
let mut moov = None;
while let Ok(atom) = AtomInfo::read(data) {
if atom.ident == AtomIdent::Fourcc(*b"moov") {
moov = (true, Some(atom));
moov = Some(atom);
break;
}
skip_unneeded(data, atom.extended, atom.len)?;
}
if !moov.0 {
return Err(FileDecodingError::new(FileType::MP4, "No \"moov\" atom found").into());
if let Some(moov) = moov {
Ok(moov)
} else {
Err(FileDecodingError::new(FileType::MP4, "No \"moov\" atom found").into())
}
Ok(moov.1.unwrap())
}
pub(crate) fn parse<R>(data: &mut R, read_properties: bool) -> Result<Self>

View file

@ -62,9 +62,9 @@ impl AudioFile for FlacFile {
}
#[allow(unused_variables)]
fn contains_tag_type(&self, tag_type: &TagType) -> bool {
fn contains_tag_type(&self, tag_type: TagType) -> bool {
#[cfg(feature = "vorbis_comments")]
return tag_type == &TagType::VorbisComments && self.vorbis_comments.is_some();
return tag_type == TagType::VorbisComments && self.vorbis_comments.is_some();
#[cfg(not(feature = "vorbis_comments"))]
return false;

View file

@ -96,7 +96,7 @@ where
blocks_remove.sort_unstable();
blocks_remove.reverse();
let first = blocks_remove.pop().unwrap();
let first = blocks_remove.pop().unwrap(); // Infallible
for (s, e) in &blocks_remove {
file_bytes.drain(*s as usize..*e as usize);

View file

@ -61,8 +61,8 @@ impl AudioFile for OpusFile {
true
}
fn contains_tag_type(&self, tag_type: &TagType) -> bool {
tag_type == &TagType::VorbisComments
fn contains_tag_type(&self, tag_type: TagType) -> bool {
tag_type == TagType::VorbisComments
}
}

View file

@ -60,8 +60,8 @@ impl AudioFile for SpeexFile {
true
}
fn contains_tag_type(&self, tag_type: &TagType) -> bool {
tag_type == &TagType::VorbisComments
fn contains_tag_type(&self, tag_type: TagType) -> bool {
tag_type == TagType::VorbisComments
}
}

View file

@ -64,8 +64,8 @@ impl AudioFile for VorbisFile {
true
}
fn contains_tag_type(&self, tag_type: &TagType) -> bool {
tag_type == &TagType::VorbisComments
fn contains_tag_type(&self, tag_type: TagType) -> bool {
tag_type == TagType::VorbisComments
}
}

View file

@ -30,7 +30,7 @@ pub trait AudioFile: Into<TaggedFile> {
/// Checks if the file contains any tags
fn contains_tag(&self) -> bool;
/// Checks if the file contains the given [`TagType`]
fn contains_tag_type(&self, tag_type: &TagType) -> bool;
fn contains_tag_type(&self, tag_type: TagType) -> bool;
}
/// A generic representation of a file
@ -47,8 +47,8 @@ pub struct TaggedFile {
impl TaggedFile {
/// Returns the file's [`FileType`]
pub fn file_type(&self) -> &FileType {
&self.ty
pub fn file_type(&self) -> FileType {
self.ty
}
/// Returns the file's [`FileProperties`]
@ -70,17 +70,17 @@ impl TaggedFile {
/// Determines whether the file supports the given [`TagType`]
pub fn supports_tag_type(&self, tag_type: TagType) -> bool {
self.ty.supports_tag_type(&tag_type)
self.ty.supports_tag_type(tag_type)
}
/// Get a reference to a specific [`TagType`]
pub fn tag(&self, tag_type: &TagType) -> Option<&Tag> {
self.tags.iter().find(|i| i.tag_type() == tag_type)
self.tags.iter().find(|i| i.tag_type() == *tag_type)
}
/// Get a mutable reference to a specific [`TagType`]
pub fn tag_mut(&mut self, tag_type: &TagType) -> Option<&mut Tag> {
self.tags.iter_mut().find(|i| i.tag_type() == tag_type)
self.tags.iter_mut().find(|i| i.tag_type() == *tag_type)
}
/// Returns the primary tag
@ -114,7 +114,7 @@ impl TaggedFile {
///
/// If a tag is replaced, it will be returned
pub fn insert_tag(&mut self, tag: Tag) -> Option<Tag> {
let tag_type = *tag.tag_type();
let tag_type = tag.tag_type();
if self.supports_tag_type(tag_type) {
let ret = self.remove_tag(tag_type);
@ -132,7 +132,7 @@ impl TaggedFile {
pub fn remove_tag(&mut self, tag_type: TagType) -> Option<Tag> {
self.tags
.iter()
.position(|t| t.tag_type() == &tag_type)
.position(|t| t.tag_type() == tag_type)
.map(|pos| self.tags.remove(pos))
}
@ -225,28 +225,28 @@ impl FileType {
}
/// Returns if the target `FileType` supports a [`TagType`]
pub fn supports_tag_type(&self, tag_type: &TagType) -> bool {
pub fn supports_tag_type(&self, tag_type: TagType) -> bool {
match self {
#[cfg(feature = "id3v2")]
FileType::AIFF | FileType::APE | FileType::MP3 | FileType::WAV
if tag_type == &TagType::Id3v2 =>
if tag_type == TagType::Id3v2 =>
{
true
},
#[cfg(feature = "aiff_text_chunks")]
FileType::AIFF if tag_type == &TagType::AiffText => true,
FileType::AIFF if tag_type == TagType::AiffText => true,
#[cfg(feature = "id3v1")]
FileType::APE | FileType::MP3 if tag_type == &TagType::Id3v1 => true,
FileType::APE | FileType::MP3 if tag_type == TagType::Id3v1 => true,
#[cfg(feature = "ape")]
FileType::APE | FileType::MP3 if tag_type == &TagType::Ape => true,
FileType::APE | FileType::MP3 if tag_type == TagType::Ape => true,
#[cfg(feature = "vorbis_comments")]
FileType::Opus | FileType::FLAC | FileType::Vorbis | FileType::Speex => {
tag_type == &TagType::VorbisComments
tag_type == TagType::VorbisComments
},
#[cfg(feature = "mp4_ilst")]
FileType::MP4 => tag_type == &TagType::Mp4Ilst,
FileType::MP4 => tag_type == TagType::Mp4Ilst,
#[cfg(feature = "riff_info_list")]
FileType::WAV => tag_type == &TagType::RiffInfo,
FileType::WAV => tag_type == TagType::RiffInfo,
_ => false,
}
}

View file

@ -130,8 +130,8 @@ impl Tag {
}
/// Returns the [`TagType`]
pub fn tag_type(&self) -> &TagType {
&self.tag_type
pub fn tag_type(&self) -> TagType {
self.tag_type
}
/// Returns the number of [`TagItem`]s
@ -440,7 +440,7 @@ impl TagType {
None => return Err(LoftyError::new(ErrorKind::UnknownFormat)),
};
if !file_type.supports_tag_type(self) {
if !file_type.supports_tag_type(*self) {
return Err(LoftyError::new(ErrorKind::UnsupportedTag));
}

View file

@ -7,7 +7,7 @@ fn read() {
// Here we have an AIFF file with both an ID3v2 chunk and text chunks
let file = lofty::read_from_path("tests/files/assets/minimal/full_test.aiff", false).unwrap();
assert_eq!(file.file_type(), &FileType::AIFF);
assert_eq!(file.file_type(), FileType::AIFF);
// Verify the ID3v2 tag first
crate::verify_artist!(file, primary_tag, "Foo artist", 1);
@ -22,7 +22,7 @@ fn write() {
let mut tagged_file = lofty::read_from(&mut file, false).unwrap();
assert_eq!(tagged_file.file_type(), &FileType::AIFF);
assert_eq!(tagged_file.file_type(), FileType::AIFF);
// ID3v2
crate::set_artist!(tagged_file, primary_tag_mut, "Foo artist", 1 => file, "Bar artist");

View file

@ -7,7 +7,7 @@ fn read() {
// Here we have an APE file with an ID3v2, ID3v1, and an APEv2 tag
let file = lofty::read_from_path("tests/files/assets/minimal/full_test.ape", false).unwrap();
assert_eq!(file.file_type(), &FileType::APE);
assert_eq!(file.file_type(), FileType::APE);
// Verify the APEv2 tag first
crate::verify_artist!(file, primary_tag, "Foo artist", 1);
@ -26,7 +26,7 @@ fn write() {
let mut tagged_file = lofty::read_from(&mut file, false).unwrap();
assert_eq!(tagged_file.file_type(), &FileType::APE);
assert_eq!(tagged_file.file_type(), FileType::APE);
// APEv2
crate::set_artist!(tagged_file, primary_tag_mut, "Foo artist", 1 => file, "Bar artist");

View file

@ -8,7 +8,7 @@ fn read() {
let file =
lofty::read_from_path("tests/files/assets/minimal/m4a_codec_aac.m4a", false).unwrap();
assert_eq!(file.file_type(), &FileType::MP4);
assert_eq!(file.file_type(), FileType::MP4);
// Verify the ilst tag
crate::verify_artist!(file, primary_tag, "Foo artist", 1);
@ -20,7 +20,7 @@ fn write() {
let mut tagged_file = lofty::read_from(&mut file, false).unwrap();
assert_eq!(tagged_file.file_type(), &FileType::MP4);
assert_eq!(tagged_file.file_type(), FileType::MP4);
// ilst
crate::set_artist!(tagged_file, tag_mut, TagType::Mp4Ilst, "Foo artist", 1 => file, "Bar artist");

View file

@ -7,7 +7,7 @@ fn read() {
// Here we have an MP3 file with an ID3v2, ID3v1, and an APEv2 tag
let file = lofty::read_from_path("tests/files/assets/minimal/full_test.mp3", false).unwrap();
assert_eq!(file.file_type(), &FileType::MP3);
assert_eq!(file.file_type(), FileType::MP3);
// Verify the ID3v2 tag first
crate::verify_artist!(file, primary_tag, "Foo artist", 1);
@ -26,7 +26,7 @@ fn read_with_junk_bytes_between_frames() {
lofty::read_from_path("tests/files/assets/junk_between_id3_and_mp3.mp3", true).unwrap();
// note that the file contains ID3v2 and ID3v1 data
assert_eq!(file.file_type(), &FileType::MP3);
assert_eq!(file.file_type(), FileType::MP3);
let id3v2_tag = &file.tags()[0];
assert_eq!(id3v2_tag.artist(), Some("artist test"));
@ -49,7 +49,7 @@ fn write() {
let mut tagged_file = lofty::read_from(&mut file, false).unwrap();
assert_eq!(tagged_file.file_type(), &FileType::MP3);
assert_eq!(tagged_file.file_type(), FileType::MP3);
// ID3v2
crate::set_artist!(tagged_file, primary_tag_mut, "Foo artist", 1 => file, "Bar artist");

View file

@ -7,12 +7,12 @@ use std::io::{Seek, SeekFrom, Write};
#[test]
fn opus_read() {
read("tests/files/assets/minimal/full_test.opus", &FileType::Opus)
read("tests/files/assets/minimal/full_test.opus", FileType::Opus)
}
#[test]
fn opus_write() {
write("tests/files/assets/minimal/full_test.opus", &FileType::Opus)
write("tests/files/assets/minimal/full_test.opus", FileType::Opus)
}
#[test]
@ -26,12 +26,12 @@ fn opus_remove() {
#[test]
fn flac_read() {
// FLAC does **not** require a Vorbis comment block be present, this file has one
read("tests/files/assets/minimal/full_test.flac", &FileType::FLAC)
read("tests/files/assets/minimal/full_test.flac", FileType::FLAC)
}
#[test]
fn flac_write() {
write("tests/files/assets/minimal/full_test.flac", &FileType::FLAC)
write("tests/files/assets/minimal/full_test.flac", FileType::FLAC)
}
#[test]
@ -44,18 +44,12 @@ fn flac_remove() {
#[test]
fn vorbis_read() {
read(
"tests/files/assets/minimal/full_test.ogg",
&FileType::Vorbis,
)
read("tests/files/assets/minimal/full_test.ogg", FileType::Vorbis)
}
#[test]
fn vorbis_write() {
write(
"tests/files/assets/minimal/full_test.ogg",
&FileType::Vorbis,
)
write("tests/files/assets/minimal/full_test.ogg", FileType::Vorbis)
}
#[test]
@ -68,12 +62,12 @@ fn vorbis_remove() {
#[test]
fn speex_read() {
read("tests/files/assets/minimal/full_test.spx", &FileType::Speex)
read("tests/files/assets/minimal/full_test.spx", FileType::Speex)
}
#[test]
fn speex_write() {
write("tests/files/assets/minimal/full_test.spx", &FileType::Speex)
write("tests/files/assets/minimal/full_test.spx", FileType::Speex)
}
#[test]
@ -84,7 +78,7 @@ fn speex_remove() {
)
}
fn read(path: &str, file_type: &FileType) {
fn read(path: &str, file_type: FileType) {
let file = lofty::read_from_path(path, false).unwrap();
assert_eq!(file.file_type(), file_type);
@ -92,7 +86,7 @@ fn read(path: &str, file_type: &FileType) {
crate::verify_artist!(file, primary_tag, "Foo artist", 2);
}
fn write(path: &str, file_type: &FileType) {
fn write(path: &str, file_type: FileType) {
let mut file = temp_file!(path);
let mut tagged_file = lofty::read_from(&mut file, false).unwrap();

View file

@ -8,7 +8,7 @@ fn read() {
let file =
lofty::read_from_path("tests/files/assets/minimal/wav_format_pcm.wav", false).unwrap();
assert_eq!(file.file_type(), &FileType::WAV);
assert_eq!(file.file_type(), FileType::WAV);
// Verify the ID3v2 tag first
crate::verify_artist!(file, primary_tag, "Foo artist", 1);
@ -23,7 +23,7 @@ fn write() {
let mut tagged_file = lofty::read_from(&mut file, false).unwrap();
assert_eq!(tagged_file.file_type(), &FileType::WAV);
assert_eq!(tagged_file.file_type(), FileType::WAV);
// ID3v2
crate::set_artist!(tagged_file, primary_tag_mut, "Foo artist", 1 => file, "Bar artist");