From a2c4e78f70df19e1cbfce24ea17cbf0743c16f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Laignel?= Date: Thu, 5 Jul 2018 20:42:57 +0200 Subject: [PATCH] GenericFormattedValue: serde impl --- Gir_Gst.toml | 8 ++- gstreamer/src/auto/enums.rs | 1 + gstreamer/src/format.rs | 101 ++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/Gir_Gst.toml b/Gir_Gst.toml index 39b830f28..0ef5cf23d 100644 --- a/Gir_Gst.toml +++ b/Gir_Gst.toml @@ -26,7 +26,6 @@ generate = [ "Gst.URIHandler", "Gst.URIType", "Gst.ElementFactoryListType", - "Gst.Format", "Gst.BufferingMode", "Gst.CoreError", "Gst.ResourceError", @@ -155,6 +154,13 @@ status = "generate" name = "Serialize, Deserialize" cfg_condition = "feature = \"ser_de\"" +[[object]] +name = "Gst.Format" +status = "generate" + [[object.derive]] + name = "Serialize, Deserialize" + cfg_condition = "feature = \"ser_de\"" + [[object]] name = "Gst.Structure" status = "manual" diff --git a/gstreamer/src/auto/enums.rs b/gstreamer/src/auto/enums.rs index bd507f6ce..bd2bf8ac0 100644 --- a/gstreamer/src/auto/enums.rs +++ b/gstreamer/src/auto/enums.rs @@ -773,6 +773,7 @@ impl SetValue for FlowReturn { } #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "ser_de", derive(Serialize, Deserialize))] pub enum Format { Undefined, Default, diff --git a/gstreamer/src/format.rs b/gstreamer/src/format.rs index 58a551710..641b187a3 100644 --- a/gstreamer/src/format.rs +++ b/gstreamer/src/format.rs @@ -12,6 +12,7 @@ use ClockTime; use Format; #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] +#[cfg_attr(feature = "ser_de", derive(Serialize, Deserialize))] pub enum GenericFormattedValue { Undefined(i64), Default(Default), @@ -23,10 +24,13 @@ pub enum GenericFormattedValue { } #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)] +#[cfg_attr(feature = "ser_de", derive(Serialize, Deserialize))] pub struct Default(pub Option); #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)] +#[cfg_attr(feature = "ser_de", derive(Serialize, Deserialize))] pub struct Bytes(pub Option); #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)] +#[cfg_attr(feature = "ser_de", derive(Serialize, Deserialize))] pub struct Buffers(pub Option); pub type Time = ClockTime; @@ -482,3 +486,100 @@ impl_format_value_traits!(Default, Default, Default); impl_format_value_traits!(Bytes, Bytes, Bytes); impl_format_value_traits!(ClockTime, Time, Time); impl_format_value_traits!(Buffers, Buffers, Buffers); + +#[cfg(test)] +mod tests { + #[cfg(feature = "ser_de")] + #[test] + fn test_serialize() { + extern crate ron; + extern crate serde_json; + + use super::Buffers; + use super::Bytes; + use ClockTime; + use super::Default; + use Format; + use GenericFormattedValue; + + ::init().unwrap(); + + // don't use newlines + let mut pretty_config = ron::ser::PrettyConfig::default(); + pretty_config.new_line = "".to_string(); + + let value = GenericFormattedValue::Undefined(42); + let res = ron::ser::to_string_pretty(&value, pretty_config.clone()); + assert_eq!(Ok("Undefined(42)".to_owned()), res); + let res = serde_json::to_string(&value).unwrap(); + assert_eq!("{\"Undefined\":42}".to_owned(), res); + + let value = GenericFormattedValue::Default(Default(Some(42))); + let res = ron::ser::to_string_pretty(&value, pretty_config.clone()); + assert_eq!(Ok("Default((Some(42)))".to_owned()), res); + let res = serde_json::to_string(&value).unwrap(); + assert_eq!("{\"Default\":42}".to_owned(), res); + + let value = GenericFormattedValue::Default(Default(None)); + let res = ron::ser::to_string_pretty(&value, pretty_config.clone()); + assert_eq!(Ok("Default((None))".to_owned()), res); + let res = serde_json::to_string(&value).unwrap(); + assert_eq!("{\"Default\":null}".to_owned(), res); + + let value = GenericFormattedValue::Bytes(Bytes(Some(42))); + let res = ron::ser::to_string_pretty(&value, pretty_config.clone()); + assert_eq!(Ok("Bytes((Some(42)))".to_owned()), res); + let res = serde_json::to_string(&value).unwrap(); + assert_eq!("{\"Bytes\":42}".to_owned(), res); + + let value = GenericFormattedValue::Time(ClockTime::from_nseconds(42_123_456_789)); + let res = ron::ser::to_string_pretty(&value, pretty_config.clone()); + assert_eq!(Ok("Time(Some(42123456789))".to_owned()), res); + let res = serde_json::to_string(&value).unwrap(); + assert_eq!("{\"Time\":42123456789}".to_owned(), res); + + let value = GenericFormattedValue::Buffers(Buffers(Some(42))); + let res = ron::ser::to_string_pretty(&value, pretty_config.clone()); + assert_eq!(Ok("Buffers((Some(42)))".to_owned()), res); + let res = serde_json::to_string(&value).unwrap(); + assert_eq!("{\"Buffers\":42}".to_owned(), res); + + let value = GenericFormattedValue::Percent(Some(42)); + let res = ron::ser::to_string_pretty(&value, pretty_config.clone()); + assert_eq!(Ok("Percent(Some(42))".to_owned()), res); + let res = serde_json::to_string(&value).unwrap(); + assert_eq!("{\"Percent\":42}".to_owned(), res); + + let value = GenericFormattedValue::Other(Format::Percent, 42); + let res = ron::ser::to_string_pretty(&value, pretty_config.clone()); + assert_eq!(Ok("Other(Percent, 42)".to_owned()), res); + let res = serde_json::to_string(&value).unwrap(); + assert_eq!("{\"Other\":[\"Percent\",42]}".to_owned(), res); + + let value = GenericFormattedValue::Other(Format::__Unknown(7), 42); + let res = ron::ser::to_string_pretty(&value, pretty_config.clone()); + assert_eq!(Ok("Other(__Unknown(7), 42)".to_owned()), res); + let res = serde_json::to_string(&value).unwrap(); + assert_eq!("{\"Other\":[{\"__Unknown\":7},42]}".to_owned(), res); + } + + #[cfg(feature = "ser_de")] + #[test] + fn test_deserialize() { + extern crate ron; + extern crate serde_json; + + use GenericFormattedValue; + use Format; + + ::init().unwrap(); + + let format_ron = "Other(Percent, 42)"; + let format: GenericFormattedValue = ron::de::from_str(format_ron).unwrap(); + assert_eq!(format, GenericFormattedValue::Other(Format::Percent, 42)); + + let format_json = "{\"Other\":[\"Percent\",42]}"; + let format: GenericFormattedValue = serde_json::from_str(format_json).unwrap(); + assert_eq!(format, GenericFormattedValue::Other(Format::Percent, 42)); + } +}