-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EBML: Start defining structure for writes
- Loading branch information
1 parent
81fc15d
commit a6669bf
Showing
16 changed files
with
695 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use crate::ebml::tag::write::{write_element, ElementWriterCtx, WriteableElement}; | ||
use crate::ebml::{AttachedFile, ElementId, VInt}; | ||
use crate::io::FileLike; | ||
|
||
const FileDescription_ID: ElementId = ElementId(0x467E); | ||
const FileName_ID: ElementId = ElementId(0x466E); | ||
const FileMediaType_ID: ElementId = ElementId(0x4660); | ||
const FileData_ID: ElementId = ElementId(0x465C); | ||
const FileUID_ID: ElementId = ElementId(0x46AE); | ||
const FileReferral_ID: ElementId = ElementId(0x4675); | ||
const FileUsedStartTime_ID: ElementId = ElementId(0x4661); | ||
const FileUsedEndTime_ID: ElementId = ElementId(0x4662); | ||
|
||
impl WriteableElement for AttachedFile { | ||
const ID: ElementId = ElementId(0x61A7); | ||
|
||
fn write_element<F: FileLike>( | ||
&self, | ||
ctx: ElementWriterCtx, | ||
writer: &mut F, | ||
) -> crate::error::Result<()> { | ||
self.validate()?; | ||
|
||
let mut element_children = Vec::new(); | ||
if let Some(description) = &self.description { | ||
write_element( | ||
ctx, | ||
FileDescription_ID, | ||
&description.as_str(), | ||
&mut element_children, | ||
)?; | ||
} | ||
|
||
write_element( | ||
ctx, | ||
FileName_ID, | ||
&self.file_name.as_str(), | ||
&mut element_children, | ||
)?; | ||
|
||
write_element( | ||
ctx, | ||
FileMediaType_ID, | ||
&self.mime_type.as_str(), | ||
&mut element_children, | ||
)?; | ||
|
||
write_element( | ||
ctx, | ||
FileData_ID, | ||
&self.file_data.as_slice(), | ||
&mut element_children, | ||
)?; | ||
|
||
let uid = VInt::<u64>::try_from(self.uid)?; | ||
write_element(ctx, FileUID_ID, &uid, &mut element_children)?; | ||
|
||
if let Some(referral) = &self.referral { | ||
write_element( | ||
ctx, | ||
FileReferral_ID, | ||
&referral.as_slice(), | ||
&mut element_children, | ||
)?; | ||
} | ||
|
||
if let Some(start_time) = &self.used_start_time { | ||
let vint = VInt::<u64>::try_from(*start_time)?; | ||
write_element(ctx, FileUsedStartTime_ID, &vint, &mut element_children)?; | ||
} | ||
|
||
if let Some(end_time) = &self.used_end_time { | ||
let vint = VInt::<u64>::try_from(*end_time)?; | ||
write_element(ctx, FileUsedEndTime_ID, &vint, &mut element_children)?; | ||
} | ||
|
||
write_element(ctx, Self::ID, &element_children.as_slice(), writer)?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub(super) mod attached_file; | ||
pub(super) mod target; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::ebml::tag::write::{write_element, EbmlWriteExt, ElementWriterCtx, WriteableElement}; | ||
use crate::ebml::{ElementId, Target, TargetType, VInt}; | ||
use crate::io::FileLike; | ||
|
||
const TargetTypeValue_ID: ElementId = ElementId(0x68CA); | ||
const TargetType_ID: ElementId = ElementId(0x63CA); | ||
const TagTrackUID_ID: ElementId = ElementId(0x63C5); | ||
const TagEditionUID_ID: ElementId = ElementId(0x63C9); | ||
const TagChapterUID_ID: ElementId = ElementId(0x63C4); | ||
const TagAttachmentUID_ID: ElementId = ElementId(0x63C6); | ||
|
||
impl WriteableElement for Target { | ||
const ID: ElementId = ElementId(0x63C0); | ||
|
||
fn write_element<F: FileLike>( | ||
&self, | ||
ctx: ElementWriterCtx, | ||
writer: &mut F, | ||
) -> crate::error::Result<()> { | ||
if self.is_empty_candidate() { | ||
writer.write_id(ctx, Self::ID)?; | ||
writer.write_size(ctx, VInt::<u64>::ZERO)?; | ||
return Ok(()); | ||
} | ||
|
||
let mut element_children = Vec::new(); | ||
if self.target_type == TargetType::Album { | ||
write_element( | ||
ctx, | ||
TargetTypeValue_ID, | ||
&[].as_slice(), | ||
&mut element_children, | ||
)?; | ||
} else { | ||
let vint = VInt::<u64>::try_from(self.target_type as u64)?; | ||
write_element(ctx, TargetTypeValue_ID, &vint, &mut element_children)?; | ||
} | ||
|
||
if let Some(name) = &self.name { | ||
write_element(ctx, TargetType_ID, &name.as_str(), &mut element_children)?; | ||
} | ||
|
||
if let Some(track_uids) = &self.track_uids { | ||
for &uid in track_uids { | ||
let vint = VInt::<u64>::try_from(uid)?; | ||
write_element(ctx, TagTrackUID_ID, &vint, &mut element_children)?; | ||
} | ||
} | ||
|
||
if let Some(edition_uids) = &self.edition_uids { | ||
for &uid in edition_uids { | ||
let vint = VInt::<u64>::try_from(uid)?; | ||
write_element(ctx, TagEditionUID_ID, &vint, &mut element_children)?; | ||
} | ||
} | ||
|
||
if let Some(chapter_uids) = &self.chapter_uids { | ||
for &uid in chapter_uids { | ||
let vint = VInt::<u64>::try_from(uid)?; | ||
write_element(ctx, TagChapterUID_ID, &vint, &mut element_children)?; | ||
} | ||
} | ||
|
||
if let Some(attachment_uids) = &self.attachment_uids { | ||
for &uid in attachment_uids { | ||
let vint = VInt::<u64>::try_from(uid)?; | ||
write_element(ctx, TagAttachmentUID_ID, &vint, &mut element_children)?; | ||
} | ||
} | ||
|
||
write_element(ctx, Self::ID, &element_children.as_slice(), writer)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use std::io::Cursor; | ||
|
||
#[test_log::test] | ||
fn write_empty_default() { | ||
let target = Target::default(); | ||
|
||
let mut buf = Cursor::new(Vec::new()); | ||
target | ||
.write_element( | ||
ElementWriterCtx { | ||
max_id_len: 4, | ||
max_size_len: 8, | ||
}, | ||
&mut buf, | ||
) | ||
.unwrap(); | ||
|
||
let expected = vec![0x63, 0xC0, 0x80]; | ||
|
||
assert_eq!(buf.into_inner(), expected); | ||
} | ||
} |
Oops, something went wrong.