mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2024-11-26 11:31:06 +00:00
format: serde: avoid redundant ()
for some types
This commit is contained in:
parent
dfc961679f
commit
9f2f684188
4 changed files with 136 additions and 101 deletions
|
@ -24,13 +24,10 @@ 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<u64>);
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)]
|
||||
#[cfg_attr(feature = "ser_de", derive(Serialize, Deserialize))]
|
||||
pub struct Bytes(pub Option<u64>);
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)]
|
||||
#[cfg_attr(feature = "ser_de", derive(Serialize, Deserialize))]
|
||||
pub struct Buffers(pub Option<u64>);
|
||||
pub type Time = ClockTime;
|
||||
|
||||
|
@ -486,99 +483,3 @@ 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 super::Default;
|
||||
use ClockTime;
|
||||
use Format;
|
||||
use GenericFormattedValue;
|
||||
|
||||
::init().unwrap();
|
||||
|
||||
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 Format;
|
||||
use GenericFormattedValue;
|
||||
|
||||
::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));
|
||||
}
|
||||
}
|
||||
|
|
132
gstreamer/src/format_serde.rs
Normal file
132
gstreamer/src/format_serde.rs
Normal file
|
@ -0,0 +1,132 @@
|
|||
// 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};
|
||||
|
||||
use format::{Buffers, Bytes, Default};
|
||||
|
||||
// Manual implementation for some types that would otherwise yield representations such as:
|
||||
// "Default((Some(42)))"
|
||||
macro_rules! impl_ser_de(
|
||||
($t:ident) => {
|
||||
impl Serialize for $t {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<'de> Deserialize<'de> for $t {
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
Option::<u64>::deserialize(deserializer).map(|value| $t(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
impl_ser_de!(Buffers);
|
||||
impl_ser_de!(Bytes);
|
||||
impl_ser_de!(Default);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate ron;
|
||||
extern crate serde_json;
|
||||
|
||||
use format::Default;
|
||||
use Format;
|
||||
use GenericFormattedValue;
|
||||
|
||||
#[test]
|
||||
fn test_serialize() {
|
||||
use format::Buffers;
|
||||
use format::Bytes;
|
||||
use ClockTime;
|
||||
|
||||
::init().unwrap();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize() {
|
||||
::init().unwrap();
|
||||
|
||||
let format_ron = "Default(Some(42))";
|
||||
let format: GenericFormattedValue = ron::de::from_str(format_ron).unwrap();
|
||||
assert_eq!(format, GenericFormattedValue::Default(Default(Some(42))));
|
||||
|
||||
let format_json = "{\"Default\":42}";
|
||||
let format: GenericFormattedValue = serde_json::from_str(format_json).unwrap();
|
||||
assert_eq!(format, GenericFormattedValue::Default(Default(Some(42))));
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
|
@ -214,6 +214,8 @@ pub use typefind::*;
|
|||
|
||||
pub mod format;
|
||||
pub use format::{FormattedValue, GenericFormattedValue, SpecificFormattedValue};
|
||||
#[cfg(feature = "ser_de")]
|
||||
pub(crate) mod format_serde;
|
||||
|
||||
mod segment;
|
||||
pub use segment::*;
|
||||
|
|
|
@ -55,7 +55,7 @@ impl<'a> Serialize for Fraction {
|
|||
impl<'de> Deserialize<'de> for Fraction {
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
Rational32::deserialize(deserializer)
|
||||
.and_then(|rational| Ok(Fraction::new(*rational.numer(), *rational.denom())))
|
||||
.map(|rational| Fraction::new(*rational.numer(), *rational.denom()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,7 +165,7 @@ macro_rules! de_value(
|
|||
{
|
||||
let value = $seq
|
||||
.next_element::<$t>()?
|
||||
.and_then(|base_value| Some(base_value.to_value()));
|
||||
.map(|base_value| base_value.to_value());
|
||||
Ok(value)
|
||||
}
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue