From 6ab32205bba834ce0e089422fd5925c522c2b0b1 Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Tue, 17 Jan 2017 03:00:09 -0500 Subject: [PATCH] Change Schema to be implemented by a dedicated type instead of its element type. --- src/lib.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dd5d75e..679d27b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,17 +93,22 @@ pub fn decode_tag(bytes: &[u8]) -> Result, Error> { } } -pub trait Schema<'a>: Sized { +pub trait Schema<'a> { + type Element; fn should_unwrap(element_id: u64) -> bool; - fn decode<'b: 'a>(element_id: u64, bytes: &'b[u8]) -> Result; + fn decode<'b: 'a>(element_id: u64, bytes: &'b[u8]) -> Result; } +pub struct Webm; + #[derive(Debug, PartialEq)] pub enum WebmElement<'a> { Unknown(u64, &'a[u8]) } -impl<'a> Schema<'a> for WebmElement<'a> { +impl<'a> Schema<'a> for Webm { + type Element = WebmElement<'a>; + fn should_unwrap(element_id: u64) -> bool { false } @@ -114,12 +119,12 @@ impl<'a> Schema<'a> for WebmElement<'a> { } } -pub fn decode_element<'a, 'b: 'a, T: Schema<'a>>(bytes: &'b[u8]) -> Result, Error> { +pub fn decode_element<'a, 'b: 'a, T: Schema<'a>>(bytes: &'b[u8]) -> Result, Error> { match decode_tag(bytes) { Ok(None) => Ok(None), Err(err) => Err(err), Ok(Some((element_id, payload_size_tag, tag_size))) => { - let should_unwrap = ::should_unwrap(element_id); + let should_unwrap = T::should_unwrap(element_id); let payload_size = match (should_unwrap, payload_size_tag) { (true, _) => 0, @@ -133,7 +138,7 @@ pub fn decode_element<'a, 'b: 'a, T: Schema<'a>>(bytes: &'b[u8]) -> Result Ok(Some((element, element_size))), Err(error) => Err(error) } @@ -212,7 +217,7 @@ mod tests { #[test] fn decode_sanity_test() { - let decoded = decode_element(TEST_FILE); + let decoded = decode_element::(TEST_FILE); if let Ok(Some((WebmElement::Unknown(tag, slice), element_size))) = decoded { assert_eq!(tag, 0x0A45DFA3); // EBML tag, sans the length indicator bit assert_eq!(slice.len(), 31); // known header payload length