2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use serde::{
|
|
|
|
de,
|
|
|
|
de::{Deserialize, Deserializer},
|
|
|
|
ser::{Serialize, Serializer},
|
|
|
|
};
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use crate::{
|
|
|
|
format::{FormattedValue, FormattedValueIntrinsic, SpecificFormattedValueIntrinsic},
|
|
|
|
Format, FormattedSegment, GenericFormattedValue, Segment, SegmentFlags,
|
|
|
|
};
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
#[derive(serde::Serialize, serde::Deserialize)]
|
2018-07-28 14:54:39 +00:00
|
|
|
struct FormattedSegmentSerde {
|
2018-07-16 19:46:10 +00:00
|
|
|
flags: SegmentFlags,
|
|
|
|
rate: f64,
|
|
|
|
applied_rate: f64,
|
|
|
|
format: Format,
|
|
|
|
base: i64,
|
|
|
|
offset: i64,
|
|
|
|
start: i64,
|
|
|
|
stop: i64,
|
|
|
|
time: i64,
|
|
|
|
position: i64,
|
|
|
|
duration: i64,
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:27:16 +00:00
|
|
|
impl<T: FormattedValueIntrinsic> Serialize for FormattedSegment<T> {
|
2018-07-28 14:54:39 +00:00
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
let fmt_seg = unsafe {
|
|
|
|
FormattedSegmentSerde {
|
2021-04-11 19:39:50 +00:00
|
|
|
flags: self.flags(),
|
|
|
|
rate: self.rate(),
|
|
|
|
applied_rate: self.applied_rate(),
|
|
|
|
format: self.format(),
|
2020-10-27 17:27:16 +00:00
|
|
|
base: self.base().into_raw_value(),
|
|
|
|
offset: self.offset().into_raw_value(),
|
|
|
|
start: self.start().into_raw_value(),
|
|
|
|
stop: self.stop().into_raw_value(),
|
|
|
|
time: self.time().into_raw_value(),
|
|
|
|
position: self.position().into_raw_value(),
|
|
|
|
duration: self.duration().into_raw_value(),
|
2018-07-28 14:54:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
fmt_seg.serialize(serializer)
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Segment {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-28 14:54:39 +00:00
|
|
|
FormattedSegmentSerde::deserialize(deserializer).map(|fmt_seg_de| {
|
|
|
|
let mut segment = Self::new();
|
|
|
|
segment.set_flags(fmt_seg_de.flags);
|
|
|
|
segment.set_rate(fmt_seg_de.rate);
|
|
|
|
segment.set_applied_rate(fmt_seg_de.applied_rate);
|
|
|
|
segment.set_format(fmt_seg_de.format);
|
|
|
|
segment.set_base(GenericFormattedValue::new(
|
|
|
|
fmt_seg_de.format,
|
|
|
|
fmt_seg_de.base,
|
|
|
|
));
|
|
|
|
segment.set_offset(GenericFormattedValue::new(
|
|
|
|
fmt_seg_de.format,
|
|
|
|
fmt_seg_de.offset,
|
|
|
|
));
|
|
|
|
segment.set_start(GenericFormattedValue::new(
|
|
|
|
fmt_seg_de.format,
|
|
|
|
fmt_seg_de.start,
|
|
|
|
));
|
|
|
|
segment.set_stop(GenericFormattedValue::new(
|
|
|
|
fmt_seg_de.format,
|
|
|
|
fmt_seg_de.stop,
|
|
|
|
));
|
|
|
|
segment.set_time(GenericFormattedValue::new(
|
|
|
|
fmt_seg_de.format,
|
|
|
|
fmt_seg_de.time,
|
|
|
|
));
|
|
|
|
segment.set_position(GenericFormattedValue::new(
|
|
|
|
fmt_seg_de.format,
|
|
|
|
fmt_seg_de.position,
|
|
|
|
));
|
|
|
|
segment.set_duration(GenericFormattedValue::new(
|
|
|
|
fmt_seg_de.format,
|
|
|
|
fmt_seg_de.duration,
|
|
|
|
));
|
|
|
|
|
|
|
|
segment
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:27:16 +00:00
|
|
|
impl<'de, T: SpecificFormattedValueIntrinsic> Deserialize<'de> for FormattedSegment<T> {
|
2018-07-28 14:54:39 +00:00
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-29 16:18:24 +00:00
|
|
|
Segment::deserialize(deserializer).and_then(|segment| {
|
|
|
|
segment.downcast::<T>().map_err(|segment| {
|
|
|
|
de::Error::custom(format!(
|
|
|
|
"failed to convert segment with format {:?} to {:?}",
|
2021-04-11 19:39:50 +00:00
|
|
|
segment.format(),
|
2022-07-03 16:04:06 +00:00
|
|
|
T::default_format(),
|
2018-07-29 16:18:24 +00:00
|
|
|
))
|
2018-07-28 14:54:39 +00:00
|
|
|
})
|
2018-07-29 16:18:24 +00:00
|
|
|
})
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-01-03 18:58:25 +00:00
|
|
|
use crate::{ClockTime, Format, GenericFormattedValue, Segment, SegmentFlags};
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialize() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
let mut segment = Segment::new();
|
|
|
|
segment.set_flags(SegmentFlags::RESET | SegmentFlags::SEGMENT);
|
|
|
|
segment.set_rate(1f64);
|
|
|
|
segment.set_applied_rate(0.9f64);
|
|
|
|
segment.set_format(Format::Time);
|
2020-10-27 17:27:16 +00:00
|
|
|
segment.set_base(GenericFormattedValue::from(ClockTime::from_nseconds(123)));
|
|
|
|
segment.set_offset(GenericFormattedValue::from(ClockTime::from_nseconds(42)));
|
|
|
|
segment.set_start(GenericFormattedValue::from(ClockTime::from_nseconds(1024)));
|
|
|
|
segment.set_stop(GenericFormattedValue::from(ClockTime::from_nseconds(2048)));
|
|
|
|
segment.set_time(GenericFormattedValue::from(ClockTime::from_nseconds(1042)));
|
|
|
|
segment.set_position(GenericFormattedValue::from(ClockTime::from_nseconds(256)));
|
|
|
|
segment.set_duration(GenericFormattedValue::from(ClockTime::NONE));
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2021-10-23 07:15:29 +00:00
|
|
|
let pretty_config = ron::ser::PrettyConfig::new().new_line("".to_string());
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
let res = ron::ser::to_string_pretty(&segment, pretty_config);
|
|
|
|
assert_eq!(
|
2018-07-22 14:55:24 +00:00
|
|
|
Ok(concat!(
|
|
|
|
"(",
|
2022-05-05 10:40:07 +00:00
|
|
|
" flags: \"reset+segment\",",
|
2022-06-24 07:20:44 +00:00
|
|
|
" rate: 1.0,",
|
2018-07-22 14:55:24 +00:00
|
|
|
" applied_rate: 0.9,",
|
|
|
|
" format: Time,",
|
|
|
|
" base: 123,",
|
|
|
|
" offset: 42,",
|
|
|
|
" start: 1024,",
|
|
|
|
" stop: 2048,",
|
|
|
|
" time: 1042,",
|
|
|
|
" position: 256,",
|
|
|
|
" duration: -1,",
|
|
|
|
")"
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.to_owned()),
|
2018-07-16 19:46:10 +00:00
|
|
|
res,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-07-28 14:54:39 +00:00
|
|
|
fn test_deserialize_segment() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
let segment_ron = r#"
|
|
|
|
(
|
2022-05-05 10:40:07 +00:00
|
|
|
flags: "reset+segment",
|
2018-07-16 19:46:10 +00:00
|
|
|
rate: 1,
|
|
|
|
applied_rate: 0.9,
|
|
|
|
format: Time,
|
|
|
|
base: 123,
|
|
|
|
offset: 42,
|
|
|
|
start: 1024,
|
|
|
|
stop: 2048,
|
|
|
|
time: 1042,
|
|
|
|
position: 256,
|
|
|
|
duration: -1,
|
|
|
|
)
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let segment: Segment = ron::de::from_str(segment_ron).unwrap();
|
2021-04-11 19:39:50 +00:00
|
|
|
assert_eq!(segment.flags(), SegmentFlags::RESET | SegmentFlags::SEGMENT);
|
|
|
|
assert!((segment.rate() - 1f64).abs() < std::f64::EPSILON);
|
|
|
|
assert!((segment.applied_rate() - 0.9f64).abs() < std::f64::EPSILON);
|
|
|
|
assert_eq!(segment.format(), Format::Time);
|
2020-10-27 17:27:16 +00:00
|
|
|
assert_eq!(segment.flags(), SegmentFlags::RESET | SegmentFlags::SEGMENT);
|
|
|
|
assert!((segment.rate() - 1f64).abs() < std::f64::EPSILON);
|
|
|
|
assert!((segment.applied_rate() - 0.9f64).abs() < std::f64::EPSILON);
|
|
|
|
assert_eq!(segment.format(), Format::Time);
|
2018-07-22 14:55:24 +00:00
|
|
|
assert_eq!(
|
2021-04-11 19:39:50 +00:00
|
|
|
segment.base(),
|
2020-10-27 17:27:16 +00:00
|
|
|
GenericFormattedValue::from(ClockTime::from_nseconds(123)),
|
2018-07-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-04-11 19:39:50 +00:00
|
|
|
segment.offset(),
|
2020-10-27 17:27:16 +00:00
|
|
|
GenericFormattedValue::from(ClockTime::from_nseconds(42)),
|
2018-07-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-04-11 19:39:50 +00:00
|
|
|
segment.start(),
|
2020-10-27 17:27:16 +00:00
|
|
|
GenericFormattedValue::from(ClockTime::from_nseconds(1024)),
|
2018-07-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-04-11 19:39:50 +00:00
|
|
|
segment.stop(),
|
2020-10-27 17:27:16 +00:00
|
|
|
GenericFormattedValue::from(ClockTime::from_nseconds(2048)),
|
2018-07-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-04-11 19:39:50 +00:00
|
|
|
segment.time(),
|
2020-10-27 17:27:16 +00:00
|
|
|
GenericFormattedValue::from(ClockTime::from_nseconds(1042)),
|
2018-07-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-04-11 19:39:50 +00:00
|
|
|
segment.position(),
|
2020-10-27 17:27:16 +00:00
|
|
|
GenericFormattedValue::from(ClockTime::from_nseconds(256)),
|
2018-07-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-04-11 19:39:50 +00:00
|
|
|
segment.duration(),
|
2020-10-27 17:27:16 +00:00
|
|
|
GenericFormattedValue::from(ClockTime::NONE),
|
2018-07-22 14:55:24 +00:00
|
|
|
);
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
2018-07-28 14:54:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deserialize_formatted() {
|
2023-01-03 18:58:25 +00:00
|
|
|
use crate::{format::Time, FormattedSegment};
|
2018-07-28 14:54:39 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-28 14:54:39 +00:00
|
|
|
|
|
|
|
let segment_ron = r#"
|
|
|
|
(
|
2022-05-05 10:40:07 +00:00
|
|
|
flags: "reset+segment",
|
2018-07-28 14:54:39 +00:00
|
|
|
rate: 1,
|
|
|
|
applied_rate: 0.9,
|
|
|
|
format: Time,
|
|
|
|
base: 123,
|
|
|
|
offset: 42,
|
|
|
|
start: 1024,
|
|
|
|
stop: 2048,
|
|
|
|
time: 1042,
|
|
|
|
position: 256,
|
|
|
|
duration: -1,
|
|
|
|
)
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let fmt_seg: FormattedSegment<Time> = ron::de::from_str(segment_ron).unwrap();
|
2021-04-11 19:39:50 +00:00
|
|
|
assert_eq!(fmt_seg.flags(), SegmentFlags::RESET | SegmentFlags::SEGMENT);
|
|
|
|
assert!((fmt_seg.rate() - 1f64).abs() < std::f64::EPSILON);
|
|
|
|
assert!((fmt_seg.applied_rate() - 0.9f64).abs() < std::f64::EPSILON);
|
|
|
|
assert_eq!(fmt_seg.format(), Format::Time);
|
2020-10-27 17:27:16 +00:00
|
|
|
assert_eq!(fmt_seg.base(), Some(ClockTime::from_nseconds(123)));
|
|
|
|
assert_eq!(fmt_seg.offset(), Some(ClockTime::from_nseconds(42)));
|
|
|
|
assert_eq!(fmt_seg.start(), Some(ClockTime::from_nseconds(1024)));
|
|
|
|
assert_eq!(fmt_seg.stop(), Some(ClockTime::from_nseconds(2048)));
|
|
|
|
assert_eq!(fmt_seg.time(), Some(ClockTime::from_nseconds(1042)));
|
|
|
|
assert_eq!(fmt_seg.position(), Some(ClockTime::from_nseconds(256)));
|
|
|
|
assert_eq!(fmt_seg.duration(), ClockTime::NONE);
|
2018-07-28 14:54:39 +00:00
|
|
|
}
|
2018-07-29 16:18:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serde_roundtrip() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-29 16:18:24 +00:00
|
|
|
|
|
|
|
let mut segment = Segment::new();
|
|
|
|
segment.set_flags(SegmentFlags::RESET | SegmentFlags::SEGMENT);
|
|
|
|
segment.set_rate(1f64);
|
|
|
|
segment.set_applied_rate(0.9f64);
|
|
|
|
segment.set_format(Format::Time);
|
2020-10-27 17:27:16 +00:00
|
|
|
segment.set_base(GenericFormattedValue::from(ClockTime::from_nseconds(123)));
|
|
|
|
segment.set_offset(GenericFormattedValue::from(ClockTime::from_nseconds(42)));
|
|
|
|
segment.set_start(GenericFormattedValue::from(ClockTime::from_nseconds(1024)));
|
|
|
|
segment.set_stop(GenericFormattedValue::from(ClockTime::from_nseconds(2048)));
|
|
|
|
segment.set_time(GenericFormattedValue::from(ClockTime::from_nseconds(1042)));
|
|
|
|
segment.set_position(GenericFormattedValue::from(ClockTime::from_nseconds(256)));
|
|
|
|
segment.set_duration(GenericFormattedValue::from(ClockTime::NONE));
|
2018-07-29 16:18:24 +00:00
|
|
|
let segment_se = ron::ser::to_string(&segment).unwrap();
|
|
|
|
|
|
|
|
let segment_de: Segment = ron::de::from_str(segment_se.as_str()).unwrap();
|
2021-04-11 19:39:50 +00:00
|
|
|
assert_eq!(segment_de.flags(), segment.flags());
|
|
|
|
assert!((segment_de.rate() - segment.rate()).abs() < std::f64::EPSILON);
|
|
|
|
assert!((segment_de.applied_rate() - segment.applied_rate()).abs() < std::f64::EPSILON);
|
|
|
|
assert_eq!(segment_de.format(), segment.format());
|
|
|
|
assert_eq!(segment_de.base(), segment.base());
|
|
|
|
assert_eq!(segment_de.offset(), segment.offset());
|
|
|
|
assert_eq!(segment_de.start(), segment.start());
|
|
|
|
assert_eq!(segment_de.stop(), segment.stop());
|
|
|
|
assert_eq!(segment_de.time(), segment.time());
|
|
|
|
assert_eq!(segment_de.position(), segment.position());
|
|
|
|
assert_eq!(segment_de.duration(), segment.duration());
|
2018-07-29 16:18:24 +00:00
|
|
|
}
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|