From 301d457cd38fc7ff165e2535888f2150dae964d8 Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Fri, 18 Mar 2022 15:06:42 -0400 Subject: [PATCH] Give FLAC its own module --- CHANGELOG.md | 1 + src/{ogg => }/flac/block.rs | 16 ++++++++-------- src/{ogg => }/flac/mod.rs | 18 ++++++++++++++++-- src/{ogg => }/flac/properties.rs | 2 +- src/{ogg => }/flac/read.rs | 4 ++-- src/{ogg => }/flac/write.rs | 2 +- src/lib.rs | 1 + src/ogg/mod.rs | 2 -- src/ogg/read.rs | 2 +- src/ogg/tag.rs | 3 ++- src/ogg/write.rs | 3 ++- src/probe.rs | 2 +- src/properties.rs | 4 ++-- tests/files/ogg.rs | 2 +- 14 files changed, 39 insertions(+), 23 deletions(-) rename src/{ogg => }/flac/block.rs (71%) rename src/{ogg => }/flac/mod.rs (80%) rename src/{ogg => }/flac/properties.rs (97%) rename src/{ogg => }/flac/read.rs (95%) rename src/{ogg => }/flac/write.rs (99%) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3a3eea7..dd8a6137 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The method still only supports PNG and JPEG, but rather than error when it encounters an unknown image, it will return `PictureInformation::default` - `lofty::read_from` will now wrap the `File` in a `BufReader` +- **FLAC**: FLAC now has its own module at `lofty::flac` ### Fixed - **MP4**: Non-full `meta` atoms are now properly handled. diff --git a/src/ogg/flac/block.rs b/src/flac/block.rs similarity index 71% rename from src/ogg/flac/block.rs rename to src/flac/block.rs index 9c4e7a74..37853327 100644 --- a/src/ogg/flac/block.rs +++ b/src/flac/block.rs @@ -5,17 +5,17 @@ use std::io::{Read, Seek, SeekFrom}; use byteorder::{BigEndian, ReadBytesExt}; -pub(super) struct Block { - pub(super) byte: u8, - pub(super) ty: u8, - pub(super) last: bool, - pub(super) content: Vec, - pub(super) start: u64, - pub(super) end: u64, +pub(crate) struct Block { + pub(crate) byte: u8, + pub(crate) ty: u8, + pub(crate) last: bool, + pub(crate) content: Vec, + pub(crate) start: u64, + pub(crate) end: u64, } impl Block { - pub(in crate::ogg) fn read(data: &mut R) -> Result + pub(crate) fn read(data: &mut R) -> Result where R: Read + Seek, { diff --git a/src/ogg/flac/mod.rs b/src/flac/mod.rs similarity index 80% rename from src/ogg/flac/mod.rs rename to src/flac/mod.rs index f08fb55c..a9c4ecf9 100644 --- a/src/ogg/flac/mod.rs +++ b/src/flac/mod.rs @@ -1,21 +1,35 @@ +//! Items for FLAC +//! +//! ## File notes +//! +//! * See [`FlacFile`] + mod block; mod properties; mod read; #[cfg(feature = "vorbis_comments")] pub(crate) mod write; -#[cfg(feature = "vorbis_comments")] -use super::tag::VorbisComments; use crate::error::Result; use crate::file::{AudioFile, FileType, TaggedFile}; #[cfg(feature = "id3v2")] use crate::id3::v2::tag::Id3v2Tag; +#[cfg(feature = "vorbis_comments")] +use crate::ogg::VorbisComments; use crate::properties::FileProperties; use crate::tag::TagType; use std::io::{Read, Seek}; /// A FLAC file +/// +/// ## Notes +/// +/// * The ID3v2 tag is **read only**, and it's use is discouraged by spec +/// * Picture blocks will be stored in the `VorbisComments` tag, meaning a file could have no vorbis +/// comments block, but `FlacFile::vorbis_comments` will exist. +/// * When writing, the pictures will be stored in their own picture blocks +/// * This behavior will likely change in the future pub struct FlacFile { #[cfg(feature = "id3v2")] /// An ID3v2 tag diff --git a/src/ogg/flac/properties.rs b/src/flac/properties.rs similarity index 97% rename from src/ogg/flac/properties.rs rename to src/flac/properties.rs index 1e3c5fc8..c753510c 100644 --- a/src/ogg/flac/properties.rs +++ b/src/flac/properties.rs @@ -6,7 +6,7 @@ use std::time::Duration; use byteorder::{BigEndian, ReadBytesExt}; -pub(super) fn read_properties( +pub(crate) fn read_properties( stream_info: &mut R, stream_length: u64, file_length: u64, diff --git a/src/ogg/flac/read.rs b/src/flac/read.rs similarity index 95% rename from src/ogg/flac/read.rs rename to src/flac/read.rs index 155bd1da..f715c3d4 100644 --- a/src/ogg/flac/read.rs +++ b/src/flac/read.rs @@ -14,7 +14,7 @@ use crate::{ use std::io::{Read, Seek, SeekFrom}; -pub(super) fn verify_flac(data: &mut R) -> Result +pub(crate) fn verify_flac(data: &mut R) -> Result where R: Read + Seek, { @@ -40,7 +40,7 @@ where Ok(block) } -pub(super) fn read_from(data: &mut R, read_properties: bool) -> Result +pub(crate) fn read_from(data: &mut R, read_properties: bool) -> Result where R: Read + Seek, { diff --git a/src/ogg/flac/write.rs b/src/flac/write.rs similarity index 99% rename from src/ogg/flac/write.rs rename to src/flac/write.rs index 44ef38eb..a300535b 100644 --- a/src/ogg/flac/write.rs +++ b/src/flac/write.rs @@ -12,7 +12,7 @@ use byteorder::{LittleEndian, WriteBytesExt}; const MAX_BLOCK_SIZE: u32 = 16_777_215; -pub(in crate) fn write_to<'a, II, IP>( +pub(crate) fn write_to<'a, II, IP>( data: &mut File, tag: &mut VorbisCommentsRef<'a, II, IP>, ) -> Result<()> diff --git a/src/lib.rs b/src/lib.rs index 442d09e9..a3c27031 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,6 +167,7 @@ pub mod ape; pub mod error; pub(crate) mod file; +pub mod flac; pub mod id3; pub mod iff; pub(crate) mod macros; diff --git a/src/ogg/mod.rs b/src/ogg/mod.rs index e6eaece9..2e079869 100644 --- a/src/ogg/mod.rs +++ b/src/ogg/mod.rs @@ -4,7 +4,6 @@ //! //! The only supported tag format is [`VorbisComments`] pub(crate) mod constants; -pub(crate) mod flac; pub(crate) mod opus; pub(crate) mod read; pub(crate) mod speex; @@ -28,7 +27,6 @@ cfg_if::cfg_if! { } } -pub use flac::FlacFile; pub use opus::properties::OpusProperties; pub use opus::OpusFile; pub use speex::properties::SpeexProperties; diff --git a/src/ogg/read.rs b/src/ogg/read.rs index 84fae0dd..4225168d 100644 --- a/src/ogg/read.rs +++ b/src/ogg/read.rs @@ -19,7 +19,7 @@ pub type OGGTags = (Option, Page); pub type OGGTags = (Option<()>, Page); #[cfg(feature = "vorbis_comments")] -pub(super) fn read_comments(data: &mut R, tag: &mut VorbisComments) -> Result<()> +pub(crate) fn read_comments(data: &mut R, tag: &mut VorbisComments) -> Result<()> where R: Read, { diff --git a/src/ogg/tag.rs b/src/ogg/tag.rs index 7a7db726..f1fbdd43 100644 --- a/src/ogg/tag.rs +++ b/src/ogg/tag.rs @@ -7,6 +7,7 @@ use crate::tag::item::{ItemKey, ItemValue, TagItem}; use crate::tag::{Tag, TagType}; use crate::traits::{Accessor, TagExt}; +use crate::flac::write; use std::fs::{File, OpenOptions}; use std::io::{Cursor, Write}; use std::path::Path; @@ -289,7 +290,7 @@ where let file = probe.into_inner(); match f_ty { - Some(FileType::FLAC) => super::flac::write::write_to(file, self), + Some(FileType::FLAC) => write::write_to(file, self), Some(FileType::Opus) => super::write::write(file, self, OGGFormat::Opus), Some(FileType::Vorbis) => super::write::write(file, self, OGGFormat::Vorbis), Some(FileType::Speex) => super::write::write(file, self, OGGFormat::Speex), diff --git a/src/ogg/write.rs b/src/ogg/write.rs index a4443ba2..5119ff61 100644 --- a/src/ogg/write.rs +++ b/src/ogg/write.rs @@ -11,6 +11,7 @@ use std::convert::TryFrom; use std::fs::File; use std::io::{Cursor, Read, Seek, SeekFrom, Write}; +use crate::flac::write; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use ogg_pager::Page; @@ -45,7 +46,7 @@ pub(in crate) fn write_to(file: &mut File, tag: &Tag, file_type: FileType) -> Re }; if let FileType::FLAC = file_type { - return super::flac::write::write_to(file, &mut comments_ref); + return write::write_to(file, &mut comments_ref); } let format = match file_type { diff --git a/src/probe.rs b/src/probe.rs index b0083403..5c6ff23b 100644 --- a/src/probe.rs +++ b/src/probe.rs @@ -1,12 +1,12 @@ use crate::ape::ApeFile; use crate::error::{ErrorKind, LoftyError, Result}; use crate::file::{AudioFile, FileType, TaggedFile}; +use crate::flac::FlacFile; use crate::iff::aiff::AiffFile; use crate::iff::wav::WavFile; use crate::mp3::header::search_for_frame_sync; use crate::mp3::Mp3File; use crate::mp4::Mp4File; -use crate::ogg::flac::FlacFile; use crate::ogg::opus::OpusFile; use crate::ogg::speex::SpeexFile; use crate::ogg::vorbis::VorbisFile; diff --git a/src/properties.rs b/src/properties.rs index 1f15ea15..d2f35735 100644 --- a/src/properties.rs +++ b/src/properties.rs @@ -60,12 +60,12 @@ impl FileProperties { #[cfg(test)] mod tests { use crate::ape::{ApeFile, ApeProperties}; + use crate::flac::FlacFile; use crate::iff::{AiffFile, WavFile, WavFormat, WavProperties}; use crate::mp3::{ChannelMode, Emphasis, Layer, Mp3File, Mp3Properties, MpegVersion}; use crate::mp4::{AudioObjectType, Mp4Codec, Mp4File, Mp4Properties}; use crate::ogg::{ - FlacFile, OpusFile, OpusProperties, SpeexFile, SpeexProperties, VorbisFile, - VorbisProperties, + OpusFile, OpusProperties, SpeexFile, SpeexProperties, VorbisFile, VorbisProperties, }; use crate::{AudioFile, FileProperties}; diff --git a/tests/files/ogg.rs b/tests/files/ogg.rs index ec3c335e..02a5eb25 100644 --- a/tests/files/ogg.rs +++ b/tests/files/ogg.rs @@ -124,7 +124,7 @@ fn remove(path: &str, tag_type: TagType) { #[test] fn flac_with_id3v2() { - use lofty::ogg::FlacFile; + use lofty::flac::FlacFile; use lofty::{Accessor, AudioFile}; let file = std::fs::read("tests/files/assets/flac_with_id3v2.flac").unwrap();