Caps: serde: use a placeholder for CapsFeature

`CapsFeature` is not available in `gstreamer-rs` yet. Use a placeholder
with a `None` value for now.
This commit is contained in:
François Laignel 2018-07-22 15:48:26 +02:00 committed by Sebastian Dröge
parent fd0c3f4464
commit 0aac055727
2 changed files with 79 additions and 22 deletions

View file

@ -6,23 +6,38 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use serde::de;
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor}; use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use serde::ser::{Serialize, Serializer, SerializeSeq}; use serde::ser::{Serialize, Serializer, SerializeSeq, SerializeTuple};
use std::fmt; use std::fmt;
use Caps; use Caps;
use CapsRef; use CapsRef;
use Structure; use Structure;
use StructureRef;
impl<'a> Serialize for CapsRef { // `CapsFeature` is not available in `gstreamer-rs` yet
struct CapsItemSe<'a>(&'a StructureRef);
impl<'a> Serialize for CapsItemSe<'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)?;
// `CapsFeature` is not available in `gstreamer-rs` yet
// Fake the type for now and use `None` as a value
tup.serialize_element::<Option<Structure>>(&None)?;
tup.end()
}
}
impl Serialize for CapsRef {
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 iter = self.iter(); let iter = self.iter();
let size = iter.size_hint().0; let size = iter.size_hint().0;
if size > 0 { if size > 0 {
let mut seq = serializer.serialize_seq(Some(size))?; let mut seq = serializer.serialize_seq(Some(size))?;
for structure in iter { for structure in iter {
seq.serialize_element(structure)?; seq.serialize_element(&CapsItemSe(structure))?;
} }
seq.end() seq.end()
} else { } else {
@ -32,26 +47,65 @@ impl<'a> Serialize for CapsRef {
} }
} }
impl<'a> Serialize for Caps { impl Serialize for Caps {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_ref().serialize(serializer) self.as_ref().serialize(serializer)
} }
} }
// `CapsFeature` is not available in `gstreamer-rs` yet
struct CapsItemDe(Structure);
impl From<CapsItemDe> for Structure {
fn from(caps_item: CapsItemDe) -> Structure {
caps_item.0
}
}
struct CapsItemVisitor;
impl<'de> Visitor<'de> for CapsItemVisitor {
type Value = CapsItemDe;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a tuple `(Structure, Option<CapsFeature>)`")
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let structure = seq.next_element::<Structure>()?
.ok_or(de::Error::custom("Expected a `Structure` for `Caps` item"))?;
// `CapsFeature` is not available in `gstreamer-rs` yet
// Fake the type for now and expect `None` as a value
let feature_option = seq.next_element::<Option<Structure>>()?
.ok_or(de::Error::custom("Expected an `Option<CapsFeature>` for `Caps` item"))?;
if feature_option.is_some() {
Err(de::Error::custom(
"Found a value for `CapsFeature`, expected `None` (not implemented yet)"
))
} else {
Ok(CapsItemDe(structure))
}
}
}
impl<'de> Deserialize<'de> for CapsItemDe {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<CapsItemDe, D::Error> {
deserializer.deserialize_tuple(2, CapsItemVisitor)
}
}
struct CapsVisitor; struct CapsVisitor;
impl<'de> Visitor<'de> for CapsVisitor { impl<'de> Visitor<'de> for CapsVisitor {
type Value = Caps; type Value = Caps;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a sequence of `Structure`s") formatter.write_str("a sequence of `(Structure, Option<CapsFeature>)`")
} }
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 mut caps = Caps::new_empty(); let mut caps = Caps::new_empty();
{ {
let caps = caps.get_mut().unwrap(); let caps = caps.get_mut().unwrap();
while let Some(structure) = seq.next_element::<Structure>()? { while let Some(caps_item) = seq.next_element::<CapsItemDe>()? {
caps.append_structure(structure); caps.append_structure(caps_item.into());
} }
} }
Ok(caps) Ok(caps)
@ -92,7 +146,7 @@ mod tests {
Ok( Ok(
concat!( concat!(
"[", "[",
" (\"foo/bar\", [", " ((\"foo/bar\", [",
" (\"int\", \"i32\", 12),", " (\"int\", \"i32\", 12),",
" (\"bool\", \"bool\", true),", " (\"bool\", \"bool\", true),",
" (\"string\", \"String\", \"bla\"),", " (\"string\", \"String\", \"bla\"),",
@ -101,7 +155,7 @@ mod tests {
" (\"i32\", 1),", " (\"i32\", 1),",
" (\"i32\", 2),", " (\"i32\", 2),",
" ]),", " ]),",
" ]),", " ]), None),",
"]" "]"
) )
.to_owned() .to_owned()
@ -118,16 +172,19 @@ mod tests {
let caps_ron = r#" let caps_ron = r#"
[ [
("foo/bar", [ (
("int", "i32", 12), ("foo/bar", [
("bool", "bool", true), ("int", "i32", 12),
("string", "String", "bla"), ("bool", "bool", true),
("fraction", "Fraction", (1, 2)), ("string", "String", "bla"),
("array", "Array", [ ("fraction", "Fraction", (1, 2)),
("i32", 1), ("array", "Array", [
("i32", 2), ("i32", 1),
("i32", 2),
]),
]), ]),
]), None,
),
]"#; ]"#;
let caps: Caps = ron::de::from_str(caps_ron).unwrap(); let caps: Caps = ron::de::from_str(caps_ron).unwrap();
let s = caps.get_structure(0).unwrap(); let s = caps.get_structure(0).unwrap();

View file

@ -152,10 +152,10 @@ mod tests {
" )),", " )),",
" buffer_list: None,", " buffer_list: None,",
" caps: Some([", " caps: Some([",
" (\"sample/caps\", [", " ((\"sample/caps\", [",
" (\"int\", \"i32\", 12),", " (\"int\", \"i32\", 12),",
" (\"bool\", \"bool\", true),", " (\"bool\", \"bool\", true),",
" ]),", " ]), None),",
" ]),", " ]),",
" segment: Some((", " segment: Some((",
" flags: (", " flags: (",
@ -257,10 +257,10 @@ mod tests {
)), )),
buffer_list: None, buffer_list: None,
caps: Some([ caps: Some([
("sample/caps", [ (("sample/caps", [
("int", "i32", 12), ("int", "i32", 12),
("bool", "bool", true), ("bool", "bool", true),
]), ]), None),
]), ]),
segment: Some(( segment: Some((
flags: ( flags: (