Parse whole test file, using constants for ids.

This commit is contained in:
Tangent 128 2017-04-30 00:19:38 -04:00
parent c567099b73
commit 466f0c46f0

View file

@ -128,6 +128,9 @@ pub trait Schema<'a> {
} }
pub const EBML_HEAD_ID: u64 = 0x0A45DFA3;
pub const VOID_ID: u64 = 0x6C;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Ebml<'b, S: Schema<'b>, T: 'b>(S, &'b T); pub struct Ebml<'b, S: Schema<'b>, T: 'b>(S, &'b T);
@ -145,13 +148,23 @@ impl<'b, S: Schema<'b>> IntoIterator for Ebml<'b, S, &'b[u8]> {
} }
} }
const SEGMENT_ID: u64 = 0x08538067;
const SEEK_HEAD_ID: u64 = 0x014D9B74;
const SEGMENT_INFO_ID: u64 = 0x0549A966;
const CUES_ID: u64 = 0x0C53BB6B;
const TRACKS_ID: u64 = 0x0654AE6B;
const CLUSTER_ID: u64 = 0x0F43B675;
pub struct Webm; pub struct Webm;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum WebmElement<'b> { pub enum WebmElement<'b> {
EbmlHead, EbmlHead,
Void,
Segment, Segment,
SeekHead, SeekHead,
Info,
Cues,
Tracks(&'b[u8]),
Cluster(&'b[u8]), Cluster(&'b[u8]),
Unknown(u64) Unknown(u64)
} }
@ -162,17 +175,21 @@ impl<'a> Schema<'a> for Webm {
fn should_unwrap(&self, element_id: u64) -> bool { fn should_unwrap(&self, element_id: u64) -> bool {
match element_id { match element_id {
// Segment // Segment
0x08538067 => true, SEGMENT_ID => true,
_ => false _ => false
} }
} }
fn decode<'b: 'a>(&self, element_id: u64, bytes: &'b[u8]) -> Result<WebmElement<'b>, Error> { fn decode<'b: 'a>(&self, element_id: u64, bytes: &'b[u8]) -> Result<WebmElement<'b>, Error> {
match element_id { match element_id {
0x0A45DFA3 => Ok(WebmElement::EbmlHead), EBML_HEAD_ID => Ok(WebmElement::EbmlHead),
0x08538067 => Ok(WebmElement::Segment), VOID_ID => Ok(WebmElement::Void),
0x014D9B74 => Ok(WebmElement::SeekHead), SEGMENT_ID => Ok(WebmElement::Segment),
0x0F43B675 => Ok(WebmElement::Cluster(bytes)), SEEK_HEAD_ID => Ok(WebmElement::SeekHead),
SEGMENT_INFO_ID => Ok(WebmElement::Info),
CUES_ID => Ok(WebmElement::Cues),
TRACKS_ID => Ok(WebmElement::Tracks(bytes)),
CLUSTER_ID => Ok(WebmElement::Cluster(bytes)),
_ => Ok(WebmElement::Unknown(element_id)) _ => Ok(WebmElement::Unknown(element_id))
} }
} }
@ -299,14 +316,19 @@ mod tests {
fn decode_webm_test1() { fn decode_webm_test1() {
let source = &TEST_FILE; let source = &TEST_FILE;
let mut iter = Ebml(Webm, source).into_iter(); let mut iter = Ebml(Webm, source).into_iter();
// EBML Header
// test that we match the structure of the test file
assert_eq!(iter.next(), Some(WebmElement::EbmlHead)); assert_eq!(iter.next(), Some(WebmElement::EbmlHead));
// Segment
assert_eq!(iter.next(), Some(WebmElement::Segment)); assert_eq!(iter.next(), Some(WebmElement::Segment));
// SeekHead
assert_eq!(iter.next(), Some(WebmElement::SeekHead)); assert_eq!(iter.next(), Some(WebmElement::SeekHead));
assert_eq!(iter.next(), Some(WebmElement::Void));
assert_eq!(iter.next(), Some(WebmElement::Info));
assert_eq!(iter.next(), Some(WebmElement::Tracks(&TEST_FILE[358..421])));
assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[433..13739])));
assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[13751..34814])));
assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[34826..56114])));
assert_eq!(iter.next(), Some(WebmElement::Cues));
assert_eq!(iter.next(), None);
} }
} }