WIP: update lib to bytes 0.5
This commit is contained in:
parent
f4f752548e
commit
1273b4adff
4 changed files with 47 additions and 40 deletions
|
@ -5,7 +5,8 @@ authors = ["Tangent 128 <Tangent128@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytes = "^0.4.12"
|
byteorder = "1"
|
||||||
|
bytes = "^0.5"
|
||||||
clap = "^2.33.0"
|
clap = "^2.33.0"
|
||||||
custom_error = "^1.7"
|
custom_error = "^1.7"
|
||||||
env_logger = "^0.7"
|
env_logger = "^0.7"
|
||||||
|
|
64
src/ebml.rs
64
src/ebml.rs
|
@ -1,6 +1,7 @@
|
||||||
use bytes::{BigEndian, ByteOrder, BufMut};
|
use byteorder::{BigEndian, ByteOrder};
|
||||||
|
use bytes::{BufMut};
|
||||||
use custom_error::custom_error;
|
use custom_error::custom_error;
|
||||||
use std::io::{Cursor, Error as IoError, ErrorKind, Result as IoResult, Write, Seek, SeekFrom};
|
use std::io::{Error as IoError, ErrorKind, Result as IoResult, Write, Seek, SeekFrom};
|
||||||
|
|
||||||
pub const EBML_HEAD_ID: u64 = 0x0A45DFA3;
|
pub const EBML_HEAD_ID: u64 = 0x0A45DFA3;
|
||||||
pub const DOC_TYPE_ID: u64 = 0x0282;
|
pub const DOC_TYPE_ID: u64 = 0x0282;
|
||||||
|
@ -135,10 +136,10 @@ pub fn encode_varint<T: Write>(varint: Varint, output: &mut T) -> IoResult<()> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut buffer = Cursor::new([0; 8]);
|
let mut buffer = [0; 8];
|
||||||
buffer.put_uint_be(number, size);
|
buffer.as_mut().put_uint(number, size);
|
||||||
|
|
||||||
return output.write_all(&buffer.get_ref()[..size]);
|
return output.write_all(&buffer[..size]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const FOUR_FLAG: u64 = 0x10 << (8*3);
|
const FOUR_FLAG: u64 = 0x10 << (8*3);
|
||||||
|
@ -154,10 +155,10 @@ pub fn encode_varint_4<T: Write>(varint: Varint, output: &mut T) -> IoResult<()>
|
||||||
Varint::Value(value) => FOUR_FLAG | value
|
Varint::Value(value) => FOUR_FLAG | value
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut buffer = Cursor::new([0; 4]);
|
let mut buffer = [0; 4];
|
||||||
buffer.put_u32_be(number as u32);
|
buffer.as_mut().put_u32(number as u32);
|
||||||
|
|
||||||
output.write_all(&buffer.get_ref()[..])
|
output.write_all(&buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encode_element<T: Write + Seek, F: Fn(&mut T) -> IoResult<X>, X>(tag: u64, output: &mut T, content: F) -> IoResult<()> {
|
pub fn encode_element<T: Write + Seek, F: Fn(&mut T) -> IoResult<X>, X>(tag: u64, output: &mut T, content: F) -> IoResult<()> {
|
||||||
|
@ -190,10 +191,10 @@ pub fn encode_bytes<T: Write>(tag: u64, bytes: &[u8], output: &mut T) -> IoResul
|
||||||
pub fn encode_integer<T: Write>(tag: u64, value: u64, output: &mut T) -> IoResult<()> {
|
pub fn encode_integer<T: Write>(tag: u64, value: u64, output: &mut T) -> IoResult<()> {
|
||||||
encode_tag_header(tag, Varint::Value(8), output)?;
|
encode_tag_header(tag, Varint::Value(8), output)?;
|
||||||
|
|
||||||
let mut buffer = Cursor::new([0; 8]);
|
let mut buffer = [0; 8];
|
||||||
buffer.put_u64_be(value);
|
buffer.as_mut().put_u64(value);
|
||||||
|
|
||||||
output.write_all(&buffer.get_ref()[..])
|
output.write_all(&buffer[..])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EbmlLayout {
|
pub struct EbmlLayout {
|
||||||
|
@ -262,11 +263,10 @@ pub trait FromEbml<'a>: Sized {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use bytes::{BytesMut};
|
use bytes::{BytesMut, buf::ext::BufMutExt};
|
||||||
use crate::ebml::*;
|
use crate::ebml::*;
|
||||||
use crate::ebml::EbmlError::{CorruptVarint, UnknownElementId};
|
use crate::ebml::EbmlError::{CorruptVarint, UnknownElementId};
|
||||||
use crate::ebml::Varint::{Unknown, Value};
|
use crate::ebml::Varint::{Unknown, Value};
|
||||||
use std::io::Cursor;
|
|
||||||
use crate::tests::TEST_FILE;
|
use crate::tests::TEST_FILE;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -298,44 +298,48 @@ mod tests {
|
||||||
fn encode_varints() {
|
fn encode_varints() {
|
||||||
let mut buffer = BytesMut::with_capacity(10).writer();
|
let mut buffer = BytesMut::with_capacity(10).writer();
|
||||||
|
|
||||||
let mut no_space = Cursor::new([0; 0]).writer();
|
let mut no_space = [0; 0];
|
||||||
assert_eq!(no_space.get_ref().remaining_mut(), 0);
|
let mut no_space_writer = no_space.as_mut().writer();
|
||||||
|
assert_eq!(no_space_writer.get_mut().remaining_mut(), 0);
|
||||||
|
|
||||||
let mut six_buffer = Cursor::new([0; 6]).writer();
|
let mut six_buffer = [0; 6];
|
||||||
assert_eq!(six_buffer.get_ref().remaining_mut(), 6);
|
let mut six_buffer_writer = six_buffer.as_mut().writer();
|
||||||
|
assert_eq!(six_buffer_writer.get_mut().remaining_mut(), 6);
|
||||||
|
|
||||||
// 1 byte
|
// 1 byte
|
||||||
encode_varint(Varint::Unknown, &mut buffer).unwrap();
|
encode_varint(Varint::Unknown, &mut buffer).unwrap();
|
||||||
assert_eq!(buffer.get_mut().split_to(1), &[0xFF].as_ref());
|
assert_eq!(buffer.get_mut().split_to(1), &[0xFF].as_ref());
|
||||||
assert_eq!(encode_varint(Varint::Unknown, &mut no_space).unwrap_err().kind(), ErrorKind::WriteZero);
|
assert_eq!(encode_varint(Varint::Unknown, &mut no_space_writer).unwrap_err().kind(), ErrorKind::WriteZero);
|
||||||
|
|
||||||
encode_varint(Varint::Value(0), &mut buffer).unwrap();
|
encode_varint(Varint::Value(0), &mut buffer).unwrap();
|
||||||
assert_eq!(buffer.get_mut().split_to(1), &[0x80 | 0].as_ref());
|
assert_eq!(buffer.get_mut().split_to(1), &[0x80 | 0].as_ref());
|
||||||
assert_eq!(encode_varint(Varint::Value(0), &mut no_space).unwrap_err().kind(), ErrorKind::WriteZero);
|
assert_eq!(encode_varint(Varint::Value(0), &mut no_space_writer).unwrap_err().kind(), ErrorKind::WriteZero);
|
||||||
|
|
||||||
encode_varint(Varint::Value(1), &mut buffer).unwrap();
|
encode_varint(Varint::Value(1), &mut buffer).unwrap();
|
||||||
assert_eq!(buffer.get_mut().split_to(1), &[0x80 | 1].as_ref());
|
assert_eq!(buffer.get_mut().split_to(1), &[0x80 | 1].as_ref());
|
||||||
assert_eq!(encode_varint(Varint::Value(1), &mut no_space).unwrap_err().kind(), ErrorKind::WriteZero);
|
assert_eq!(encode_varint(Varint::Value(1), &mut no_space_writer).unwrap_err().kind(), ErrorKind::WriteZero);
|
||||||
|
|
||||||
encode_varint(Varint::Value(126), &mut buffer).unwrap();
|
encode_varint(Varint::Value(126), &mut buffer).unwrap();
|
||||||
assert_eq!(buffer.get_mut().split_to(1), &[0xF0 | 126].as_ref());
|
assert_eq!(buffer.get_mut().split_to(1), &[0xF0 | 126].as_ref());
|
||||||
assert_eq!(encode_varint(Varint::Value(126), &mut no_space).unwrap_err().kind(), ErrorKind::WriteZero);
|
assert_eq!(encode_varint(Varint::Value(126), &mut no_space_writer).unwrap_err().kind(), ErrorKind::WriteZero);
|
||||||
|
|
||||||
// 2 bytes
|
// 2 bytes
|
||||||
encode_varint(Varint::Value(127), &mut buffer).unwrap();
|
encode_varint(Varint::Value(127), &mut buffer).unwrap();
|
||||||
assert_eq!(&buffer.get_mut().split_to(2), &[0x40, 127].as_ref());
|
assert_eq!(&buffer.get_mut().split_to(2), &[0x40, 127].as_ref());
|
||||||
assert_eq!(encode_varint(Varint::Value(127), &mut no_space).unwrap_err().kind(), ErrorKind::WriteZero);
|
assert_eq!(encode_varint(Varint::Value(127), &mut no_space_writer).unwrap_err().kind(), ErrorKind::WriteZero);
|
||||||
|
|
||||||
encode_varint(Varint::Value(128), &mut buffer).unwrap();
|
encode_varint(Varint::Value(128), &mut buffer).unwrap();
|
||||||
assert_eq!(&buffer.get_mut().split_to(2), &[0x40, 128].as_ref());
|
assert_eq!(&buffer.get_mut().split_to(2), &[0x40, 128].as_ref());
|
||||||
assert_eq!(encode_varint(Varint::Value(128), &mut no_space).unwrap_err().kind(), ErrorKind::WriteZero);
|
assert_eq!(encode_varint(Varint::Value(128), &mut no_space_writer).unwrap_err().kind(), ErrorKind::WriteZero);
|
||||||
|
|
||||||
// 6 bytes
|
// 6 bytes
|
||||||
assert_eq!(six_buffer.get_ref().remaining_mut(), 6);
|
assert_eq!(six_buffer_writer.get_mut().remaining_mut(), 6);
|
||||||
encode_varint(Varint::Value(0x03FFFFFFFFFE), &mut six_buffer).unwrap();
|
encode_varint(Varint::Value(0x03FFFFFFFFFE), &mut six_buffer_writer).unwrap();
|
||||||
assert_eq!(six_buffer.get_ref().remaining_mut(), 0);
|
assert_eq!(six_buffer_writer.get_mut().remaining_mut(), 0);
|
||||||
assert_eq!(&six_buffer.get_ref().get_ref(), &[0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE].as_ref());
|
assert_eq!(&six_buffer, &[0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE].as_ref());
|
||||||
six_buffer = Cursor::new([0; 6]).writer();
|
|
||||||
|
let mut six_buffer = [0; 6];
|
||||||
|
let mut six_buffer_writer = six_buffer.as_mut().writer();
|
||||||
|
|
||||||
// 7 bytes
|
// 7 bytes
|
||||||
encode_varint(Varint::Value(0x03FFFFFFFFFF), &mut buffer).unwrap();
|
encode_varint(Varint::Value(0x03FFFFFFFFFF), &mut buffer).unwrap();
|
||||||
|
@ -347,8 +351,8 @@ mod tests {
|
||||||
encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut buffer).unwrap();
|
encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut buffer).unwrap();
|
||||||
assert_eq!(&buffer.get_mut().split_to(7), &[0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE].as_ref());
|
assert_eq!(&buffer.get_mut().split_to(7), &[0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE].as_ref());
|
||||||
|
|
||||||
assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut no_space).unwrap_err().kind(), ErrorKind::WriteZero);
|
assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut no_space_writer).unwrap_err().kind(), ErrorKind::WriteZero);
|
||||||
assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut six_buffer).unwrap_err().kind(), ErrorKind::WriteZero);
|
assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut six_buffer_writer).unwrap_err().kind(), ErrorKind::WriteZero);
|
||||||
|
|
||||||
// 8 bytes
|
// 8 bytes
|
||||||
encode_varint(Varint::Value(0x01FFFFFFFFFFFF), &mut buffer).unwrap();
|
encode_varint(Varint::Value(0x01FFFFFFFFFFFF), &mut buffer).unwrap();
|
||||||
|
|
|
@ -112,7 +112,6 @@ impl<I: Buf, S: Stream<Item = Result<I, WebmetroError>> + Unpin> EbmlStreamingPa
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use bytes::IntoBuf;
|
|
||||||
use futures3::{future::poll_fn, stream::StreamExt, FutureExt};
|
use futures3::{future::poll_fn, stream::StreamExt, FutureExt};
|
||||||
use matches::assert_matches;
|
use matches::assert_matches;
|
||||||
use std::task::Poll::*;
|
use std::task::Poll::*;
|
||||||
|
@ -131,7 +130,7 @@ mod tests {
|
||||||
];
|
];
|
||||||
|
|
||||||
let mut stream_parser = futures3::stream::iter(pieces.iter())
|
let mut stream_parser = futures3::stream::iter(pieces.iter())
|
||||||
.map(|bytes| Ok(bytes.into_buf()))
|
.map(|bytes| Ok(&bytes[..]))
|
||||||
.parse_ebml();
|
.parse_ebml();
|
||||||
|
|
||||||
assert_matches!(
|
assert_matches!(
|
||||||
|
@ -183,7 +182,7 @@ mod tests {
|
||||||
|
|
||||||
async {
|
async {
|
||||||
let mut parser = futures3::stream::iter(pieces.iter())
|
let mut parser = futures3::stream::iter(pieces.iter())
|
||||||
.map(|bytes| Ok(bytes.into_buf()))
|
.map(|bytes| Ok(&bytes[..]))
|
||||||
.parse_ebml();
|
.parse_ebml();
|
||||||
|
|
||||||
assert_matches!(parser.next().await?, Some(WebmElement::EbmlHead));
|
assert_matches!(parser.next().await?, Some(WebmElement::EbmlHead));
|
||||||
|
|
15
src/webm.rs
15
src/webm.rs
|
@ -1,5 +1,6 @@
|
||||||
use std::io::{Cursor, Error as IoError, ErrorKind, Result as IoResult, Write, Seek};
|
use std::io::{Error as IoError, ErrorKind, Result as IoResult, Write, Seek};
|
||||||
use bytes::{BigEndian, BufMut, ByteOrder};
|
use byteorder::{BigEndian, ByteOrder};
|
||||||
|
use bytes::BufMut;
|
||||||
use crate::ebml::*;
|
use crate::ebml::*;
|
||||||
use crate::iterator::ebml_iter;
|
use crate::iterator::ebml_iter;
|
||||||
use crate::iterator::EbmlIterator;
|
use crate::iterator::EbmlIterator;
|
||||||
|
@ -103,11 +104,12 @@ pub fn encode_simple_block<T: Write>(block: SimpleBlock, output: &mut T) -> IoRe
|
||||||
|
|
||||||
encode_varint(Varint::Value(track), output)?;
|
encode_varint(Varint::Value(track), output)?;
|
||||||
|
|
||||||
let mut buffer = Cursor::new([0; 3]);
|
let mut buffer = [0; 3];
|
||||||
buffer.put_i16_be(timecode);
|
let mut cursor = buffer.as_mut();
|
||||||
buffer.put_u8(flags);
|
cursor.put_i16(timecode);
|
||||||
|
cursor.put_u8(flags);
|
||||||
|
|
||||||
output.write_all(&buffer.get_ref()[..])?;
|
output.write_all(&buffer)?;
|
||||||
output.write_all(data)
|
output.write_all(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,6 +133,7 @@ pub fn encode_webm_element<T: Write + Seek>(element: WebmElement, output: &mut T
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::io::Cursor;
|
||||||
use crate::tests::{
|
use crate::tests::{
|
||||||
TEST_FILE,
|
TEST_FILE,
|
||||||
ENCODE_WEBM_TEST_FILE
|
ENCODE_WEBM_TEST_FILE
|
||||||
|
|
Loading…
Reference in a new issue