Skip to content

Commit 2756c3f

Browse files
committed
EBML: Bring branch up to date
1 parent d836314 commit 2756c3f

File tree

16 files changed

+71
-31
lines changed

16 files changed

+71
-31
lines changed

lofty/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ byteorder = { workspace = true }
1818
# ID3 compressed frames
1919
flate2 = { version = "1.0.30", optional = true }
2020
# Proc macros
21-
lofty_attr = "0.11.0"
21+
lofty_attr = { path = "../lofty_attr" }
2222
# Debug logging
2323
log = "0.4.22"
2424
# OGG Vorbis/Opus

src/ebml/element_reader.rs renamed to lofty/src/ebml/element_reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ where
240240
self.ctx.max_id_length,
241241
self.ctx.max_size_length,
242242
)?;
243-
let Some(master) = MASTER_ELEMENTS.get(&header.id) else {
243+
let Some(master) = master_elements().get(&header.id) else {
244244
// We encountered an unknown master element
245245
return Ok(ElementReaderYield::Unknown(header));
246246
};
@@ -290,7 +290,7 @@ where
290290
if child.data_type == ElementDataType::Master {
291291
self.store_previous_master();
292292
self.ctx.current_master = Some(
293-
*MASTER_ELEMENTS
293+
*master_elements()
294294
.get(&header.id)
295295
.expect("Nested master elements should be defined at this level."),
296296
);

src/ebml/mod.rs renamed to lofty/src/ebml/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
mod element_reader;
33
mod properties;
44
mod read;
5-
mod tag;
5+
pub(crate) mod tag;
66
mod vint;
77

88
use lofty_attr::LoftyFile;
File renamed without changes.

src/ebml/read.rs renamed to lofty/src/ebml/read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ mod segment_info;
33
mod segment_tracks;
44

55
use super::EbmlFile;
6+
use crate::config::ParseOptions;
67
use crate::ebml::element_reader::{ElementHeader, ElementIdent, ElementReader, ElementReaderYield};
78
use crate::ebml::vint::VInt;
89
use crate::ebml::EbmlProperties;
910
use crate::error::Result;
1011
use crate::macros::decode_err;
11-
use crate::probe::ParseOptions;
1212

1313
use std::io::{Read, Seek};
1414

src/ebml/read/segment.rs renamed to lofty/src/ebml/read/segment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::{segment_info, segment_tracks};
2+
use crate::config::ParseOptions;
23
use crate::ebml::element_reader::{ElementIdent, ElementReader, ElementReaderYield};
34
use crate::ebml::properties::EbmlProperties;
45
use crate::ebml::tag::EbmlTag;
56
use crate::error::Result;
67
use crate::macros::decode_err;
7-
use crate::probe::ParseOptions;
88

99
use std::io::{Read, Seek};
1010

src/ebml/read/segment_info.rs renamed to lofty/src/ebml/read/segment_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::config::{ParseOptions, ParsingMode};
12
use crate::ebml::element_reader::{ElementIdent, ElementReader, ElementReaderYield};
23
use crate::ebml::properties::EbmlProperties;
34
use crate::error::Result;
45
use crate::macros::decode_err;
5-
use crate::probe::ParseOptions;
66

77
use std::io::{Read, Seek};
88

@@ -35,7 +35,7 @@ where
3535

3636
if properties.segment_info.timestamp_scale == 0 {
3737
log::warn!("Segment.Info.TimecodeScale is 0, which is invalid");
38-
if parse_options.parsing_mode == crate::probe::ParsingMode::Strict {
38+
if parse_options.parsing_mode == ParsingMode::Strict {
3939
decode_err!(@BAIL Ebml, "Segment.Info.TimecodeScale must be nonzero");
4040
}
4141
}

src/ebml/read/segment_tracks.rs renamed to lofty/src/ebml/read/segment_tracks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::config::ParseOptions;
12
use crate::ebml::element_reader::{ElementIdent, ElementReader, ElementReaderYield};
23
use crate::ebml::properties::EbmlProperties;
34
use crate::error::Result;
45
use crate::macros::decode_err;
5-
use crate::probe::ParseOptions;
66

77
use std::io::{Read, Seek};
88

src/ebml/tag/mod.rs renamed to lofty/src/ebml/tag/mod.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::config::WriteOptions;
12
use crate::error::LoftyError;
2-
use crate::tag::Tag;
3-
use crate::traits::{Accessor, MergeTag, SplitTag, TagExt};
3+
use crate::io::{FileLike, Length, Truncate};
4+
use crate::tag::{Accessor, MergeTag, SplitTag, Tag, TagExt, TagType};
45

5-
use std::fs::File;
66
use std::io::Write;
77
use std::ops::Deref;
88
use std::path::Path;
@@ -20,6 +20,11 @@ impl TagExt for EbmlTag {
2020
type Err = LoftyError;
2121
type RefKey<'a> = &'a str;
2222

23+
#[inline]
24+
fn tag_type(&self) -> TagType {
25+
TagType::Ebml
26+
}
27+
2328
fn len(&self) -> usize {
2429
todo!()
2530
}
@@ -32,19 +37,37 @@ impl TagExt for EbmlTag {
3237
todo!()
3338
}
3439

35-
fn save_to(&self, _file: &mut File) -> std::result::Result<(), Self::Err> {
40+
fn save_to<F>(
41+
&self,
42+
_file: &mut F,
43+
_write_options: WriteOptions,
44+
) -> std::result::Result<(), Self::Err>
45+
where
46+
F: FileLike,
47+
LoftyError: From<<F as Truncate>::Error>,
48+
LoftyError: From<<F as Length>::Error>,
49+
{
3650
todo!()
3751
}
3852

39-
fn dump_to<W: Write>(&self, _writer: &mut W) -> std::result::Result<(), Self::Err> {
53+
fn dump_to<W: Write>(
54+
&self,
55+
_writer: &mut W,
56+
_write_options: WriteOptions,
57+
) -> std::result::Result<(), Self::Err> {
4058
todo!()
4159
}
4260

4361
fn remove_from_path<P: AsRef<Path>>(&self, _path: P) -> std::result::Result<(), Self::Err> {
4462
todo!()
4563
}
4664

47-
fn remove_from(&self, _file: &mut File) -> std::result::Result<(), Self::Err> {
65+
fn remove_from<F>(&self, _file: &mut F) -> std::result::Result<(), Self::Err>
66+
where
67+
F: FileLike,
68+
LoftyError: From<<F as Truncate>::Error>,
69+
LoftyError: From<<F as Length>::Error>,
70+
{
4871
todo!()
4972
}
5073

src/ebml/vint.rs renamed to lofty/src/ebml/vint.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl VInt {
3333
/// ```rust
3434
/// use lofty::ebml::VInt;
3535
///
36-
/// # fn main() -> lofty::Result<()> {
36+
/// # fn main() -> lofty::error::Result<()> {
3737
/// // This value is too large to represent
3838
/// let invalid_vint = VInt::from_u64(u64::MAX);
3939
/// assert!(invalid_vint.is_err());
@@ -57,7 +57,7 @@ impl VInt {
5757
/// ```rust
5858
/// use lofty::ebml::VInt;
5959
///
60-
/// # fn main() -> lofty::Result<()> {
60+
/// # fn main() -> lofty::error::Result<()> {
6161
/// let vint = VInt::from_u64(2)?;
6262
/// assert_eq!(vint.value(), 2);
6363
/// # Ok(()) }
@@ -80,7 +80,7 @@ impl VInt {
8080
/// ```rust
8181
/// use lofty::ebml::VInt;
8282
///
83-
/// # fn main() -> lofty::Result<()> {
83+
/// # fn main() -> lofty::error::Result<()> {
8484
/// // This octet count (9) is too large to represent
8585
/// let mut invalid_vint_reader = &[0b0000_0000_1];
8686
/// let invalid_vint = VInt::parse(&mut &invalid_vint_reader[..], 8);
@@ -128,9 +128,9 @@ impl VInt {
128128
/// ```rust
129129
/// use lofty::ebml::VInt;
130130
///
131-
/// # fn main() -> lofty::Result<()> {
131+
/// # fn main() -> lofty::error::Result<()> {
132132
/// // Parse the EBML header element ID
133-
/// let mut reader = &[0x1A, 0x45, 0xDF, 0xA3];
133+
/// let mut reader = &[0x1A, 0x45, 0xDF, 0xA3][..];
134134
/// let vint = VInt::parse_from_element_id(&mut reader, 8)?;
135135
/// assert_eq!(vint.value(), 0x1A45DFA3);
136136
/// # Ok(()) }
@@ -175,7 +175,7 @@ impl VInt {
175175
/// ```rust
176176
/// use lofty::ebml::VInt;
177177
///
178-
/// # fn main() -> lofty::Result<()> {
178+
/// # fn main() -> lofty::error::Result<()> {
179179
/// // Anything <= 254 will fit into a single octet
180180
/// let vint = VInt::from_u64(100)?;
181181
/// assert_eq!(vint.octet_length(), 1);
@@ -215,7 +215,7 @@ impl VInt {
215215
/// ```rust
216216
/// use lofty::ebml::VInt;
217217
///
218-
/// # fn main() -> lofty::Result<()> {
218+
/// # fn main() -> lofty::error::Result<()> {
219219
/// let vint = VInt::from_u64(10)?;
220220
/// let bytes = vint.as_bytes(None)?;
221221
///

lofty/src/file/file_type.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum FileType {
1313
Aac,
1414
Aiff,
1515
Ape,
16+
Ebml,
1617
Flac,
1718
Mpeg,
1819
Mp4,
@@ -52,6 +53,7 @@ impl FileType {
5253
match self {
5354
FileType::Aac | FileType::Aiff | FileType::Mpeg | FileType::Wav => TagType::Id3v2,
5455
FileType::Ape | FileType::Mpc | FileType::WavPack => TagType::Ape,
56+
FileType::Ebml => TagType::Ebml,
5557
FileType::Flac | FileType::Opus | FileType::Vorbis | FileType::Speex => {
5658
TagType::VorbisComments
5759
},
@@ -90,6 +92,7 @@ impl FileType {
9092

9193
match tag_type {
9294
TagType::Ape => crate::ape::ApeTag::SUPPORTED_FORMATS.contains(self),
95+
TagType::Ebml => crate::ebml::EbmlTag::SUPPORTED_FORMATS.contains(self),
9396
TagType::Id3v1 => crate::id3::v1::Id3v1Tag::SUPPORTED_FORMATS.contains(self),
9497
TagType::Id3v2 => crate::id3::v2::Id3v2Tag::SUPPORTED_FORMATS.contains(self),
9598
TagType::Mp4Ilst => crate::mp4::Ilst::SUPPORTED_FORMATS.contains(self),
@@ -137,6 +140,7 @@ impl FileType {
137140
"opus" => Some(Self::Opus),
138141
"flac" => Some(Self::Flac),
139142
"ogg" => Some(Self::Vorbis),
143+
"mka" | "mkv" | "webm" => Some(Self::Ebml),
140144
"mp4" | "m4a" | "m4b" | "m4p" | "m4r" | "m4v" | "3gp" => Some(Self::Mp4),
141145
"mpc" | "mp+" | "mpp" => Some(Self::Mpc),
142146
"spx" => Some(Self::Speex),
@@ -300,6 +304,7 @@ impl FileType {
300304
None
301305
},
302306
119 if buf.len() >= 4 && &buf[..4] == b"wvpk" => Some(Self::WavPack),
307+
26 if buf.starts_with(&[0x1A, 0x45, 0xDF, 0xA3]) => Some(Self::Ebml),
303308
_ if buf.len() >= 8 && &buf[4..8] == b"ftyp" => Some(Self::Mp4),
304309
_ if buf.starts_with(b"MPCK") || buf.starts_with(b"MP+") => Some(Self::Mpc),
305310
_ => None,

lofty/src/tag/split_merge_tag.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub trait MergeTag: private::Sealed {
7070
// https://rust-lang.github.io/api-guidelines/future-proofing.html#c-sealed
7171
mod private {
7272
use crate::ape::ApeTag;
73+
use crate::ebml::EbmlTag;
7374
use crate::id3::v1::Id3v1Tag;
7475
use crate::id3::v2::Id3v2Tag;
7576
use crate::iff::aiff::AiffTextChunks;
@@ -85,6 +86,9 @@ mod private {
8586
impl Sealed for ApeTag {}
8687
impl Sealed for crate::ape::tag::SplitTagRemainder {}
8788

89+
impl Sealed for EbmlTag {}
90+
impl Sealed for crate::ebml::tag::SplitTagRemainder {}
91+
8892
impl Sealed for Id3v1Tag {}
8993
impl Sealed for crate::id3::v1::tag::SplitTagRemainder {}
9094

lofty/src/tag/tag_ext.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ pub trait TagExt: Accessor + Into<Tag> + Sized + private::Sealed {
153153
// https://rust-lang.github.io/api-guidelines/future-proofing.html#c-sealed
154154
mod private {
155155
use crate::ape::ApeTag;
156+
use crate::ebml::EbmlTag;
156157
use crate::id3::v1::Id3v1Tag;
157158
use crate::id3::v2::Id3v2Tag;
158159
use crate::iff::aiff::AiffTextChunks;
@@ -165,6 +166,7 @@ mod private {
165166

166167
impl Sealed for AiffTextChunks {}
167168
impl Sealed for ApeTag {}
169+
impl Sealed for EbmlTag {}
168170
impl Sealed for Id3v1Tag {}
169171
impl Sealed for Id3v2Tag {}
170172
impl Sealed for Ilst {}

lofty/src/tag/tag_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use std::path::Path;
1515
pub enum TagType {
1616
/// This covers both APEv1 and APEv2 as it doesn't matter much
1717
Ape,
18+
/// Represents an EBML tag element
19+
Ebml,
1820
/// Represents an ID3v1 tag
1921
Id3v1,
2022
/// This covers all ID3v2 versions since they all get upgraded to ID3v2.4

lofty_attr/src/ebml.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) struct EbmlMasterElement {
1010
}
1111

1212
impl Parse for EbmlMasterElement {
13-
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
13+
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
1414
let readable_ident = input.parse::<Ident>()?;
1515
let _: syn::Token![:] = input.parse()?;
1616

@@ -30,7 +30,7 @@ pub(crate) struct EbmlMasterInfo {
3030
}
3131

3232
impl Parse for EbmlMasterInfo {
33-
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
33+
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
3434
let _id_field = input.parse::<Ident>()?;
3535
let _: syn::Token![:] = input.parse()?;
3636

@@ -60,7 +60,7 @@ pub(crate) struct EbmlChildElement {
6060
}
6161

6262
impl Parse for EbmlChildElement {
63-
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
63+
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
6464
let readable_ident = input.parse::<Ident>()?;
6565
let _: syn::Token![:] = input.parse()?;
6666

@@ -80,7 +80,7 @@ pub(crate) struct EbmlChildInfo {
8080
}
8181

8282
impl Parse for EbmlChildInfo {
83-
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
83+
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
8484
let id = input.parse::<syn::LitInt>()?.base10_parse()?;
8585
let _: syn::Token![,] = input.parse()?;
8686

lofty_attr/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use crate::lofty_file::LoftyFile;
4444
use crate::lofty_tag::{LoftyTag, LoftyTagAttribute};
4545

4646
use proc_macro::TokenStream;
47+
use quote::quote;
4748
use syn::{parse_macro_input, ItemStruct};
4849

4950
/// Creates a file usable by Lofty
@@ -111,10 +112,13 @@ pub fn ebml_master_elements(input: TokenStream) -> TokenStream {
111112
#( #identifiers_iter ),*
112113
}
113114

114-
static MASTER_ELEMENTS: once_cell::sync::Lazy<std::collections::HashMap<VInt, MasterElement>> = once_cell::sync::Lazy::new(|| {
115-
let mut m = std::collections::HashMap::new();
116-
#( #elements_map_inserts )*
117-
m
118-
});
115+
fn master_elements() -> &'static ::std::collections::HashMap<VInt, MasterElement> {
116+
static INSTANCE: ::std::sync::OnceLock<::std::collections::HashMap<VInt, MasterElement>> = ::std::sync::OnceLock::new();
117+
INSTANCE.get_or_init(|| {
118+
let mut m = ::std::collections::HashMap::new();
119+
#( #elements_map_inserts )*
120+
m
121+
})
122+
}
119123
})
120124
}

0 commit comments

Comments
 (0)