1use super::data::{DataFormat, DataFrameDescriptor};
2use crate::{
3 frame::{Frame, FrameData, FrameSpec},
4 variant::Variant,
5 Error, FrameDescriptor, FrameDescriptorSpec, MediaType, Result,
6};
7
8pub type DataFrame<'a> = Frame<'a, DataFrameDescriptor>;
9
10pub struct DataFrameCreator;
11
12impl DataFrameCreator {
13 pub fn create(&self, format: DataFormat) -> Result<Frame<'static>> {
14 let desc = DataFrameDescriptor::new(format);
15
16 self.create_with_descriptor(desc)
17 }
18
19 pub fn create_with_descriptor(&self, desc: DataFrameDescriptor) -> Result<Frame<'static>> {
20 Ok(Frame::from_data(FrameDescriptor::Data(desc), FrameData::Variant(Variant::new())))
21 }
22
23 pub fn create_from_variant(&self, variant: &Variant) -> Result<Frame<'static>> {
24 Ok(Frame::from_data(FrameDescriptor::Data(DataFrameDescriptor::new(DataFormat::Variant)), FrameData::Variant(variant.clone())))
25 }
26}
27
28impl<D: FrameDescriptorSpec> Frame<'_, D> {
29 pub fn data(&self) -> Option<&Variant> {
30 if let FrameData::Variant(v) = &self.data {
31 Some(v)
32 } else {
33 None
34 }
35 }
36
37 pub fn data_mut(&mut self) -> Option<&mut Variant> {
38 if let FrameData::Variant(v) = &mut self.data {
39 Some(v)
40 } else {
41 None
42 }
43 }
44}
45
46impl Frame<'_> {
47 pub fn data_creator() -> DataFrameCreator {
48 DataFrameCreator
49 }
50
51 pub fn data_descriptor(&self) -> Option<&DataFrameDescriptor> {
52 #[allow(irrefutable_let_patterns)]
53 if let FrameDescriptor::Data(desc) = &self.desc {
54 Some(desc)
55 } else {
56 None
57 }
58 }
59
60 pub fn is_data(&self) -> bool {
61 self.desc.is_data()
62 }
63}
64
65impl DataFrame<'_> {
66 pub fn new(format: DataFormat) -> DataFrame<'static> {
67 Self::new_with_descriptor(DataFrameDescriptor::new(format))
68 }
69
70 pub fn new_with_descriptor(desc: DataFrameDescriptor) -> DataFrame<'static> {
71 Frame::from_data_with_generic_descriptor(desc, FrameData::Variant(Variant::new()))
72 }
73}
74
75impl<'a> From<DataFrame<'a>> for Frame<'a> {
76 fn from(frame: DataFrame<'a>) -> Self {
77 Frame {
78 desc: FrameDescriptor::Data(frame.desc),
79 source: frame.source,
80 pts: frame.pts,
81 dts: frame.dts,
82 duration: frame.duration,
83 time_base: frame.time_base,
84 metadata: frame.metadata,
85 data: frame.data,
86 }
87 }
88}
89
90impl<'a> TryFrom<Frame<'a>> for DataFrame<'a> {
91 type Error = Error;
92
93 fn try_from(frame: Frame<'a>) -> Result<Self> {
94 #[allow(irrefutable_let_patterns)]
95 if let FrameDescriptor::Data(desc) = frame.desc {
96 Ok(Frame {
97 desc,
98 source: frame.source,
99 pts: frame.pts,
100 dts: frame.dts,
101 duration: frame.duration,
102 time_base: frame.time_base,
103 metadata: frame.metadata,
104 data: frame.data,
105 })
106 } else {
107 Err(Error::Invalid("not data frame".to_string()))
108 }
109 }
110}
111
112impl FrameSpec<DataFrameDescriptor> for DataFrame<'_> {
113 fn new_with_descriptor(desc: DataFrameDescriptor) -> Result<Frame<'static, DataFrameDescriptor>> {
114 Ok(DataFrame::new_with_descriptor(desc))
115 }
116
117 fn media_type(&self) -> MediaType {
118 MediaType::Data
119 }
120}