serde: fmt pass

Note: clippy didn't raise any warning
This commit is contained in:
François Laignel 2018-07-22 16:55:24 +02:00 committed by Sebastian Dröge
parent d4fb10ef4c
commit ab9cd29bd6
12 changed files with 503 additions and 524 deletions

View file

@ -8,8 +8,8 @@
use serde::de::{Deserialize, Deserializer};
use serde::ser;
use serde::ser::{Serialize, Serializer, SerializeStruct};
use serde_bytes::{Bytes, ByteBuf};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use serde_bytes::{ByteBuf, Bytes};
use Buffer;
use BufferFlags;
@ -70,8 +70,7 @@ impl From<BufferDe> for Buffer {
impl<'de> Deserialize<'de> for Buffer {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
BufferDe::deserialize(deserializer)
.map(|buffer_de| buffer_de.into())
BufferDe::deserialize(deserializer).map(|buffer_de| buffer_de.into())
}
}
@ -103,22 +102,19 @@ mod tests {
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()
),
Ok(concat!(
"(",
" pts: Some(1),",
" dts: None,",
" duration: Some(5),",
" offset: 3,",
" offset_end: 4,",
" flags: (",
" bits: 1048592,",
" ),",
" buffer: \"AQIDBA==\",",
")"
).to_owned()),
res
);
@ -126,29 +122,27 @@ mod tests {
assert_eq!(
concat!(
"{",
"\"pts\":1,",
"\"dts\":null,",
"\"duration\":5,",
"\"offset\":3,",
"\"offset_end\":4,",
"\"flags\":{\"bits\":1048592},",
"\"buffer\":[1,2,3,4]",
"\"pts\":1,",
"\"dts\":null,",
"\"duration\":5,",
"\"offset\":3,",
"\"offset_end\":4,",
"\"flags\":{\"bits\":1048592},",
"\"buffer\":[1,2,3,4]",
"}"
)
.to_owned(),
).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
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
);
@ -211,9 +205,8 @@ mod tests {
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
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());
@ -227,4 +220,4 @@ mod tests {
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
}
}
}
}

View file

@ -7,7 +7,7 @@
// except according to those terms.
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use serde::ser::{Serialize, Serializer, SerializeSeq};
use serde::ser::{Serialize, SerializeSeq, Serializer};
use std::fmt;
@ -106,35 +106,32 @@ mod tests {
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()
),
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,
);
}

View file

