Skip to content

Commit

Permalink
EBML: Rename TagType::Ebml -> TagType::Matroska
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Oct 14, 2024
1 parent 786b2c6 commit 3075f42
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lofty/src/ebml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub use vint::*;
#[lofty(internal_write_module_do_not_use_anywhere_else)]
pub struct EbmlFile {
/// An EBML tag
#[lofty(tag_type = "Ebml")]
pub(crate) ebml_tag: Option<EbmlTag>,
#[lofty(tag_type = "Matroska")]
pub(crate) ebml_tag: Option<MatroskaTag>,
/// The file's audio properties
pub(crate) properties: EbmlProperties,
}
4 changes: 2 additions & 2 deletions lofty/src/ebml/read/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{segment_attachments, segment_info, segment_tags, segment_tracks};
use crate::config::ParseOptions;
use crate::ebml::element_reader::{ElementHeader, ElementIdent, ElementReader, ElementReaderYield};
use crate::ebml::properties::EbmlProperties;
use crate::ebml::tag::EbmlTag;
use crate::ebml::tag::MatroskaTag;
use crate::ebml::ElementId;
use crate::error::Result;

Expand All @@ -12,7 +12,7 @@ pub(super) fn read_from<R>(
element_reader: &mut ElementReader<R>,
parse_options: ParseOptions,
properties: &mut EbmlProperties,
) -> Result<Option<EbmlTag>>
) -> Result<Option<MatroskaTag>>
where
R: Read + Seek,
{
Expand Down
4 changes: 2 additions & 2 deletions lofty/src/ebml/read/segment_attachments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::config::ParseOptions;
use crate::ebml::element_reader::{
ElementChildIterator, ElementIdent, ElementReader, ElementReaderYield,
};
use crate::ebml::{AttachedFile, EbmlTag};
use crate::ebml::{AttachedFile, MatroskaTag};
use crate::error::Result;
use crate::macros::decode_err;
use crate::picture::MimeType;
Expand All @@ -13,7 +13,7 @@ use std::io::{Read, Seek};
pub(super) fn read_from<R>(
children_reader: &mut ElementChildIterator<'_, R>,
_parse_options: ParseOptions,
tag: &mut EbmlTag,
tag: &mut MatroskaTag,
) -> Result<()>
where
R: Read + Seek,
Expand Down
4 changes: 2 additions & 2 deletions lofty/src/ebml/read/segment_chapters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::ParseOptions;
use crate::ebml::element_reader::ElementChildIterator;
use crate::ebml::EbmlTag;
use crate::ebml::MatroskaTag;
use crate::error::Result;

use std::io::{Read, Seek};
Expand All @@ -9,7 +9,7 @@ use std::io::{Read, Seek};
pub(super) fn read_from<R>(
_children_reader: &mut ElementChildIterator<'_, R>,
_parse_options: ParseOptions,
_tag: &mut EbmlTag,
_tag: &mut MatroskaTag,
) -> Result<()>
where
R: Read + Seek,
Expand Down
4 changes: 2 additions & 2 deletions lofty/src/ebml/read/segment_tags.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::ParseOptions;
use crate::ebml::element_reader::{ElementChildIterator, ElementIdent, ElementReaderYield};
use crate::ebml::{EbmlTag, Language, SimpleTag, Tag, TagValue, Target, TargetType};
use crate::ebml::{Language, MatroskaTag, SimpleTag, Tag, TagValue, Target, TargetType};
use crate::error::Result;
use crate::macros::decode_err;

Expand All @@ -9,7 +9,7 @@ use std::io::{Read, Seek};
pub(super) fn read_from<R>(
children_reader: &mut ElementChildIterator<'_, R>,
_parse_options: ParseOptions,
tag: &mut EbmlTag,
tag: &mut MatroskaTag,
) -> Result<()>
where
R: Read + Seek,
Expand Down
6 changes: 3 additions & 3 deletions lofty/src/ebml/tag/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! NOTE: We can **ONLY** convert `SimpleTags` that come from a target with **NO** uids
use super::{EbmlTag, Language, SimpleTag, TargetType, TOMBSTONE_SIMPLE_TAG};
use super::{Language, MatroskaTag, SimpleTag, TargetType, TOMBSTONE_SIMPLE_TAG};
use crate::tag::items::Lang;
use crate::tag::{ItemKey, Tag, TagItem, TagType};

Expand Down Expand Up @@ -135,8 +135,8 @@ matroska_mapping_tables!(
const TAG_RETAINED: bool = true;
const TAG_CONSUMED: bool = false;

pub(super) fn split_tag(mut ebml_tag: EbmlTag) -> (EbmlTag, Tag) {
let mut tag = Tag::new(TagType::Ebml);
pub(super) fn split_tag(mut ebml_tag: MatroskaTag) -> (MatroskaTag, Tag) {
let mut tag = Tag::new(TagType::Matroska);

// TODO: Pictures, can they be handled in a generic way?
// What about the uid and referral?
Expand Down
52 changes: 26 additions & 26 deletions lofty/src/ebml/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! impl_accessor {
paste::paste! {
$(
fn $method(&self) -> Option<Cow<'_, str>> {
self.get_str(EbmlTagKey(TargetType::$target, Cow::Borrowed($name)))
self.get_str(MatroskaTagKey(TargetType::$target, Cow::Borrowed($name)))
}

fn [<set_ $method>](&mut self, value: String) {
Expand All @@ -57,18 +57,18 @@ macro_rules! impl_accessor {
/// * [`Target`]
/// * [`AttachedFile`]
#[derive(Default, Debug, PartialEq, Eq, Clone)]
#[tag(description = "An `EBML` \"tag\"", supported_formats(Ebml))]
pub struct EbmlTag {
#[tag(description = "A Matroska/WebM \"tag\"", supported_formats(Ebml))]
pub struct MatroskaTag {
pub(crate) tags: Vec<Tag<'static>>,
pub(crate) attached_files: Vec<AttachedFile<'static>>,
}

// TODO
#[allow(missing_docs)]
pub struct EbmlTagKey<'a>(TargetType, Cow<'a, str>);
pub struct MatroskaTagKey<'a>(TargetType, Cow<'a, str>);

impl EbmlTag {
fn get(&self, key: EbmlTagKey<'_>) -> Option<&SimpleTag<'_>> {
impl MatroskaTag {
fn get(&self, key: MatroskaTagKey<'_>) -> Option<&SimpleTag<'_>> {
fn tag_matches_target(tag: &Tag<'_>, target_type: TargetType) -> bool {
let Some(target) = &tag.target else {
// An empty target is implicitly `Album`
Expand All @@ -78,7 +78,7 @@ impl EbmlTag {
target.is_candidate_for_type(target_type)
}

let EbmlTagKey(target, key) = key;
let MatroskaTagKey(target, key) = key;

let applicable_tags = self
.tags
Expand All @@ -98,7 +98,7 @@ impl EbmlTag {
None
}

fn get_str(&self, key: EbmlTagKey<'_>) -> Option<Cow<'_, str>> {
fn get_str(&self, key: MatroskaTagKey<'_>) -> Option<Cow<'_, str>> {
let simple_tag = self.get(key)?;
simple_tag.get_str().map(Cow::from)
}
Expand Down Expand Up @@ -131,11 +131,11 @@ impl EbmlTag {
/// # Examples
///
/// ```rust,no_run
/// use lofty::ebml::EbmlTag;
/// use lofty::ebml::MatroskaTag;
/// use lofty::picture::Picture;
///
/// # fn main() -> lofty::error::Result<()> {
/// let mut tag = EbmlTag::default();
/// let mut tag = MatroskaTag::default();
///
/// let mut picture = std::fs::read("something.png")?;
/// let mut picture2 = std::fs::read("something_else.png")?;
Expand All @@ -153,14 +153,14 @@ impl EbmlTag {

/// Inserts a new [`Picture`]
///
/// Note: See [`EbmlTag::insert_attached_file`]
/// Note: See [`MatroskaTag::insert_attached_file`]
///
/// ```rust,no_run
/// use lofty::ebml::EbmlTag;
/// use lofty::ebml::MatroskaTag;
/// use lofty::picture::Picture;
///
/// # fn main() -> lofty::error::Result<()> {
/// let mut tag = EbmlTag::default();
/// let mut tag = MatroskaTag::default();
///
/// let mut picture_file = std::fs::read("something.png")?;
/// tag.insert_picture(Picture::from_reader(&mut &picture_file[..])?);
Expand Down Expand Up @@ -219,7 +219,7 @@ impl EbmlTag {
}
}

impl Accessor for EbmlTag {
impl Accessor for MatroskaTag {
impl_accessor!(
artist => (Track, "ARTIST"),
title => (Track, "TITLE"),
Expand Down Expand Up @@ -267,21 +267,21 @@ impl Accessor for EbmlTag {
}
}

impl TagExt for EbmlTag {
impl TagExt for MatroskaTag {
type Err = LoftyError;
type RefKey<'a> = EbmlTagKey<'a>;
type RefKey<'a> = MatroskaTagKey<'a>;

#[inline]
fn tag_type(&self) -> TagType {
TagType::Ebml
TagType::Matroska
}

fn len(&self) -> usize {
self.tags.iter().map(Tag::len).sum::<usize>() + self.attached_files.len()
}

fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
let EbmlTagKey(target_type, key) = key;
let MatroskaTagKey(target_type, key) = key;
self.tags.iter().any(|tag| {
if let Some(target) = &tag.target {
return target.target_type == target_type
Expand Down Expand Up @@ -338,23 +338,23 @@ impl TagExt for EbmlTag {

#[doc(hidden)]
#[derive(Debug, Clone, Default)]
pub struct SplitTagRemainder(EbmlTag);
pub struct SplitTagRemainder(MatroskaTag);

impl From<SplitTagRemainder> for EbmlTag {
impl From<SplitTagRemainder> for MatroskaTag {
fn from(from: SplitTagRemainder) -> Self {
from.0
}
}

impl Deref for SplitTagRemainder {
type Target = EbmlTag;
type Target = MatroskaTag;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl SplitTag for EbmlTag {
impl SplitTag for MatroskaTag {
type Remainder = SplitTagRemainder;

fn split_tag(mut self) -> (Self::Remainder, crate::tag::Tag) {
Expand All @@ -364,20 +364,20 @@ impl SplitTag for EbmlTag {
}

impl MergeTag for SplitTagRemainder {
type Merged = EbmlTag;
type Merged = MatroskaTag;

fn merge_tag(self, _tag: crate::tag::Tag) -> Self::Merged {
todo!()
}
}

impl From<EbmlTag> for crate::tag::Tag {
fn from(input: EbmlTag) -> Self {
impl From<MatroskaTag> for crate::tag::Tag {
fn from(input: MatroskaTag) -> Self {
input.split_tag().1
}
}

impl From<crate::tag::Tag> for EbmlTag {
impl From<crate::tag::Tag> for MatroskaTag {
fn from(input: crate::tag::Tag) -> Self {
SplitTagRemainder::default().merge_tag(input)
}
Expand Down
5 changes: 3 additions & 2 deletions lofty/src/file/file_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl FileType {
/// | `Ape` , `Mpc`, `WavPack` | `Ape` |
/// | `Flac`, `Opus`, `Vorbis`, `Speex` | `VorbisComments` |
/// | `Mp4` | `Mp4Ilst` |
/// | `Ebml` | `Matroska` |
///
/// # Panics
///
Expand All @@ -53,7 +54,7 @@ impl FileType {
match self {
FileType::Aac | FileType::Aiff | FileType::Mpeg | FileType::Wav => TagType::Id3v2,
FileType::Ape | FileType::Mpc | FileType::WavPack => TagType::Ape,
FileType::Ebml => TagType::Ebml,
FileType::Ebml => TagType::Matroska,
FileType::Flac | FileType::Opus | FileType::Vorbis | FileType::Speex => {
TagType::VorbisComments
},
Expand Down Expand Up @@ -92,7 +93,7 @@ impl FileType {

match tag_type {
TagType::Ape => crate::ape::ApeTag::SUPPORTED_FORMATS.contains(self),
TagType::Ebml => crate::ebml::EbmlTag::SUPPORTED_FORMATS.contains(self),
TagType::Matroska => crate::ebml::MatroskaTag::SUPPORTED_FORMATS.contains(self),
TagType::Id3v1 => crate::id3::v1::Id3v1Tag::SUPPORTED_FORMATS.contains(self),
TagType::Id3v2 => crate::id3::v2::Id3v2Tag::SUPPORTED_FORMATS.contains(self),
TagType::Mp4Ilst => crate::mp4::Ilst::SUPPORTED_FORMATS.contains(self),
Expand Down
9 changes: 9 additions & 0 deletions lofty/src/tag/companion_tag.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::ebml::MatroskaTag;
use crate::id3::v2::Id3v2Tag;
use crate::mp4::Ilst;

#[derive(Debug, Clone)]
pub(crate) enum CompanionTag {
Id3v2(Id3v2Tag),
Ilst(Ilst),
Matroska(MatroskaTag),
}

impl CompanionTag {
Expand All @@ -21,4 +23,11 @@ impl CompanionTag {
_ => None,
}
}

pub(crate) fn matroska(self) -> Option<MatroskaTag> {
match self {
CompanionTag::Matroska(tag) => Some(tag),
_ => None,
}
}
}
2 changes: 1 addition & 1 deletion lofty/src/tag/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ impl TagItem {
return VALID_ITEMKEYS.contains(&self.item_key);
}

if tag_type == TagType::Ebml {
if tag_type == TagType::Matroska {
use crate::ebml::tag::SUPPORTED_ITEMKEYS;

return SUPPORTED_ITEMKEYS.contains(&self.item_key);
Expand Down
4 changes: 2 additions & 2 deletions lofty/src/tag/split_merge_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub trait MergeTag: private::Sealed {
// https://rust-lang.github.io/api-guidelines/future-proofing.html#c-sealed
mod private {
use crate::ape::ApeTag;
use crate::ebml::EbmlTag;
use crate::ebml::MatroskaTag;
use crate::id3::v1::Id3v1Tag;
use crate::id3::v2::Id3v2Tag;
use crate::iff::aiff::AiffTextChunks;
Expand All @@ -86,7 +86,7 @@ mod private {
impl Sealed for ApeTag {}
impl Sealed for crate::ape::tag::SplitTagRemainder {}

impl Sealed for EbmlTag {}
impl Sealed for MatroskaTag {}
impl Sealed for crate::ebml::tag::SplitTagRemainder {}

impl Sealed for Id3v1Tag {}
Expand Down
4 changes: 2 additions & 2 deletions lofty/src/tag/tag_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub trait TagExt: Accessor + Into<Tag> + Sized + private::Sealed {
// https://rust-lang.github.io/api-guidelines/future-proofing.html#c-sealed
mod private {
use crate::ape::ApeTag;
use crate::ebml::EbmlTag;
use crate::ebml::MatroskaTag;
use crate::id3::v1::Id3v1Tag;
use crate::id3::v2::Id3v2Tag;
use crate::iff::aiff::AiffTextChunks;
Expand All @@ -166,7 +166,7 @@ mod private {

impl Sealed for AiffTextChunks {}
impl Sealed for ApeTag {}
impl Sealed for EbmlTag {}
impl Sealed for MatroskaTag {}
impl Sealed for Id3v1Tag {}
impl Sealed for Id3v2Tag {}
impl Sealed for Ilst {}
Expand Down
4 changes: 2 additions & 2 deletions lofty/src/tag/tag_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::path::Path;
pub enum TagType {
/// This covers both APEv1 and APEv2 as it doesn't matter much
Ape,
/// Represents an EBML tag element
Ebml,
/// Represents a `\Segment\Tags` element in Matroska/WebM
Matroska,
/// Represents an ID3v1 tag
Id3v1,
/// This covers all ID3v2 versions since they all get upgraded to ID3v2.4
Expand Down
4 changes: 4 additions & 0 deletions lofty/src/tag/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ where
FileType::Aac => aac::write::write_to(file, tag, write_options),
FileType::Aiff => iff::aiff::write::write_to(file, tag, write_options),
FileType::Ape => ape::write::write_to(file, tag, write_options),
FileType::Ebml => todo!("write EBML tags"),
FileType::Flac => flac::write::write_to(file, tag, write_options),
FileType::Opus | FileType::Speex | FileType::Vorbis => {
crate::ogg::write::write_to(file, tag, file_type, write_options)
Expand Down Expand Up @@ -98,6 +99,9 @@ pub(crate) fn dump_tag<W: Write>(
}
}
.dump_to(writer, write_options),
TagType::Matroska => {
todo!("Dump EBML tags")
},
_ => Ok(()),
}
}
Expand Down
Loading

0 comments on commit 3075f42

Please sign in to comment.