1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Take a look at the license at the top of the repository in the LICENSE file.

use serde::{
    de,
    de::{Deserialize, Deserializer},
    ser::{Serialize, Serializer},
};

use crate::{
    format::{FormattedValue, FormattedValueIntrinsic, SpecificFormattedValueIntrinsic},
    Format, FormattedSegment, GenericFormattedValue, Segment, SegmentFlags,
};

#[derive(serde::Serialize, serde::Deserialize)]
struct FormattedSegmentSerde {
    flags: SegmentFlags,
    rate: f64,
    applied_rate: f64,
    format: Format,
    base: i64,
    offset: i64,
    start: i64,
    stop: i64,
    time: i64,
    position: i64,
    duration: i64,
}

impl<T: FormattedValueIntrinsic> Serialize for FormattedSegment<T> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let fmt_seg = unsafe {
            FormattedSegmentSerde {
                flags: self.flags(),
                rate: self.rate(),
                applied_rate: self.applied_rate(),
                format: self.format(),
                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(),
            }
        };
        fmt_seg.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Segment {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        skip_assert_initialized!();
        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
        })
    }
}

impl<'de, T: SpecificFormattedValueIntrinsic> Deserialize<'de> for FormattedSegment<T> {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        skip_assert_initialized!();
        Segment::deserialize(deserializer).and_then(|segment| {
            segment.downcast::<T>().map_err(|segment| {
                de::Error::custom(format!(
                    "failed to convert segment with format {:?} to {:?}",
                    segment.format(),
                    T::default_format(),
                ))
            })
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::{ClockTime, Format, GenericFormattedValue, Segment, SegmentFlags};

    #[test]
    fn test_serialize() {
        crate::init().unwrap();

        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);
        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));

        let pretty_config = ron::ser::PrettyConfig::new().new_line("".to_string());

        let res = ron::ser::to_string_pretty(&segment, pretty_config);
        assert_eq!(
            Ok(concat!(
                "(",
                "    flags: \"reset+segment\",",
                "    rate: 1.0,",
                "    applied_rate: 0.9,",
                "    format: Time,",
                "    base: 123,",
                "    offset: 42,",
                "    start: 1024,",
                "    stop: 2048,",
                "    time: 1042,",
                "    position: 256,",
                "    duration: -1,",
                ")"
            )
            .to_owned()),
            res,
        );
    }

    #[test]
    fn test_deserialize_segment() {
        crate::init().unwrap();

        let segment_ron = r#"
            (
                flags: "reset+segment",
                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();
        assert_eq!(segment.flags(), SegmentFlags::RESET | SegmentFlags::SEGMENT);
        assert!((segment.rate() - 1f64).abs() < f64::EPSILON);
        assert!((segment.applied_rate() - 0.9f64).abs() < f64::EPSILON);
        assert_eq!(segment.format(), Format::Time);
        assert_eq!(segment.flags(), SegmentFlags::RESET | SegmentFlags::SEGMENT);
        assert!((segment.rate() - 1f64).abs() < f64::EPSILON);
        assert!((segment.applied_rate() - 0.9f64).abs() < f64::EPSILON);
        assert_eq!(segment.format(), Format::Time);
        assert_eq!(
            segment.base(),
            GenericFormattedValue::from(ClockTime::from_nseconds(123)),
        );
        assert_eq!(
            segment.offset(),
            GenericFormattedValue::from(ClockTime::from_nseconds(42)),
        );
        assert_eq!(
            segment.start(),
            GenericFormattedValue::from(ClockTime::from_nseconds(1024)),
        );
        assert_eq!(
            segment.stop(),
            GenericFormattedValue::from(ClockTime::from_nseconds(2048)),
        );
        assert_eq!(
            segment.time(),
            GenericFormattedValue::from(ClockTime::from_nseconds(1042)),
        );
        assert_eq!(
            segment.position(),
            GenericFormattedValue::from(ClockTime::from_nseconds(256)),
        );
        assert_eq!(
            segment.duration(),
            GenericFormattedValue::from(ClockTime::NONE),
        );
    }

    #[test]
    fn test_deserialize_formatted() {
        use crate::{format::Time, FormattedSegment};

        crate::init().unwrap();

        let segment_ron = r#"
            (
                flags: "reset+segment",
                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();
        assert_eq!(fmt_seg.flags(), SegmentFlags::RESET | SegmentFlags::SEGMENT);
        assert!((fmt_seg.rate() - 1f64).abs() < f64::EPSILON);
        assert!((fmt_seg.applied_rate() - 0.9f64).abs() < f64::EPSILON);
        assert_eq!(fmt_seg.format(), Format::Time);
        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);
    }

    #[test]
    fn test_serde_roundtrip() {
        crate::init().unwrap();

        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);
        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));
        let segment_se = ron::ser::to_string(&segment).unwrap();

        let segment_de: Segment = ron::de::from_str(segment_se.as_str()).unwrap();
        assert_eq!(segment_de.flags(), segment.flags());
        assert!((segment_de.rate() - segment.rate()).abs() < f64::EPSILON);
        assert!((segment_de.applied_rate() - segment.applied_rate()).abs() < 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());
    }
}