@ -8,7 +8,7 @@
use serde::de;
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use serde::ser::{Serialize, Serializer, SerializeSeq, SerializeTuple};
use serde::ser::{Serialize, SerializeSeq, SerializeTuple, Serializer};
use std::fmt;
@ -70,15 +70,19 @@ impl<'de> Visitor<'de> for CapsItemVisitor {
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let structure = seq.next_element::<Structure>()?
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"))?;
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)"
"Found a value for `CapsFeature`, expected `None` (not implemented yet)",
))
} else {
Ok(CapsItemDe(structure))
@ -143,23 +147,20 @@ mod tests {
let res = ron::ser::to_string_pretty(&caps, pretty_config);
assert_eq!(
Ok(
concat!(
"[",
" ((\"foo/bar\", [",
" (\"int\", \"i32\", 12),",
" (\"bool\", \"bool\", true),",
" (\"string\", \"String\", \"bla\"),",
" (\"fraction\", \"Fraction\", (1, 2)),",
" (\"array\", \"Array\", [",
" (\"i32\", 1),",
" (\"i32\", 2),",
" ]),",
" ]), None),",
"]"
)
.to_owned()
),
Ok(concat!(
"[",
" ((\"foo/bar\", [",
" (\"int\", \"i32\", 12),",
" (\"bool\", \"bool\", true),",
" (\"string\", \"String\", \"bla\"),",
" (\"fraction\", \"Fraction\", (1, 2)),",
" (\"array\", \"Array\", [",
" (\"i32\", 1),",
" (\"i32\", 2),",
" ]),",
" ]), None),",
"]"
).to_owned()),
res,
);
}

View file

@ -35,8 +35,7 @@ impl<'de> Visitor<'de> for ClockTimeVisitor {
where
D: Deserializer<'de>,
{
u64::deserialize(deserializer)
.and_then(|value| Ok(ClockTime::from_nseconds(value)))
u64::deserialize(deserializer).and_then(|value| Ok(ClockTime::from_nseconds(value)))
}
fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {

View file

@ -7,7 +7,7 @@
// except according to those terms.
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer, SerializeStruct};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use DateTime;
impl<'a> Serialize for DateTime {
@ -53,8 +53,7 @@ impl From<DateTimeDe> for DateTime {
impl<'de> Deserialize<'de> for DateTime {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
DateTimeDe::deserialize(deserializer)
.and_then(|datetime_de| Ok(datetime_de.into()))
DateTimeDe::deserialize(deserializer).and_then(|datetime_de| Ok(datetime_de.into()))
}
}
@ -76,21 +75,18 @@ mod tests {
let res = ron::ser::to_string_pretty(&datetime, pretty_config);
assert_eq!(
Ok(
concat!(
"(",
" tz_offset: 2,",
" y: 2018,",
" m: 5,",
" d: 28,",
" h: 16,",
" mn: 6,",
" s: 42,",
" us: 841000,",
")"
)
.to_owned()
),
Ok(concat!(
"(",
" tz_offset: 2,",
" y: 2018,",
" m: 5,",
" d: 28,",
" h: 16,",
" mn: 6,",
" s: 42,",
" us: 841000,",
")"
).to_owned()),
res,
);

View file

@ -497,8 +497,8 @@ mod tests {
use super::Buffers;
use super::Bytes;
use ClockTime;
use super::Default;
use ClockTime;
use Format;
use GenericFormattedValue;
@ -568,8 +568,8 @@ mod tests {
extern crate ron;
extern crate serde_json;
use GenericFormattedValue;
use Format;
use GenericFormattedValue;
::init().unwrap();

View file

@ -7,7 +7,7 @@
// except according to those terms.
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer, SerializeStruct};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use Buffer;
use BufferList;
@ -67,8 +67,7 @@ impl From<SampleDe> for Sample {
impl<'de> Deserialize<'de> for Sample {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
SampleDe::deserialize(deserializer)
.and_then(|sample_de| Ok(sample_de.into()))
SampleDe::deserialize(deserializer).and_then(|sample_de| Ok(sample_de.into()))
}
}
@ -136,49 +135,46 @@ mod tests {
let res = ron::ser::to_string_pretty(&sample, pretty_config.clone());
assert_eq!(
Ok(
concat!(
"(",
" buffer: Some((",
" pts: Some(1),",
" dts: None,",
" duration: Some(4),",
" offset: 0,",
" offset_end: 4,",
" flags: (",
" bits: 0,",
" ),",
" buffer: \"AQIDBA==\",",
" )),",
" buffer_list: None,",
" caps: Some([",
" ((\"sample/caps\", [",
" (\"int\", \"i32\", 12),",
" (\"bool\", \"bool\", true),",
" ]), None),",
" ]),",
" segment: Some((",
" flags: (",
" bits: 9,",
" ),",
" rate: 1,",
" applied_rate: 0.9,",
" format: Time,",
" base: 123,",
" offset: 42,",
" start: 1024,",
" stop: 2048,",
" time: 1042,",
" position: 256,",
" duration: -1,",
" )),",
" info: Some((\"sample.info\", [",
" (\"f3\", \"i32\", 123),",
" ])),",
")"
)
.to_owned()
),
Ok(concat!(
"(",
" buffer: Some((",
" pts: Some(1),",
" dts: None,",
" duration: Some(4),",
" offset: 0,",
" offset_end: 4,",
" flags: (",
" bits: 0,",
" ),",
" buffer: \"AQIDBA==\",",
" )),",
" buffer_list: None,",
" caps: Some([",
" ((\"sample/caps\", [",
" (\"int\", \"i32\", 12),",
" (\"bool\", \"bool\", true),",
" ]), None),",
" ]),",
" segment: Some((",
" flags: (",
" bits: 9,",
" ),",
" rate: 1,",
" applied_rate: 0.9,",
" format: Time,",
" base: 123,",
" offset: 42,",
" start: 1024,",
" stop: 2048,",
" time: 1042,",
" position: 256,",
" duration: -1,",
" )),",
" info: Some((\"sample.info\", [",
" (\"f3\", \"i32\", 123),",
" ])),",
")"
).to_owned()),
res
);
@ -198,42 +194,39 @@ mod tests {
let res = ron::ser::to_string_pretty(&sample, pretty_config);
assert_eq!(
Ok(
concat!(
"(",
" buffer: Some((",
" pts: Some(1),",
" dts: None,",
" duration: Some(4),",
" offset: 0,",
" offset_end: 4,",
" flags: (",
" bits: 0,",
" ),",
" buffer: \"AQIDBA==\",",
" )),",
" buffer_list: None,",
" caps: None,",
" segment: Some((",
" flags: (",
" bits: 0,",
" ),",
" 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()
),
Ok(concat!(
"(",
" buffer: Some((",
" pts: Some(1),",
" dts: None,",
" duration: Some(4),",
" offset: 0,",
" offset_end: 4,",
" flags: (",
" bits: 0,",
" ),",
" buffer: \"AQIDBA==\",",
" )),",
" buffer_list: None,",
" caps: None,",
" segment: Some((",
" flags: (",
" bits: 0,",
" ),",
" 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()),
res
);
}

View file

@ -7,7 +7,7 @@
// except according to those terms.
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer, SerializeStruct};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use Format;
use GenericFormattedValue;
@ -54,13 +54,34 @@ impl From<SegmentDe> for Segment {
segment.set_rate(segment_de.rate);
segment.set_applied_rate(segment_de.applied_rate);
segment.set_format(segment_de.format);
segment.set_base(GenericFormattedValue::new(segment_de.format, segment_de.base));
segment.set_offset(GenericFormattedValue::new(segment_de.format, segment_de.offset));
segment.set_start(GenericFormattedValue::new(segment_de.format, segment_de.start));
segment.set_stop(GenericFormattedValue::new(segment_de.format, segment_de.stop));
segment.set_time(GenericFormattedValue::new(segment_de.format, segment_de.time));
segment.set_position(GenericFormattedValue::new(segment_de.format, segment_de.position));
segment.set_duration(GenericFormattedValue::new(segment_de.format, segment_de.duration));
segment.set_base(GenericFormattedValue::new(
segment_de.format,
segment_de.base,
));
segment.set_offset(GenericFormattedValue::new(
segment_de.format,
segment_de.offset,
));
segment.set_start(GenericFormattedValue::new(
segment_de.format,
segment_de.start,
));
segment.set_stop(GenericFormattedValue::new(
segment_de.format,
segment_de.stop,
));
segment.set_time(GenericFormattedValue::new(
segment_de.format,
segment_de.time,
));
segment.set_position(GenericFormattedValue::new(
segment_de.format,
segment_de.position,
));
segment.set_duration(GenericFormattedValue::new(
segment_de.format,
segment_de.duration,
));
segment
}
@ -68,8 +89,7 @@ impl From<SegmentDe> for Segment {
impl<'de> Deserialize<'de> for Segment {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
SegmentDe::deserialize(deserializer)
.and_then(|segment_de| Ok(segment_de.into()))
SegmentDe::deserialize(deserializer).and_then(|segment_de| Ok(segment_de.into()))
}
}
@ -105,26 +125,23 @@ mod tests {
let res = ron::ser::to_string_pretty(&segment, pretty_config);
assert_eq!(
Ok(
concat!(
"(",
" flags: (",
" bits: 9,",
" ),",
" rate: 1,",
" applied_rate: 0.9,",
" format: Time,",
" base: 123,",
" offset: 42,",
" start: 1024,",
" stop: 2048,",
" time: 1042,",
" position: 256,",
" duration: -1,",
")"
)
.to_owned()
),
Ok(concat!(
"(",
" flags: (",
" bits: 9,",
" ),",
" rate: 1,",
" applied_rate: 0.9,",
" format: Time,",
" base: 123,",
" offset: 42,",
" start: 1024,",
" stop: 2048,",
" time: 1042,",
" position: 256,",
" duration: -1,",
")"
).to_owned()),
res,
);
}
@ -152,16 +169,40 @@ mod tests {
"#;
let segment: Segment = ron::de::from_str(segment_ron).unwrap();
assert_eq!(segment.get_flags(), SegmentFlags::RESET | SegmentFlags::SEGMENT);
assert_eq!(
segment.get_flags(),
SegmentFlags::RESET | SegmentFlags::SEGMENT
);
assert_eq!(segment.get_rate(), 1f64);
assert_eq!(segment.get_applied_rate(), 0.9f64);
assert_eq!(segment.get_format(), Format::Time);
assert_eq!(segment.get_base(), GenericFormattedValue::Time(ClockTime::from_nseconds(123)));
assert_eq!(segment.get_offset(), GenericFormattedValue::Time(ClockTime::from_nseconds(42)));
assert_eq!(segment.get_start(), GenericFormattedValue::Time(ClockTime::from_nseconds(1024)));
assert_eq!(segment.get_stop(), GenericFormattedValue::Time(ClockTime::from_nseconds(2048)));
assert_eq!(segment.get_time(), GenericFormattedValue::Time(ClockTime::from_nseconds(1042)));
assert_eq!(segment.get_position(), GenericFormattedValue::Time(ClockTime::from_nseconds(256)));
assert_eq!(segment.get_duration(), GenericFormattedValue::Time(ClockTime::none()));
assert_eq!(
segment.get_base(),
GenericFormattedValue::Time(ClockTime::from_nseconds(123))
);
assert_eq!(
segment.get_offset(),
GenericFormattedValue::Time(ClockTime::from_nseconds(42))
);
assert_eq!(
segment.get_start(),
GenericFormattedValue::Time(ClockTime::from_nseconds(1024))
);
assert_eq!(
segment.get_stop(),
GenericFormattedValue::Time(ClockTime::from_nseconds(2048))
);
assert_eq!(
segment.get_time(),
GenericFormattedValue::Time(ClockTime::from_nseconds(1042))
);
assert_eq!(
segment.get_position(),
GenericFormattedValue::Time(ClockTime::from_nseconds(256))
);
assert_eq!(
segment.get_duration(),
GenericFormattedValue::Time(ClockTime::none())
);
}
}

View file

@ -12,7 +12,7 @@ use glib::ToValue;
use serde::de;
use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
use serde::ser;
use serde::ser::{Serialize, Serializer, SerializeSeq, SerializeTuple};
use serde::ser::{Serialize, SerializeSeq, SerializeTuple, Serializer};
use std::fmt;
@ -84,14 +84,16 @@ impl<'de> Visitor<'de> for FieldVisitor {
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(
"a tuple of 3 elements (name: `String`, type name: `String`, value: `Value`)"
"a tuple of 3 elements (name: `String`, type name: `String`, value: `Value`)",
)
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let name = seq.next_element::<String>()?
let name = seq
.next_element::<String>()?
.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"))?;
let send_value = de_send_value!(type_name, seq)?
.ok_or(de::Error::custom("Expected a value for `Value`"))?;
@ -142,7 +144,8 @@ impl<'de> Visitor<'de> for StructureVisitor {
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let name = seq.next_element::<String>()?
let name = seq
.next_element::<String>()?
.ok_or(de::Error::custom("Expected a name for the `Structure`"))?;
let mut structure = Structure::new_empty(&name);
seq.next_element_seed(FieldsDe(structure.as_mut()))?
@ -184,21 +187,18 @@ mod tests {
let res = ron::ser::to_string_pretty(&s, pretty_config);
assert_eq!(
Ok(
concat!(
"(\"test\", [",
" (\"f1\", \"String\", \"abc\"),",
" (\"f2\", \"String\", \"bcd\"),",
" (\"f3\", \"i32\", 123),",
" (\"fraction\", \"Fraction\", (1, 2)),",
" (\"array\", \"Array\", [",
" (\"i32\", 1),",
" (\"i32\", 2),",
" ]),",
"])"
)
.to_owned()
),
Ok(concat!(
"(\"test\", [",
" (\"f1\", \"String\", \"abc\"),",
" (\"f2\", \"String\", \"bcd\"),",
" (\"f3\", \"i32\", 123),",
" (\"fraction\", \"Fraction\", (1, 2)),",
" (\"array\", \"Array\", [",
" (\"i32\", 1),",
" (\"i32\", 2),",
" ]),",
"])"
).to_owned()),
res,
);
}

View file

@ -8,23 +8,23 @@
use ffi;
use glib;
use glib::translate::{ToGlibPtr, from_glib};
use glib::translate::{from_glib, ToGlibPtr};
use glib::{SendValue, ToValue};
use serde::de;
use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
use serde::ser;
use serde::ser::{Serialize, Serializer, SerializeSeq, SerializeTuple};
use serde::ser::{Serialize, SerializeSeq, SerializeTuple, Serializer};
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use tags::*;
use value_serde::{DATE_TIME_OTHER_TYPE_ID, SAMPLE_OTHER_TYPE_ID};
use DateTime;
use Sample;
use TagMergeMode;
use tags::*;
use value_serde::{DATE_TIME_OTHER_TYPE_ID, SAMPLE_OTHER_TYPE_ID};
macro_rules! ser_tag (
($value:ident, $seq:ident, $t:ty) => (
@ -58,22 +58,16 @@ impl<'a> Serialize for TagValuesSer<'a> {
} 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),
)
)
)
Err(ser::Error::custom(format!(
"unimplemented `Tag` serialization for type {}",
glib::Type::Other(type_id),
)))
}
}
type_ => {
Err(
ser::Error::custom(
format!("unimplemented `Tag` serialization for type {}", type_)
)
)
}
type_ => Err(ser::Error::custom(format!(
"unimplemented `Tag` serialization for type {}",
type_
))),
}?;
}
seq.end()
@ -155,38 +149,28 @@ impl<'de, 'a> Visitor<'de> for TagValuesVisitor<'a> {
} else if *SAMPLE_OTHER_TYPE_ID == type_id {
de_tag_value!(self.0, seq, Sample)
} else {
return Err(
de::Error::custom(
format!(
"unimplemented deserialization for `Tag` {} with type `{}`",
self.0,
glib::Type::Other(type_id),
),
)
);
return Err(de::Error::custom(format!(
"unimplemented deserialization for `Tag` {} with type `{}`",
self.0,
glib::Type::Other(type_id),
)));
}
}
type_ => {
return Err(
de::Error::custom(
format!(
"unimplemented deserialization for `Tag` {} with type `{}`",
self.0,
type_,
),
)
);
return Err(de::Error::custom(format!(
"unimplemented deserialization for `Tag` {} with type `{}`",
self.0, type_,
)));
}
}?;
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,
)))?
}
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,
}
}
@ -210,17 +194,14 @@ 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)"
)
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)
))?
.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`"))
@ -271,8 +252,8 @@ mod tests {
fn test_serialize() {
use Buffer;
use GenericFormattedValue;
use TagMergeMode;
use Sample;
use TagMergeMode;
::init().unwrap();
@ -297,12 +278,7 @@ mod tests {
buffer.set_offset(0);
buffer.set_offset_end(0);
}
Sample::new::<GenericFormattedValue>(
Some(&buffer),
None,
None,
None,
)
Sample::new::<GenericFormattedValue>(Some(&buffer), None, None, None)
};
tags.add::<Image>(&sample, TagMergeMode::Append); // Sample
}
@ -312,70 +288,68 @@ mod tests {
let res = ron::ser::to_string_pretty(&tags, pretty_config);
assert_eq!(
Ok(
concat!(
"[",
" (\"title\", [",
" \"a title\",",
" \"another title\",",
" ]),",
" (\"duration\", [",
" 120000000000,",
" ]),",
" (\"bitrate\", [",
" 96000,",
" ]),",
" (\"replaygain-track-gain\", [",
" 1,",
" ]),",
" (\"datetime\", [",
" (",
" tz_offset: 2,",
" y: 2018,",
" m: 5,",
" d: 28,",
" h: 16,",
" mn: 6,",
" s: 42,",
" us: 841000,",
" ),",
" ]),",
" (\"image\", [",
" (",
" buffer: Some((",
" pts: None,",
" dts: None,",
" duration: None,",
" offset: 0,",
" offset_end: 0,",
" flags: (",
" bits: 0,",
" ),",
" buffer: \"AQIDBA==\",",
" )),",
" buffer_list: None,",
" caps: None,",
" segment: Some((",
" flags: (",
" bits: 0,",
" ),",
" 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()
),
Ok(concat!(
"[",
" (\"title\", [",
" \"a title\",",
" \"another title\",",
" ]),",
" (\"duration\", [",
" 120000000000,",
" ]),",
" (\"bitrate\", [",
" 96000,",
" ]),",
" (\"replaygain-track-gain\", [",
" 1,",
" ]),",
" (\"datetime\", [",
" (",
" tz_offset: 2,",
" y: 2018,",
" m: 5,",
" d: 28,",
" h: 16,",
" mn: 6,",
" s: 42,",
" us: 841000,",
" ),",
" ]),",
" (\"image\", [",
" (",
" buffer: Some((",
" pts: None,",
" dts: None,",
" duration: None,",
" offset: 0,",
" offset_end: 0,",
" flags: (",
" bits: 0,",
" ),",
" buffer: \"AQIDBA==\",",
" )),",
" buffer_list: None,",
" caps: None,",
" segment: Some((",
" flags: (",
" bits: 0,",
" ),",
" 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()),
res,
);
}
@ -430,8 +404,14 @@ mod tests {
"#;
let tags: TagList = ron::de::from_str(tag_list_ron).unwrap();
assert_eq!(tags.get_index::<Title>(0).unwrap().get(), Some("a title"));
assert_eq!(tags.get_index::<Title>(1).unwrap().get(), Some("another title"));
assert_eq!(tags.get_index::<Duration>(0).unwrap().get(), Some(::SECOND * 120));
assert_eq!(
tags.get_index::<Title>(1).unwrap().get(),
Some("another title")
);
assert_eq!(
tags.get_index::<Duration>(0).unwrap().get(),
Some(::SECOND * 120)
);
assert_eq!(tags.get_index::<Bitrate>(0).unwrap().get(), Some(96_000));
assert_eq!(tags.get_index::<TrackGain>(0).unwrap().get(), Some(1f64));
let datetime = tags.get_index::<DateTime>(0).unwrap().get().unwrap();
@ -456,7 +436,10 @@ mod tests {
"#;
let tags: TagList = serde_json::from_str(tag_json).unwrap();
assert_eq!(tags.get_index::<Title>(0).unwrap().get(), Some("a title"));
assert_eq!(tags.get_index::<Title>(1).unwrap().get(), Some("another title"));
assert_eq!(
tags.get_index::<Title>(1).unwrap().get(),
Some("another title")
);
assert_eq!(tags.get_index::<Bitrate>(0).unwrap().get(), Some(96_000));
assert_eq!(tags.get_index::<TrackGain>(0).unwrap().get(), Some(1f64));
let datetime = tags.get_index::<DateTime>(0).unwrap().get().unwrap();

View file

@ -7,13 +7,13 @@
// except according to those terms.
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer, SerializeStruct};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use toc::*;
use TagList;
use TocEntryType;
use TocScope;
use TocLoopType;
use TocScope;
impl Serialize for TocRef {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
@ -76,8 +76,7 @@ impl From<TocDe> for Toc {
impl<'de> Deserialize<'de> for Toc {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
TocDe::deserialize(deserializer)
.map(|toc_de| toc_de.into())
TocDe::deserialize(deserializer).map(|toc_de| toc_de.into())
}
}
@ -118,8 +117,7 @@ impl From<TocEntryDe> for TocEntry {
impl<'de> Deserialize<'de> for TocEntry {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
TocEntryDe::deserialize(deserializer)
.map(|toc_entry_de| toc_entry_de.into())
TocEntryDe::deserialize(deserializer).map(|toc_entry_de| toc_entry_de.into())
}
}
@ -127,15 +125,15 @@ impl<'de> Deserialize<'de> for TocEntry {
mod tests {
extern crate ron;
use toc::*;
use TocEntryType;
use TocScope;
use toc::*;
#[test]
fn test_serialize() {
use TagMergeMode;
use TagList;
use tags::Title;
use TagList;
use TagMergeMode;
::init().unwrap();
@ -143,7 +141,9 @@ mod tests {
{
let toc = toc.get_mut().unwrap();
let mut tags = TagList::new();
tags.get_mut().unwrap().add::<Title>(&"toc", TagMergeMode::Append);
tags.get_mut()
.unwrap()
.add::<Title>(&"toc", TagMergeMode::Append);
toc.set_tags(tags);
let mut toc_edition = TocEntry::new(TocEntryType::Edition, "edition");
@ -160,7 +160,9 @@ mod tests {
let toc_chap_1_1 = toc_chap_1_1.get_mut().unwrap();
toc_chap_1_1.set_start_stop_times(0, 4);
let mut tags = TagList::new();
tags.get_mut().unwrap().add::<Title>(&"chapter 1.1", TagMergeMode::Append);
tags.get_mut()
.unwrap()
.add::<Title>(&"chapter 1.1", TagMergeMode::Append);
toc_chap_1_1.set_tags(tags);
}
toc_chap_1.append_sub_entry(toc_chap_1_1);
@ -170,7 +172,9 @@ mod tests {
let toc_chap_1_2 = toc_chap_1_2.get_mut().unwrap();
toc_chap_1_2.set_start_stop_times(4, 10);
let mut tags = TagList::new();
tags.get_mut().unwrap().add::<Title>(&"chapter 1.2", TagMergeMode::Append);
tags.get_mut()
.unwrap()
.add::<Title>(&"chapter 1.2", TagMergeMode::Append);
toc_chap_1_2.set_tags(tags);
}
toc_chap_1.append_sub_entry(toc_chap_1_2);
@ -182,7 +186,9 @@ mod tests {
let toc_chap_2 = toc_chap_2.get_mut().unwrap();
toc_chap_2.set_start_stop_times(10, 15);
let mut tags = TagList::new();
tags.get_mut().unwrap().add::<Title>(&"chapter 2", TagMergeMode::Append);
tags.get_mut()
.unwrap()
.add::<Title>(&"chapter 2", TagMergeMode::Append);
toc_chap_2.set_tags(tags);
}
toc_edition.append_sub_entry(toc_chap_2);
@ -195,77 +201,75 @@ mod tests {
let res = ron::ser::to_string_pretty(&toc, pretty_config);
assert_eq!(
Ok(
concat!(
"(",
" scope: Global,",
" tags: Some([",
" (\"title\", [",
" \"toc\",",
" ]),",
" ]),",
" entries: [",
" (",
" entry_type: Edition,",
" uid: \"edition\",",
" start_stop: Some((0, 15)),",
" tags: None,",
" loop: Some((None, 0)),",
" sub_entries: [",
" (",
" entry_type: Chapter,",
" uid: \"chapter1\",",
" start_stop: Some((0, 10)),",
" tags: None,",
" loop: Some((None, 0)),",
" sub_entries: [",
" (",
" entry_type: Chapter,",
" uid: \"chapter1.1\",",
" start_stop: Some((0, 4)),",
" tags: Some([",
" (\"title\", [",
" \"chapter 1.1\",",
" ]),",
" ]),",
" loop: Some((None, 0)),",
" sub_entries: [",
" ],",
" ),",
" (",
" entry_type: Chapter,",
" uid: \"chapter1.2\",",
" start_stop: Some((4, 10)),",
" tags: Some([",
" (\"title\", [",
" \"chapter 1.2\",",
" ]),",
" ]),",
" loop: Some((None, 0)),",
" sub_entries: [",
" ],",
" ),",
" ],",
" ),",
" (",
" entry_type: Chapter,",
" uid: \"chapter2\",",
" start_stop: Some((10, 15)),",
" tags: Some([",
" (\"title\", [",
" \"chapter 2\",",
" ]),",
" ]),",
" loop: Some((None, 0)),",
" sub_entries: [",
" ],",
" ),",
" ],",
" ),",
" ],",
")",
).to_owned()
),
Ok(concat!(
"(",
" scope: Global,",
" tags: Some([",
" (\"title\", [",
" \"toc\",",
" ]),",
" ]),",
" entries: [",
" (",
" entry_type: Edition,",
" uid: \"edition\",",
" start_stop: Some((0, 15)),",
" tags: None,",
" loop: Some((None, 0)),",
" sub_entries: [",
" (",
" entry_type: Chapter,",
" uid: \"chapter1\",",
" start_stop: Some((0, 10)),",
" tags: None,",
" loop: Some((None, 0)),",
" sub_entries: [",
" (",
" entry_type: Chapter,",
" uid: \"chapter1.1\",",
" start_stop: Some((0, 4)),",
" tags: Some([",
" (\"title\", [",
" \"chapter 1.1\",",
" ]),",
" ]),",
" loop: Some((None, 0)),",
" sub_entries: [",
" ],",
" ),",
" (",
" entry_type: Chapter,",
" uid: \"chapter1.2\",",
" start_stop: Some((4, 10)),",
" tags: Some([",
" (\"title\", [",
" \"chapter 1.2\",",
" ]),",
" ]),",
" loop: Some((None, 0)),",
" sub_entries: [",
" ],",
" ),",
" ],",
" ),",
" (",
" entry_type: Chapter,",
" uid: \"chapter2\",",
" start_stop: Some((10, 15)),",
" tags: Some([",
" (\"title\", [",
" \"chapter 2\",",
" ]),",
" ]),",
" loop: Some((None, 0)),",
" sub_entries: [",
" ],",
" ),",
" ],",
" ),",
" ],",
")",
).to_owned()),
res,
);
}
@ -366,7 +370,10 @@ mod tests {
assert_eq!("chapter1.1", chapter1_1.get_uid());
assert_eq!(Some((0, 4)), chapter1_1.get_start_stop_times());
let tags = chapter1_1.get_tags().unwrap();
assert_eq!(Some("chapter 1.1"), tags.get_index::<Title>(0).unwrap().get());
assert_eq!(
Some("chapter 1.1"),
tags.get_index::<Title>(0).unwrap().get()
);
assert_eq!(0, chapter1_1.get_sub_entries().len());
let chapter1_2 = &chap1_sub_entries[1];
@ -374,7 +381,10 @@ mod tests {
assert_eq!("chapter1.2", chapter1_2.get_uid());
assert_eq!(Some((4, 10)), chapter1_2.get_start_stop_times());
let tags = chapter1_2.get_tags().unwrap();
assert_eq!(Some("chapter 1.2"), tags.get_index::<Title>(0).unwrap().get());
assert_eq!(
Some("chapter 1.2"),
tags.get_index::<Title>(0).unwrap().get()
);
assert_eq!(0, chapter1_2.get_sub_entries().len());
let chapter2 = &sub_entries[1];

View file

@ -14,7 +14,7 @@ use num_rational::Rational32;
use serde::de;
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use serde::ser;
use serde::ser::{Serialize, Serializer, SerializeTuple};
use serde::ser::{Serialize, SerializeTuple, Serializer};
use std::{fmt, mem};
@ -40,10 +40,8 @@ lazy_static! {
pub(crate) static ref FRACTION_OTHER_TYPE_ID: usize = get_other_type_id::<Fraction>();
pub(crate) static ref FRACTION_RANGE_OTHER_TYPE_ID: usize =
get_other_type_id::<FractionRange>();
pub(crate) static ref INT_RANGE_I32_OTHER_TYPE_ID: usize =
get_other_type_id::<IntRange<i32>>();
pub(crate) static ref INT_RANGE_I64_OTHER_TYPE_ID: usize =
get_other_type_id::<IntRange<i64>>();
pub(crate) static ref INT_RANGE_I32_OTHER_TYPE_ID: usize = get_other_type_id::<IntRange<i32>>();
pub(crate) static ref INT_RANGE_I64_OTHER_TYPE_ID: usize = get_other_type_id::<IntRange<i64>>();
pub(crate) static ref LIST_OTHER_TYPE_ID: usize = get_other_type_id::<List>();
pub(crate) static ref SAMPLE_OTHER_TYPE_ID: usize = get_other_type_id::<Sample>();
}
@ -235,7 +233,8 @@ impl<'de> Visitor<'de> for SendValueVisitor {
}
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"))?;
let send_value = de_send_value!(type_name, seq)?
.ok_or(de::Error::custom("Expected a value for `Value`"))?;
@ -245,7 +244,7 @@ impl<'de> Visitor<'de> for SendValueVisitor {
impl<'de> Deserialize<'de> for SendValue {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_tuple(2, SendValueVisitor{})
deserializer.deserialize_tuple(2, SendValueVisitor {})
}
}
@ -272,10 +271,10 @@ mod tests {
#[test]
fn test_serialize_simple() {
use Bitmask;
use Fraction;
use FractionRange;
use IntRange;
use Bitmask;
::init().unwrap();
@ -295,18 +294,7 @@ mod tests {
let fraction_range = FractionRange::new(Fraction::new(1, 3), Fraction::new(1, 2));
let res = ron::ser::to_string_pretty(&fraction_range, pretty_config.clone());
assert_eq!(
Ok(
concat!(
"(",
" min: (1, 3),",
" max: (1, 2),",
")"
)
.to_owned()
),
res,
);
assert_eq!(Ok("( min: (1, 3), max: (1, 2),)".to_owned()), res);
let res = serde_json::to_string(&fraction_range).unwrap();
assert_eq!("{\"min\":[1,3],\"max\":[1,2]}".to_owned(), res);
@ -314,19 +302,7 @@ mod tests {
// IntRange
let int_range = IntRange::<i32>::new_with_step(0, 42, 21);
let res = ron::ser::to_string_pretty(&int_range, pretty_config.clone());
assert_eq!(
Ok(
concat!(
"(",
" min: 0,",
" max: 42,",
" step: 21,",
")"
)
.to_owned()
),
res,
);
assert_eq!(Ok("( min: 0, max: 42, step: 21,)".to_owned()), res,);
let res = serde_json::to_string(&int_range).unwrap();
assert_eq!("{\"min\":0,\"max\":42,\"step\":21}".to_owned(), res);
@ -368,23 +344,19 @@ mod tests {
let res = ron::ser::to_string_pretty(&array, pretty_config.clone());
assert_eq!(
Ok(
concat!(
"[",
" (\"Fraction\", (1, 3)),",
" (\"Fraction\", (1, 2)),",
" (\"String\", \"test str\"),",
"]"
)
.to_owned()
),
Ok(concat!(
"[",
" (\"Fraction\", (1, 3)),",
" (\"Fraction\", (1, 2)),",
" (\"String\", \"test str\"),",
"]"
).to_owned()),
res,
);
let res = serde_json::to_string(&array).unwrap();
assert_eq!(
"[[\"Fraction\",[1,3]],[\"Fraction\",[1,2]],[\"String\",\"test str\"]]"
.to_owned(),
"[[\"Fraction\",[1,3]],[\"Fraction\",[1,2]],[\"String\",\"test str\"]]".to_owned(),
res
);
@ -399,15 +371,12 @@ mod tests {
let res = ron::ser::to_string_pretty(&list, pretty_config.clone());
assert_eq!(
Ok(
concat!(
"[",
" (\"Fraction\", (1, 2)),",
" (\"String\", \"test str\"),",
"]"
)
.to_owned()
),
Ok(concat!(
"[",
" (\"Fraction\", (1, 2)),",
" (\"String\", \"test str\"),",
"]"
).to_owned()),
res,
);
}
@ -418,10 +387,10 @@ mod tests {
extern crate ron;
extern crate serde_json;
use Bitmask;
use Fraction;
use FractionRange;
use IntRange;
use Bitmask;
::init().unwrap();
@ -485,8 +454,7 @@ mod tests {
::init().unwrap();
// Array
let array_ron =
r#"[
let array_ron = r#"[
("Fraction", (1, 3)),
("Fraction", (1, 2)),
("String", "test str"),
@ -505,8 +473,7 @@ mod tests {
assert_eq!("test str".to_owned(), slice[2].get::<String>().unwrap());
let array_json =
r#"[["Fraction",[1,3]],["Fraction",[1,2]],["String","test str"]]"#;
let array_json = r#"[["Fraction",[1,3]],["Fraction",[1,2]],["String","test str"]]"#;
let array: Array = serde_json::from_str(array_json).unwrap();
let slice = array.as_slice();
assert_eq!(3, slice.len());
@ -522,8 +489,7 @@ mod tests {
assert_eq!("test str".to_owned(), slice[2].get::<String>().unwrap());
// List
let list_ron =
r#"[
let list_ron = r#"[
("Fraction", (1, 2)),
("String", "test str"),
]"#;