Commit 90b046fb authored by François Laignel's avatar François Laignel Committed by Sebastian Dröge

serde: separate serde impl and tests from base module

+ fix erroneous `datetime` instead of `buffer` in `BufferRef::serialize`
+ remove ser/de for `Value`s with types `ULong` & `ILong`
parent 3f0cd6fb
......@@ -579,79 +579,6 @@ lazy_static! {
*BUFFER_COPY_METADATA | ::BufferCopyFlags::MEMORY;
}
#[cfg(feature = "ser_de")]
pub(crate) mod serde {
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer, SerializeStruct};
use serde_bytes::{Bytes, ByteBuf};
use Buffer;
use BufferFlags;
use BufferRef;
use ClockTime;
impl<'a> Serialize for BufferRef {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut datetime = serializer.serialize_struct("Buffer", 6)?;
datetime.serialize_field("pts", &self.get_pts())?;
datetime.serialize_field("dts", &self.get_dts())?;
datetime.serialize_field("duration", &self.get_duration())?;
datetime.serialize_field("offset", &self.get_offset())?;
datetime.serialize_field("offset_end", &self.get_offset_end())?;
datetime.serialize_field("flags", &self.get_flags())?;
{
let data = self.map_readable().unwrap();
datetime.serialize_field("buffer", &Bytes::new(data.as_slice()))?;
}
datetime.end()
}
}
impl<'a> Serialize for Buffer {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_ref().serialize(serializer)
}
}
#[derive(Deserialize)]
struct BufferDe {
pts: ClockTime,
dts: ClockTime,
duration: ClockTime,
offset: u64,
offset_end: u64,
flags: BufferFlags,
buffer: ByteBuf,
}
impl From<BufferDe> for Buffer {
fn from(mut buf_de: BufferDe) -> Self {
// Move the `ByteBuff`'s byte array into the `gst::Buffer`
// Are we really avoiding copies with that?
let drained_buffer: &mut Vec<u8> = buf_de.buffer.as_mut();
let mut buffer =
Buffer::from_slice(drained_buffer.drain(..).collect::<Vec<u8>>()).unwrap();
{
let buffer = buffer.get_mut().unwrap();
buffer.set_pts(buf_de.pts);
buffer.set_dts(buf_de.dts);
buffer.set_duration(buf_de.duration);
buffer.set_offset(buf_de.offset);
buffer.set_offset_end(buf_de.offset_end);
buffer.set_flags(buf_de.flags);
}
buffer
}
}
impl<'de> Deserialize<'de> for Buffer {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
BufferDe::deserialize(deserializer)
.and_then(|buffer_de| Ok(buffer_de.into()))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
......@@ -723,165 +650,4 @@ mod tests {
assert_eq!(data.as_slice(), vec![0, 2, 3, 4].as_slice());
}
}
#[cfg(feature = "ser_de")]
#[test]
fn test_serialize() {
extern crate ron;
extern crate serde_json;
extern crate serde_pickle;
use Buffer;
use BufferFlags;
::init().unwrap();
let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]).unwrap();
{
let buffer = buffer.get_mut().unwrap();
buffer.set_pts(1.into());
buffer.set_offset(3);
buffer.set_offset_end(4);
buffer.set_duration(5.into());
buffer.set_flags(BufferFlags::LIVE | BufferFlags::LAST);
}
// don't use newlines
let mut pretty_config = ron::ser::PrettyConfig::default();
pretty_config.new_line = "".to_string();
let res = ron::ser::to_string_pretty(&buffer, pretty_config);
assert_eq!(
Ok(
concat!(
"(",
" pts: Some(1),",
" dts: None,",
" duration: Some(5),",
" offset: 3,",
" offset_end: 4,",
" flags: (",
" bits: 1048592,",
" ),",
" buffer: \"AQIDBA==\",",
")"
)
.to_owned()
),
res
);
let res = serde_json::to_string(&buffer).unwrap();
assert_eq!(
concat!(
"{",
"\"pts\":1,",
"\"dts\":null,",
"\"duration\":5,",
"\"offset\":3,",
"\"offset_end\":4,",
"\"flags\":{\"bits\":1048592},",
"\"buffer\":[1,2,3,4]",
"}"
)
.to_owned(),
res
);
let res = serde_pickle::to_vec(&buffer, true).unwrap();
assert_eq!(
vec![
128, 3, 125, 40, 88, 3, 0, 0, 0, 112, 116, 115, 74, 1, 0, 0, 0, 88, 3, 0, 0, 0, 100,
116, 115, 78, 88, 8, 0, 0, 0, 100, 117, 114, 97, 116, 105, 111, 110, 74, 5, 0, 0, 0,
88, 6, 0, 0, 0, 111, 102, 102, 115, 101, 116, 74, 3, 0, 0, 0, 88, 10, 0, 0, 0, 111,
102, 102, 115, 101, 116, 95, 101, 110, 100, 74, 4, 0, 0, 0, 88, 5, 0, 0, 0, 102, 108,
97, 103, 115, 125, 40, 88, 4, 0, 0, 0, 98, 105, 116, 115, 74, 16, 0, 16, 0, 117, 88,
6, 0, 0, 0, 98, 117, 102, 102, 101, 114, 67,
4, 1, 2, 3, 4, 117, 46
],
res
);
}
#[cfg(feature = "ser_de")]
#[test]
fn test_deserialize() {
extern crate ron;
extern crate serde_json;
extern crate serde_pickle;
use Buffer;
use BufferFlags;
::init().unwrap();
let buffer_ron = r#"
(
pts: Some(1),
dts: None,
duration: Some(5),
offset: 3,
offset_end: 4,
flags: (
bits: 1048592,
),
buffer: "AQIDBA==",
)
"#;
let buffer: Buffer = ron::de::from_str(buffer_ron).unwrap();
assert_eq!(buffer.get_pts(), 1.into());
assert_eq!(buffer.get_dts(), None.into());
assert_eq!(buffer.get_offset(), 3);
assert_eq!(buffer.get_offset_end(), 4);
assert_eq!(buffer.get_duration(), 5.into());
assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::LAST);
{
let data = buffer.map_readable().unwrap();
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
}
let buffer_json = r#"
{
"pts":1,
"dts":null,
"duration":5,
"offset":3,
"offset_end":4,
"flags":{"bits":1048592},
"buffer":[1,2,3,4]
}
"#;
let buffer: Buffer = serde_json::from_str(buffer_json).unwrap();
assert_eq!(buffer.get_pts(), 1.into());
assert_eq!(buffer.get_dts(), None.into());
assert_eq!(buffer.get_offset(), 3);
assert_eq!(buffer.get_offset_end(), 4);
assert_eq!(buffer.get_duration(), 5.into());
assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::LAST);
{
let data = buffer.map_readable().unwrap();
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
}
let buffer_pickle: &[u8] = &[
128, 3, 125, 40, 88, 3, 0, 0, 0, 112, 116, 115, 74, 1, 0, 0, 0, 88, 3, 0, 0, 0, 100,
116, 115, 78, 88, 8, 0, 0, 0, 100, 117, 114, 97, 116, 105, 111, 110, 74, 5, 0, 0, 0,
88, 6, 0, 0, 0, 111, 102, 102, 115, 101, 116, 74, 3, 0, 0, 0, 88, 10, 0, 0, 0, 111,
102, 102, 115, 101, 116, 95, 101, 110, 100, 74, 4, 0, 0, 0, 88, 5, 0, 0, 0, 102, 108,
97, 103, 115, 125, 40, 88, 4, 0, 0, 0, 98, 105, 116, 115, 74, 16, 0, 16, 0, 117, 88,
6, 0, 0, 0, 98, 117, 102, 102, 101, 114, 67,
4, 1, 2, 3, 4, 117, 46
];
let buffer: Buffer = serde_pickle::from_slice(buffer_pickle).unwrap();
assert_eq!(buffer.get_pts(), 1.into());
assert_eq!(buffer.get_dts(), None.into());
assert_eq!(buffer.get_offset(), 3);
assert_eq!(buffer.get_offset_end(), 4);
assert_eq!(buffer.get_duration(), 5.into());
assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::LAST);
{
let data = buffer.map_readable().unwrap();
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
}
}
}
// Copyright (C) 2018 François Laignel <fengalin@free.fr>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer, SerializeStruct};
use serde_bytes::{Bytes, ByteBuf};
use Buffer;
use BufferFlags;
use BufferRef;
use ClockTime;
impl<'a> Serialize for BufferRef {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut buffer = serializer.serialize_struct("Buffer", 6)?;
buffer.serialize_field("pts", &self.get_pts())?;
buffer.serialize_field("dts", &self.get_dts())?;
buffer.serialize_field("duration", &self.get_duration())?;
buffer.serialize_field("offset", &self.get_offset())?;
buffer.serialize_field("offset_end", &self.get_offset_end())?;
buffer.serialize_field("flags", &self.get_flags())?;
{
let data = self.map_readable().unwrap();
buffer.serialize_field("buffer", &Bytes::new(data.as_slice()))?;
}
buffer.end()
}
}
impl<'a> Serialize for Buffer {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_ref().serialize(serializer)
}
}
#[derive(Deserialize)]
struct BufferDe {
pts: ClockTime,
dts: ClockTime,
duration: ClockTime,
offset: u64,
offset_end: u64,
flags: BufferFlags,
buffer: ByteBuf,
}
impl From<BufferDe> for Buffer {
fn from(mut buf_de: BufferDe) -> Self {
// Move the `ByteBuff`'s byte array into the `gst::Buffer`
// Are we really avoiding copies with that?
let drained_buffer: &mut Vec<u8> = buf_de.buffer.as_mut();
let mut buffer =
Buffer::from_slice(drained_buffer.drain(..).collect::<Vec<u8>>()).unwrap();
{
let buffer = buffer.get_mut().unwrap();
buffer.set_pts(buf_de.pts);
buffer.set_dts(buf_de.dts);
buffer.set_duration(buf_de.duration);
buffer.set_offset(buf_de.offset);
buffer.set_offset_end(buf_de.offset_end);
buffer.set_flags(buf_de.flags);
}
buffer
}
}
impl<'de> Deserialize<'de> for Buffer {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
BufferDe::deserialize(deserializer)
.and_then(|buffer_de| Ok(buffer_de.into()))
}
}
#[cfg(test)]
mod tests {
extern crate ron;
extern crate serde_json;
extern crate serde_pickle;
use Buffer;
use BufferFlags;
#[test]
fn test_serialize() {
::init().unwrap();
let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]).unwrap();
{
let buffer = buffer.get_mut().unwrap();
buffer.set_pts(1.into());
buffer.set_offset(3);
buffer.set_offset_end(4);
buffer.set_duration(5.into());
buffer.set_flags(BufferFlags::LIVE | BufferFlags::LAST);
}
let mut pretty_config = ron::ser::PrettyConfig::default();
pretty_config.new_line = "".to_string();
let res = ron::ser::to_string_pretty(&buffer, pretty_config);
assert_eq!(
Ok(
concat!(
"(",
" pts: Some(1),",
" dts: None,",
" duration: Some(5),",
" offset: 3,",
" offset_end: 4,",
" flags: (",
" bits: 1048592,",
" ),",
" buffer: \"AQIDBA==\",",
")"
)
.to_owned()
),
res
);
let res = serde_json::to_string(&buffer).unwrap();
assert_eq!(
concat!(
"{",
"\"pts\":1,",
"\"dts\":null,",
"\"duration\":5,",
"\"offset\":3,",
"\"offset_end\":4,",
"\"flags\":{\"bits\":1048592},",
"\"buffer\":[1,2,3,4]",
"}"
)
.to_owned(),
res
);
let res = serde_pickle::to_vec(&buffer, true).unwrap();
assert_eq!(
vec![
128, 3, 125, 40, 88, 3, 0, 0, 0, 112, 116, 115, 74, 1, 0, 0, 0, 88, 3, 0, 0, 0, 100,
116, 115, 78, 88, 8, 0, 0, 0, 100, 117, 114, 97, 116, 105, 111, 110, 74, 5, 0, 0, 0,
88, 6, 0, 0, 0, 111, 102, 102, 115, 101, 116, 74, 3, 0, 0, 0, 88, 10, 0, 0, 0, 111,
102, 102, 115, 101, 116, 95, 101, 110, 100, 74, 4, 0, 0, 0, 88, 5, 0, 0, 0, 102, 108,
97, 103, 115, 125, 40, 88, 4, 0, 0, 0, 98, 105, 116, 115, 74, 16, 0, 16, 0, 117, 88,
6, 0, 0, 0, 98, 117, 102, 102, 101, 114, 67,
4, 1, 2, 3, 4, 117, 46
],
res
);
}
#[test]
fn test_deserialize() {
::init().unwrap();
let buffer_ron = r#"
(
pts: Some(1),
dts: None,
duration: Some(5),
offset: 3,
offset_end: 4,
flags: (
bits: 1048592,
),
buffer: "AQIDBA==",
)
"#;
let buffer: Buffer = ron::de::from_str(buffer_ron).unwrap();
assert_eq!(buffer.get_pts(), 1.into());
assert_eq!(buffer.get_dts(), None.into());
assert_eq!(buffer.get_offset(), 3);
assert_eq!(buffer.get_offset_end(), 4);
assert_eq!(buffer.get_duration(), 5.into());
assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::LAST);
{
let data = buffer.map_readable().unwrap();
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
}
let buffer_json = r#"
{
"pts":1,
"dts":null,
"duration":5,
"offset":3,
"offset_end":4,
"flags":{"bits":1048592},
"buffer":[1,2,3,4]
}
"#;
let buffer: Buffer = serde_json::from_str(buffer_json).unwrap();
assert_eq!(buffer.get_pts(), 1.into());
assert_eq!(buffer.get_dts(), None.into());
assert_eq!(buffer.get_offset(), 3);
assert_eq!(buffer.get_offset_end(), 4);
assert_eq!(buffer.get_duration(), 5.into());
assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::LAST);
{
let data = buffer.map_readable().unwrap();
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
}
let buffer_pickle: &[u8] = &[
128, 3, 125, 40, 88, 3, 0, 0, 0, 112, 116, 115, 74, 1, 0, 0, 0, 88, 3, 0, 0, 0, 100,
116, 115, 78, 88, 8, 0, 0, 0, 100, 117, 114, 97, 116, 105, 111, 110, 74, 5, 0, 0, 0,
88, 6, 0, 0, 0, 111, 102, 102, 115, 101, 116, 74, 3, 0, 0, 0, 88, 10, 0, 0, 0, 111,
102, 102, 115, 101, 116, 95, 101, 110, 100, 74, 4, 0, 0, 0, 88, 5, 0, 0, 0, 102, 108,
97, 103, 115, 125, 40, 88, 4, 0, 0, 0, 98, 105, 116, 115, 74, 16, 0, 16, 0, 117, 88,
6, 0, 0, 0, 98, 117, 102, 102, 101, 114, 67,
4, 1, 2, 3, 4, 117, 46
];
let buffer: Buffer = serde_pickle::from_slice(buffer_pickle).unwrap();
assert_eq!(buffer.get_pts(), 1.into());
assert_eq!(buffer.get_dts(), None.into());
assert_eq!(buffer.get_offset(), 3);
assert_eq!(buffer.get_offset_end(), 4);
assert_eq!(buffer.get_duration(), 5.into());
assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::LAST);
{
let data = buffer.map_readable().unwrap();
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
}
}
}
......@@ -193,195 +193,3 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
}
impl<'a> ExactSizeIterator for Iter<'a> {}
#[cfg(feature = "ser_de")]
pub(crate) mod serde {
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use serde::ser::{Serialize, Serializer, SerializeSeq};
use std::fmt;
use Buffer;
use BufferList;
use BufferListRef;
impl Serialize for BufferListRef {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut iter = self.iter();
let (remaining, _) = iter.size_hint();
if remaining > 0 {
let mut seq = serializer.serialize_seq(Some(remaining))?;
while let Some(ref buffer) = iter.next() {
seq.serialize_element(buffer)?;
}
seq.end()
} else {
let seq = serializer.serialize_seq(None)?;
seq.end()
}
}
}
impl Serialize for BufferList {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_ref().serialize(serializer)
}
}
struct BufferListVisitor;
impl<'de> Visitor<'de> for BufferListVisitor {
type Value = BufferList;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a sequence of Buffers")
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let mut buffer_list = BufferList::new();
{
let buffer_list = buffer_list.get_mut().unwrap();
while let Some(buffer) = seq.next_element::<Buffer>()? {
buffer_list.add(buffer);
}
}
Ok(buffer_list)
}
}
impl<'de> Deserialize<'de> for BufferList {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_seq(BufferListVisitor)
}
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "ser_de")]
#[test]
fn test_serialize() {
extern crate ron;
use Buffer;
use BufferList;
::init().unwrap();
let mut buffer_list = BufferList::new();
{
let buffer_list = buffer_list.get_mut().unwrap();
let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]).unwrap();
{
let buffer = buffer.get_mut().unwrap();
buffer.set_pts(1.into());
buffer.set_offset(0);
buffer.set_offset_end(4);
buffer.set_duration(4.into());
}
buffer_list.add(buffer);
let mut buffer = Buffer::from_slice(vec![5, 6]).unwrap();
{
let buffer = buffer.get_mut().unwrap();
buffer.set_pts(5.into());
buffer.set_offset(4);
buffer.set_offset_end(6);
buffer.set_duration(2.into());
}
buffer_list.add(buffer);
}
// don't use newlines
let mut pretty_config = ron::ser::PrettyConfig::default();
pretty_config.new_line = "".to_string();
let res = ron::ser::to_string_pretty(&buffer_list, pretty_config);
assert_eq!(
Ok(
concat!(
"[",
" (",
" pts: Some(1),",
" dts: None,",
" duration: Some(4),",
" offset: 0,",
" offset_end: 4,",
" flags: (",
" bits: 0,",
" ),",
" buffer: \"AQIDBA==\",",
" ),",
" (",
" pts: Some(5),",
" dts: None,",
" duration: Some(2),",
" offset: 4,",
" offset_end: 6,",
" flags: (",
" bits: 0,",
" ),",
" buffer: \"BQY=\",",
" ),",
"]"
)
.to_owned()
),
res,
);
}
#[cfg(feature = "ser_de")]
#[test]
fn test_deserialize() {
extern crate ron;
use BufferList;
::init().unwrap();
let buffer_list_ron = r#"
[
(
pts: Some(1),
dts: None,
duration: Some(4),
offset: 0,
offset_end: 4,
flags: (
bits: 0,
),
buffer: "AQIDBA==",
),
(
pts: Some(5),
dts: None,
duration: Some(2),
offset: 4,
offset_end: 6,
flags: (
bits: 0,
),
buffer: "BQY=",
),
]
"#;
let buffer_list: BufferList = ron::de::from_str(buffer_list_ron).unwrap();
let mut iter = buffer_list.iter();
let buffer = iter.next().unwrap();
assert_eq!(buffer.get_pts(), 1.into());
assert_eq!(buffer.get_dts(), None.into());
{
let data = buffer.map_readable().unwrap();
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
}
let buffer = iter.next().unwrap();
assert_eq!(buffer.get_pts(), 5.into());
{
let data = buffer.map_readable().unwrap();
assert_eq!(data.as_slice(), vec![5, 6].as_slice());
}
}
}
// Copyright (C) 2018 François Laignel <fengalin@free.fr>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use serde::ser::{Serialize, Serializer, SerializeSeq};
use std::fmt;
use Buffer;
use BufferList;
use BufferListRef;
impl Serialize for BufferListRef {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut iter = self.iter();
let (remaining, _) = iter.size_hint();
if remaining > 0 {
let mut seq = serializer.serialize_seq(Some(remaining))?;