forked from mirrors/gstreamer-rs
Tags: ser/de: gather tags with the same name together
This commit is contained in:
parent
d856fedf06
commit
23307a4795
5 changed files with 296 additions and 236 deletions
|
@ -93,7 +93,9 @@ impl<'de> Visitor<'de> for FieldVisitor {
|
||||||
.ok_or(de::Error::custom("Expected a value for `Value` name"))?;
|
.ok_or(de::Error::custom("Expected a value for `Value` name"))?;
|
||||||
let type_name = seq.next_element::<String>()?
|
let type_name = seq.next_element::<String>()?
|
||||||
.ok_or(de::Error::custom("Expected a value for `Value` type"))?;
|
.ok_or(de::Error::custom("Expected a value for `Value` type"))?;
|
||||||
Ok(FieldDe(name, de_send_value!(type_name, seq)))
|
let send_value = de_send_value!(type_name, seq)?
|
||||||
|
.ok_or(de::Error::custom("Expected a value for `Value`"))?;
|
||||||
|
Ok(FieldDe(name, send_value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +105,7 @@ impl<'de> Deserialize<'de> for FieldDe {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: use DeserializeSeed instead
|
||||||
// Use `NamelessStructure` to deserialize the `Field`s and
|
// Use `NamelessStructure` to deserialize the `Field`s and
|
||||||
// to add them to the `Structure` at the same time.
|
// to add them to the `Structure` at the same time.
|
||||||
struct NamelessStructure(Structure);
|
struct NamelessStructure(Structure);
|
||||||
|
|
|
@ -39,7 +39,7 @@ macro_rules! impl_tag(
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref $rust_tag: &'static str =
|
pub static ref $rust_tag: &'static str =
|
||||||
unsafe { CStr::from_ptr(ffi::$gst_tag).to_str().unwrap() };
|
unsafe { CStr::from_ptr(ffi::$gst_tag).to_str().unwrap() };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,107 +8,102 @@
|
||||||
|
|
||||||
use ffi;
|
use ffi;
|
||||||
use glib;
|
use glib;
|
||||||
use glib::translate::{ToGlibPtr, from_glib, from_glib_none};
|
use glib::translate::{ToGlibPtr, from_glib};
|
||||||
use glib::{ToValue, Value};
|
use glib::{SendValue, ToValue};
|
||||||
use gobject_ffi;
|
|
||||||
|
|
||||||
use serde::de;
|
use serde::de;
|
||||||
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
|
use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
|
||||||
use serde::ser;
|
use serde::ser;
|
||||||
use serde::ser::{Serialize, Serializer, SerializeSeq, SerializeTuple};
|
use serde::ser::{Serialize, Serializer, SerializeSeq, SerializeTuple};
|
||||||
|
|
||||||
use std::ffi::CStr;
|
use std::cell::RefCell;
|
||||||
use std::ffi::CString;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Sample;
|
use Sample;
|
||||||
use miniobject::MiniObject;
|
use TagMergeMode;
|
||||||
use tags::*;
|
use tags::*;
|
||||||
use value_serde::{DATE_TIME_OTHER_TYPE_ID, SAMPLE_OTHER_TYPE_ID};
|
use value_serde::{DATE_TIME_OTHER_TYPE_ID, SAMPLE_OTHER_TYPE_ID};
|
||||||
|
|
||||||
struct TagSer<'a>(&'a str, Value);
|
|
||||||
|
|
||||||
macro_rules! ser_tag (
|
macro_rules! ser_tag (
|
||||||
($tag_ser:ident, $ser:ident, $t:ty) => (
|
($value:ident, $seq:ident, $t:ty) => (
|
||||||
ser_value!($tag_ser.1, $tag_ser.0, $t, |tag_name, value| {
|
ser_value!($value, (), $t, |_, value| {
|
||||||
let mut tup = $ser.serialize_tuple(2)?;
|
$seq.serialize_element(&value)
|
||||||
tup.serialize_element(tag_name)?;
|
|
||||||
tup.serialize_element(&value)?;
|
|
||||||
tup.end()
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
);
|
);
|
||||||
|
|
||||||
impl<'a> Serialize for TagSer<'a> {
|
// serialize trait is only available for `&self`, but we need to mutate the iterator
|
||||||
|
struct TagValuesSer<'a>(Rc<RefCell<GenericTagIterator<'a>>>);
|
||||||
|
impl<'a> TagValuesSer<'a> {
|
||||||
|
fn from(tags_ser: &TagsSer<'a>) -> Self {
|
||||||
|
TagValuesSer(Rc::clone(&tags_ser.1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Serialize for TagValuesSer<'a> {
|
||||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
match self.1.type_() {
|
let mut tag_iter = self.0.borrow_mut();
|
||||||
glib::Type::F64 => ser_tag!(self, serializer, f64),
|
let mut seq = serializer.serialize_seq(tag_iter.size_hint().1)?;
|
||||||
glib::Type::String => ser_tag!(self, serializer, String),
|
while let Some(value) = tag_iter.next() {
|
||||||
glib::Type::U32 => ser_tag!(self, serializer, u32),
|
match value.type_() {
|
||||||
glib::Type::U64 => ser_tag!(self, serializer, u64),
|
glib::Type::F64 => ser_tag!(value, seq, f64),
|
||||||
glib::Type::Other(type_id) => {
|
glib::Type::String => ser_tag!(value, seq, String),
|
||||||
if *DATE_TIME_OTHER_TYPE_ID == type_id {
|
glib::Type::U32 => ser_tag!(value, seq, u32),
|
||||||
ser_tag!(self, serializer, DateTime)
|
glib::Type::U64 => ser_tag!(value, seq, u64),
|
||||||
} else if *SAMPLE_OTHER_TYPE_ID == type_id {
|
glib::Type::Other(type_id) => {
|
||||||
ser_tag!(self, serializer, Sample)
|
if *DATE_TIME_OTHER_TYPE_ID == type_id {
|
||||||
} else {
|
ser_tag!(value, seq, DateTime)
|
||||||
|
} else if *SAMPLE_OTHER_TYPE_ID == type_id {
|
||||||
|
ser_tag!(value, seq, Sample)
|
||||||
|
} else {
|
||||||
|
Err(
|
||||||
|
ser::Error::custom(
|
||||||
|
format!("unimplemented `Tag` serialization for type {}",
|
||||||
|
glib::Type::Other(type_id),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type_ => {
|
||||||
Err(
|
Err(
|
||||||
ser::Error::custom(
|
ser::Error::custom(
|
||||||
format!("unimplemented Tag serialization for type {}",
|
format!("unimplemented `Tag` serialization for type {}", type_)
|
||||||
glib::Type::Other(type_id),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}?;
|
||||||
type_ => {
|
|
||||||
Err(
|
|
||||||
ser::Error::custom(
|
|
||||||
format!("unimplemented Tag serialization for type {}", type_)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
seq.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TagsSer<'a>(&'a str, Rc<RefCell<GenericTagIterator<'a>>>);
|
||||||
|
impl<'a> TagsSer<'a> {
|
||||||
|
fn new(name: &'a str, tag_iter: GenericTagIterator<'a>) -> Self {
|
||||||
|
TagsSer(name, Rc::new(RefCell::new(tag_iter)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Serialize for TagsSer<'a> {
|
||||||
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
|
let mut tup = serializer.serialize_tuple(2)?;
|
||||||
|
tup.serialize_element(self.0)?;
|
||||||
|
tup.serialize_element(&TagValuesSer::from(&self))?;
|
||||||
|
tup.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for TagListRef {
|
impl Serialize for TagListRef {
|
||||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
let tag_count = unsafe { ffi::gst_tag_list_n_tags(self.as_ptr()) };
|
let tag_count = self.n_tags();
|
||||||
if tag_count > 0 {
|
if tag_count > 0 {
|
||||||
let mut seq = serializer.serialize_seq(Some(tag_count as usize))?;
|
let mut seq = serializer.serialize_seq(Some(tag_count as usize))?;
|
||||||
for name_index in 0..tag_count {
|
let tag_list_iter = self.iter_tag_list();
|
||||||
unsafe {
|
for (tag_name, tag_iter) in tag_list_iter {
|
||||||
let tag_name = ffi::gst_tag_list_nth_tag_name(self.as_ptr(), name_index as u32);
|
seq.serialize_element(&TagsSer::new(tag_name, tag_iter))?;
|
||||||
let tag_size = ffi::gst_tag_list_get_tag_size(self.as_ptr(), tag_name);
|
|
||||||
for tag_index in 0..tag_size {
|
|
||||||
let value = ffi::gst_tag_list_get_value_index(
|
|
||||||
self.as_ptr(),
|
|
||||||
tag_name,
|
|
||||||
tag_index,
|
|
||||||
);
|
|
||||||
|
|
||||||
if !value.is_null() {
|
|
||||||
let tag_name = CStr::from_ptr(tag_name)
|
|
||||||
.to_str()
|
|
||||||
.map_err(|_| {
|
|
||||||
ser::Error::custom(
|
|
||||||
format!(
|
|
||||||
"invalid UTF-8 characters in Tag name {:?}",
|
|
||||||
tag_name,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
seq.serialize_element(
|
|
||||||
&TagSer(
|
|
||||||
tag_name,
|
|
||||||
from_glib_none(value as *mut gobject_ffi::GValue),
|
|
||||||
)
|
|
||||||
)?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
seq.end()
|
seq.end()
|
||||||
} else if tag_count == 0 {
|
} else if tag_count == 0 {
|
||||||
|
@ -126,54 +121,45 @@ impl Serialize for TagList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TagDe(CString, Value);
|
|
||||||
struct TagVisitor;
|
|
||||||
|
|
||||||
macro_rules! de_tag_value(
|
macro_rules! de_tag_value(
|
||||||
($tag_name:expr, $seq:expr, $t:ty) => (
|
($tag_name:expr, $seq:expr, $t:ty) => (
|
||||||
{
|
de_send_value!("Tag", $tag_name, $seq, $t)
|
||||||
let value = de_value!("Tag", $tag_name, $seq, $t);
|
|
||||||
TagDe($tag_name, value)
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
);
|
);
|
||||||
|
|
||||||
impl<'de> Visitor<'de> for TagVisitor {
|
struct TagValues<'a>(&'a str, &'a mut TagListRef);
|
||||||
type Value = TagDe;
|
|
||||||
|
struct TagValuesVisitor<'a>(&'a str, &'a mut TagListRef);
|
||||||
|
impl<'de, 'a> Visitor<'de> for TagValuesVisitor<'a> {
|
||||||
|
type Value = ();
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
formatter.write_str("a tuple of 2 elements (name: String, value: Tag value type)")
|
formatter.write_str("a sequence of `Tag` values with the same type")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<(), A::Error> {
|
||||||
let name = CString::new(
|
let tag_type: glib::Type = unsafe {
|
||||||
seq
|
let tag_name = self.0.to_glib_none();
|
||||||
.next_element::<String>()
|
from_glib(ffi::gst_tag_get_type(tag_name.0))
|
||||||
.map_err(|err| de::Error::custom(
|
};
|
||||||
format!("Error reading Tag name. {:?}", err)
|
|
||||||
))?
|
|
||||||
.ok_or(de::Error::custom("Expected a value for Tag name"))?
|
|
||||||
.as_str()
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
unsafe {
|
loop {
|
||||||
let type_: glib::Type = from_glib(ffi::gst_tag_get_type(name.as_ptr() as *const i8));
|
let tag_value = match tag_type {
|
||||||
let tag_de = match type_ {
|
glib::Type::F64 => de_tag_value!(self.0, seq, f64),
|
||||||
glib::Type::F64 => de_tag_value!(name, seq, f64),
|
glib::Type::String => de_tag_value!(self.0, seq, String),
|
||||||
glib::Type::String => de_tag_value!(name, seq, String),
|
glib::Type::U32 => de_tag_value!(self.0, seq, u32),
|
||||||
glib::Type::U32 => de_tag_value!(name, seq, u32),
|
glib::Type::U64 => de_tag_value!(self.0, seq, u64),
|
||||||
glib::Type::U64 => de_tag_value!(name, seq, u64),
|
|
||||||
glib::Type::Other(type_id) => {
|
glib::Type::Other(type_id) => {
|
||||||
if *DATE_TIME_OTHER_TYPE_ID == type_id {
|
if *DATE_TIME_OTHER_TYPE_ID == type_id {
|
||||||
de_tag_value!(name, seq, DateTime)
|
de_tag_value!(self.0, seq, DateTime)
|
||||||
} else if *SAMPLE_OTHER_TYPE_ID == type_id {
|
} else if *SAMPLE_OTHER_TYPE_ID == type_id {
|
||||||
de_tag_value!(name, seq, Sample)
|
de_tag_value!(self.0, seq, Sample)
|
||||||
} else {
|
} else {
|
||||||
return Err(
|
return Err(
|
||||||
de::Error::custom(
|
de::Error::custom(
|
||||||
format!(
|
format!(
|
||||||
"unimplemented deserialization for Tag {:?} with type `{}`",
|
"unimplemented deserialization for `Tag` {} with type `{}`",
|
||||||
name,
|
self.0,
|
||||||
glib::Type::Other(type_id),
|
glib::Type::Other(type_id),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -184,23 +170,68 @@ impl<'de> Visitor<'de> for TagVisitor {
|
||||||
return Err(
|
return Err(
|
||||||
de::Error::custom(
|
de::Error::custom(
|
||||||
format!(
|
format!(
|
||||||
"unimplemented deserialization for Tag {:?} with type `{}`",
|
"unimplemented deserialization for `Tag` {} with type `{}`",
|
||||||
name,
|
self.0,
|
||||||
type_,
|
type_,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
}?;
|
||||||
|
|
||||||
Ok(tag_de)
|
match tag_value {
|
||||||
|
Some(tag_value) => {
|
||||||
|
self.1.add_generic(self.0, &tag_value, TagMergeMode::Append)
|
||||||
|
.map_err(|_| de::Error::custom(format!(
|
||||||
|
"wrong value type for `Tag` {}",
|
||||||
|
self.0,
|
||||||
|
)))?
|
||||||
|
}
|
||||||
|
None => break,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for TagDe {
|
impl<'de, 'a> DeserializeSeed<'de> for TagValues<'a> {
|
||||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
type Value = ();
|
||||||
deserializer.deserialize_tuple(2, TagVisitor{}) // 2 items in the tuple: (name, value)
|
|
||||||
|
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<(), D::Error> {
|
||||||
|
deserializer.deserialize_seq(TagValuesVisitor(self.0, self.1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TagValuesTuple<'a>(&'a mut TagListRef);
|
||||||
|
|
||||||
|
struct TagValuesTupleVisitor<'a>(&'a mut TagListRef);
|
||||||
|
impl<'de, 'a> Visitor<'de> for TagValuesTupleVisitor<'a> {
|
||||||
|
type Value = ();
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
formatter.write_str(
|
||||||
|
"a tuple (`Tag` name: `String`, seq. of `Tag` values with the same type)"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<(), A::Error> {
|
||||||
|
let name = seq
|
||||||
|
.next_element::<String>()
|
||||||
|
.map_err(|err| de::Error::custom(
|
||||||
|
format!("Error reading Tag name. {:?}", err)
|
||||||
|
))?
|
||||||
|
.ok_or(de::Error::custom("Expected a name for the `Tag` name"))?;
|
||||||
|
seq.next_element_seed(TagValues(name.as_str(), self.0))?
|
||||||
|
.ok_or(de::Error::custom("Expected a seq of values for the `Tag`"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, 'a> DeserializeSeed<'de> for TagValuesTuple<'a> {
|
||||||
|
type Value = ();
|
||||||
|
|
||||||
|
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<(), D::Error> {
|
||||||
|
deserializer.deserialize_tuple(2, TagValuesTupleVisitor(self.0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,19 +240,15 @@ impl<'de> Visitor<'de> for TagListVisitor {
|
||||||
type Value = TagList;
|
type Value = TagList;
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
formatter.write_str("a sequence of Tags")
|
formatter.write_str("a sequence of `Tag`s")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
||||||
let tag_list = TagList::new();
|
let mut tag_list = TagList::new();
|
||||||
while let Some(tag_de) = seq.next_element::<TagDe>()? {
|
{
|
||||||
unsafe {
|
let tag_list = tag_list.get_mut().unwrap();
|
||||||
ffi::gst_tag_list_add_value(
|
while seq.next_element_seed(TagValuesTuple(tag_list))?.is_some() {
|
||||||
tag_list.as_mut_ptr(),
|
// tags are added in the dedicated deserializers
|
||||||
ffi::GST_TAG_MERGE_APPEND,
|
|
||||||
tag_de.0.as_ptr(),
|
|
||||||
tag_de.1.to_glib_none().0,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(tag_list)
|
Ok(tag_list)
|
||||||
|
@ -234,7 +261,6 @@ impl<'de> Deserialize<'de> for TagList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
extern crate ron;
|
extern crate ron;
|
||||||
|
@ -289,52 +315,64 @@ mod tests {
|
||||||
Ok(
|
Ok(
|
||||||
concat!(
|
concat!(
|
||||||
"[",
|
"[",
|
||||||
" (\"title\", \"a title\"),",
|
" (\"title\", [",
|
||||||
" (\"title\", \"another title\"),",
|
" \"a title\",",
|
||||||
" (\"duration\", 120000000000),",
|
" \"another title\",",
|
||||||
" (\"bitrate\", 96000),",
|
" ]),",
|
||||||
" (\"replaygain-track-gain\", 1),",
|
" (\"duration\", [",
|
||||||
" (\"datetime\", (",
|
" 120000000000,",
|
||||||
" tz_offset: 2,",
|
" ]),",
|
||||||
" y: 2018,",
|
" (\"bitrate\", [",
|
||||||
" m: 5,",
|
" 96000,",
|
||||||
" d: 28,",
|
" ]),",
|
||||||
" h: 16,",
|
" (\"replaygain-track-gain\", [",
|
||||||
" mn: 6,",
|
" 1,",
|
||||||
" s: 42,",
|
" ]),",
|
||||||
" us: 841000,",
|
" (\"datetime\", [",
|
||||||
" )),",
|
" (",
|
||||||
" (\"image\", (",
|
" tz_offset: 2,",
|
||||||
" buffer: Some((",
|
" y: 2018,",
|
||||||
" pts: None,",
|
" m: 5,",
|
||||||
" dts: None,",
|
" d: 28,",
|
||||||
" duration: None,",
|
" h: 16,",
|
||||||
" offset: 0,",
|
" mn: 6,",
|
||||||
" offset_end: 0,",
|
" s: 42,",
|
||||||
" flags: (",
|
" us: 841000,",
|
||||||
" bits: 0,",
|
" ),",
|
||||||
" ),",
|
" ]),",
|
||||||
" buffer: \"AQIDBA==\",",
|
" (\"image\", [",
|
||||||
" )),",
|
" (",
|
||||||
" buffer_list: None,",
|
" buffer: Some((",
|
||||||
" caps: None,",
|
" pts: None,",
|
||||||
" segment: Some((",
|
" dts: None,",
|
||||||
" flags: (",
|
" duration: None,",
|
||||||
" bits: 0,",
|
" offset: 0,",
|
||||||
" ),",
|
" offset_end: 0,",
|
||||||
" rate: 1,",
|
" flags: (",
|
||||||
" applied_rate: 1,",
|
" bits: 0,",
|
||||||
" format: Time,",
|
" ),",
|
||||||
" base: 0,",
|
" buffer: \"AQIDBA==\",",
|
||||||
" offset: 0,",
|
" )),",
|
||||||
" start: 0,",
|
" buffer_list: None,",
|
||||||
" stop: -1,",
|
" caps: None,",
|
||||||
" time: 0,",
|
" segment: Some((",
|
||||||
" position: 0,",
|
" flags: (",
|
||||||
" duration: -1,",
|
" bits: 0,",
|
||||||
" )),",
|
" ),",
|
||||||
" info: None,",
|
" rate: 1,",
|
||||||
" )),",
|
" applied_rate: 1,",
|
||||||
|
" format: Time,",
|
||||||
|
" base: 0,",
|
||||||
|
" offset: 0,",
|
||||||
|
" start: 0,",
|
||||||
|
" stop: -1,",
|
||||||
|
" time: 0,",
|
||||||
|
" position: 0,",
|
||||||
|
" duration: -1,",
|
||||||
|
" )),",
|
||||||
|
" info: None,",
|
||||||
|
" ),",
|
||||||
|
" ]),",
|
||||||
"]",
|
"]",
|
||||||
).to_owned()
|
).to_owned()
|
||||||
),
|
),
|
||||||
|
@ -350,38 +388,44 @@ mod tests {
|
||||||
|
|
||||||
let tag_list_ron = r#"
|
let tag_list_ron = r#"
|
||||||
[
|
[
|
||||||
("title", "a title"),
|
("title", [
|
||||||
("title", "another title"),
|
"a title",
|
||||||
("duration", 120000000000),
|
"another title",
|
||||||
("bitrate", 96000),
|
]),
|
||||||
("replaygain-track-gain", 1),
|
("duration", [120000000000]),
|
||||||
("datetime", (
|
("bitrate", [96000]),
|
||||||
tz_offset: 2,
|
("replaygain-track-gain", [1]),
|
||||||
y: 2018,
|
("datetime", [
|
||||||
m: 5,
|
(
|
||||||
d: 28,
|
tz_offset: 2,
|
||||||
h: 16,
|
y: 2018,
|
||||||
mn: 6,
|
m: 5,
|
||||||
s: 42,
|
d: 28,
|
||||||
us: 841000,
|
h: 16,
|
||||||
)),
|
mn: 6,
|
||||||
("image", (
|
s: 42,
|
||||||
buffer: Some((
|
us: 841000,
|
||||||
pts: None,
|
),
|
||||||
dts: None,
|
]),
|
||||||
duration: None,
|
("image", [
|
||||||
offset: 0,
|
(
|
||||||
offset_end: 0,
|
buffer: Some((
|
||||||
flags: (
|
pts: None,
|
||||||
bits: 0,
|
dts: None,
|
||||||
),
|
duration: None,
|
||||||
buffer: "AQIDBA==",
|
offset: 0,
|
||||||
)),
|
offset_end: 0,
|
||||||
buffer_list: None,
|
flags: (
|
||||||
caps: None,
|
bits: 0,
|
||||||
segment: None,
|
),
|
||||||
info: None,
|
buffer: "AQIDBA==",
|
||||||
))
|
)),
|
||||||
|
buffer_list: None,
|
||||||
|
caps: None,
|
||||||
|
segment: None,
|
||||||
|
info: None,
|
||||||
|
),
|
||||||
|
])
|
||||||
]
|
]
|
||||||
"#;
|
"#;
|
||||||
let tags: TagList = ron::de::from_str(tag_list_ron).unwrap();
|
let tags: TagList = ron::de::from_str(tag_list_ron).unwrap();
|
||||||
|
@ -402,13 +446,12 @@ mod tests {
|
||||||
|
|
||||||
let tag_json = r#"
|
let tag_json = r#"
|
||||||
[
|
[
|
||||||
["title", "a title"],
|
["title", ["a title", "another title"]],
|
||||||
["title", "another title"],
|
["duration", [120000000000]],
|
||||||
["duration", 120000000000],
|
["bitrate", [96000]],
|
||||||
["bitrate", 96000],
|
["replaygain-track-gain", [1.0]],
|
||||||
["replaygain-track-gain", 1.0],
|
["datetime",[{"tz_offset":2.0,"y":2018,"m":5,"d":28,"h":16,"mn":6,"s":42,"us":841000}]],
|
||||||
["datetime",{"tz_offset":2.0,"y":2018,"m":5,"d":28,"h":16,"mn":6,"s":42,"us":841000}],
|
["image",[{"buffer":{"pts":null,"dts":null,"duration":null,"offset":0,"offset_end":0,"flags":{"bits":0},"buffer":[1,2,3,4]},"buffer_list":null,"caps":null,"segment":null,"info":null}]]
|
||||||
["image",{"buffer":{"pts":null,"dts":null,"duration":null,"offset":0,"offset_end":0,"flags":{"bits":0},"buffer":[1,2,3,4]},"buffer_list":null,"caps":null,"segment":null,"info":null}]
|
|
||||||
]
|
]
|
||||||
"#;
|
"#;
|
||||||
let tags: TagList = serde_json::from_str(tag_json).unwrap();
|
let tags: TagList = serde_json::from_str(tag_json).unwrap();
|
||||||
|
|
|
@ -199,7 +199,9 @@ mod tests {
|
||||||
"(",
|
"(",
|
||||||
" scope: Global,",
|
" scope: Global,",
|
||||||
" tags: Some([",
|
" tags: Some([",
|
||||||
" (\"title\", \"toc\"),",
|
" (\"title\", [",
|
||||||
|
" \"toc\",",
|
||||||
|
" ]),",
|
||||||
" ]),",
|
" ]),",
|
||||||
" entries: [",
|
" entries: [",
|
||||||
" (",
|
" (",
|
||||||
|
@ -221,7 +223,9 @@ mod tests {
|
||||||
" uid: \"chapter1.1\",",
|
" uid: \"chapter1.1\",",
|
||||||
" start_stop: Some((0, 4)),",
|
" start_stop: Some((0, 4)),",
|
||||||
" tags: Some([",
|
" tags: Some([",
|
||||||
" (\"title\", \"chapter 1.1\"),",
|
" (\"title\", [",
|
||||||
|
" \"chapter 1.1\",",
|
||||||
|
" ]),",
|
||||||
" ]),",
|
" ]),",
|
||||||
" loop_: Some((None, 0)),",
|
" loop_: Some((None, 0)),",
|
||||||
" sub_entries: [",
|
" sub_entries: [",
|
||||||
|
@ -232,7 +236,9 @@ mod tests {
|
||||||
" uid: \"chapter1.2\",",
|
" uid: \"chapter1.2\",",
|
||||||
" start_stop: Some((4, 10)),",
|
" start_stop: Some((4, 10)),",
|
||||||
" tags: Some([",
|
" tags: Some([",
|
||||||
" (\"title\", \"chapter 1.2\"),",
|
" (\"title\", [",
|
||||||
|
" \"chapter 1.2\",",
|
||||||
|
" ]),",
|
||||||
" ]),",
|
" ]),",
|
||||||
" loop_: Some((None, 0)),",
|
" loop_: Some((None, 0)),",
|
||||||
" sub_entries: [",
|
" sub_entries: [",
|
||||||
|
@ -245,7 +251,9 @@ mod tests {
|
||||||
" uid: \"chapter2\",",
|
" uid: \"chapter2\",",
|
||||||
" start_stop: Some((10, 15)),",
|
" start_stop: Some((10, 15)),",
|
||||||
" tags: Some([",
|
" tags: Some([",
|
||||||
" (\"title\", \"chapter 2\"),",
|
" (\"title\", [",
|
||||||
|
" \"chapter 2\",",
|
||||||
|
" ]),",
|
||||||
" ]),",
|
" ]),",
|
||||||
" loop_: Some((None, 0)),",
|
" loop_: Some((None, 0)),",
|
||||||
" sub_entries: [",
|
" sub_entries: [",
|
||||||
|
@ -271,7 +279,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
scope: Global,
|
scope: Global,
|
||||||
tags: Some([
|
tags: Some([
|
||||||
("title", "toc"),
|
("title", ["toc"]),
|
||||||
]),
|
]),
|
||||||
entries: [
|
entries: [
|
||||||
(
|
(
|
||||||
|
@ -293,7 +301,7 @@ mod tests {
|
||||||
uid: "chapter1.1",
|
uid: "chapter1.1",
|
||||||
start_stop: Some((0, 4)),
|
start_stop: Some((0, 4)),
|
||||||
tags: Some([
|
tags: Some([
|
||||||
("title", "chapter 1.1"),
|
("title", ["chapter 1.1"]),
|
||||||
]),
|
]),
|
||||||
loop_: Some((None, 0)),
|
loop_: Some((None, 0)),
|
||||||
sub_entries: [
|
sub_entries: [
|
||||||
|
@ -304,7 +312,7 @@ mod tests {
|
||||||
uid: "chapter1.2",
|
uid: "chapter1.2",
|
||||||
start_stop: Some((4, 10)),
|
start_stop: Some((4, 10)),
|
||||||
tags: Some([
|
tags: Some([
|
||||||
("title", "chapter 1.2"),
|
("title", ["chapter 1.2"]),
|
||||||
]),
|
]),
|
||||||
loop_: Some((None, 0)),
|
loop_: Some((None, 0)),
|
||||||
sub_entries: [
|
sub_entries: [
|
||||||
|
@ -317,7 +325,7 @@ mod tests {
|
||||||
uid: "chapter2",
|
uid: "chapter2",
|
||||||
start_stop: Some((10, 15)),
|
start_stop: Some((10, 15)),
|
||||||
tags: Some([
|
tags: Some([
|
||||||
("title", "chapter 2"),
|
("title", ["chapter 2"]),
|
||||||
]),
|
]),
|
||||||
loop_: Some((None, 0)),
|
loop_: Some((None, 0)),
|
||||||
sub_entries: [
|
sub_entries: [
|
||||||
|
|
|
@ -163,32 +163,36 @@ impl_ser_send_value_collection!(Array);
|
||||||
impl_ser_send_value_collection!(List);
|
impl_ser_send_value_collection!(List);
|
||||||
|
|
||||||
macro_rules! de_value(
|
macro_rules! de_value(
|
||||||
($outer_type:expr, $type_name:expr, $seq:expr, $t:ty) => (
|
($seq:expr, $t:ty) => (
|
||||||
$seq
|
{
|
||||||
.next_element::<$t>()?
|
let value = $seq
|
||||||
.ok_or_else(||
|
.next_element::<$t>()?
|
||||||
de::Error::custom(format!(
|
.and_then(|base_value| Some(base_value.to_value()));
|
||||||
"Expected a value for `{}` with type {:?}",
|
Ok(value)
|
||||||
$outer_type,
|
}
|
||||||
$type_name,
|
|
||||||
))
|
|
||||||
)?
|
|
||||||
.to_value()
|
|
||||||
);
|
);
|
||||||
);
|
);
|
||||||
|
|
||||||
macro_rules! de_send_value(
|
macro_rules! de_send_value(
|
||||||
($type_name:expr, $seq:expr, $t:ty) => (
|
($type_name:expr, $seq:expr, $t:ty) => (
|
||||||
SendValue::from(
|
de_send_value!("Value", $type_name, $seq, $t)
|
||||||
de_value!("Value", $type_name, $seq, $t)
|
);
|
||||||
.try_into_send_value::<$t>()
|
($outer_type:expr, $type_name:expr, $seq:expr, $t:ty) => (
|
||||||
.map_err(|_|
|
match de_value!($seq, $t)? {
|
||||||
de::Error::custom(format!(
|
Some(value) => {
|
||||||
"Failed to convert `Value` with type {:?} to `SendValue`",
|
let glib_send_value = value
|
||||||
$type_name,
|
.try_into_send_value::<$t>()
|
||||||
))
|
.map_err(|_|
|
||||||
)?
|
de::Error::custom(format!(
|
||||||
)
|
"Failed to convert `{}` with type {:?} to `SendValue`",
|
||||||
|
$outer_type,
|
||||||
|
$type_name,
|
||||||
|
))
|
||||||
|
)?;
|
||||||
|
Ok(Some(SendValue::from(glib_send_value)))
|
||||||
|
}
|
||||||
|
None => Ok(None)
|
||||||
|
}
|
||||||
);
|
);
|
||||||
($type_name:expr, $seq:expr) => (
|
($type_name:expr, $seq:expr) => (
|
||||||
match $type_name.as_str() {
|
match $type_name.as_str() {
|
||||||
|
@ -233,7 +237,9 @@ impl<'de> Visitor<'de> for SendValueVisitor {
|
||||||
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
||||||
let type_name = seq.next_element::<String>()?
|
let type_name = seq.next_element::<String>()?
|
||||||
.ok_or(de::Error::custom("Expected a value for `Value` type"))?;
|
.ok_or(de::Error::custom("Expected a value for `Value` type"))?;
|
||||||
Ok(de_send_value!(type_name, seq))
|
let send_value = de_send_value!(type_name, seq)?
|
||||||
|
.ok_or(de::Error::custom("Expected a value for `Value`"))?;
|
||||||
|
Ok(send_value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue