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
use crate::mapping::Mappable;
use crate::record::TypedValue;
use crate::{ElasticMapping, Reference};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
use crate::ser;
use super::{Feed, Post};
#[derive(Serialize, Deserialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct Media {
pub content_url: String,
pub content_size: Option<u32>,
pub encoding_format: Option<String>,
#[serde(default)]
#[serde(deserialize_with = "ser::deserialize_duration")]
pub duration: Option<f32>,
pub transcript: Option<Transcript>,
pub nlp: Option<Value>,
#[serde(flatten)]
pub other: serde_json::Map<String, Value>,
#[serde(default)]
pub feeds: Vec<Reference<Feed>>,
#[serde(default)]
pub posts: Vec<Reference<Post>>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, JsonSchema)]
pub struct Transcript {
pub text: String,
pub parts: Vec<TranscriptPart>,
pub segments: Vec<Segment>,
#[serde(default)]
pub meta: HashMap<String, serde_json::Value>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, JsonSchema)]
pub struct Segment {
pub start: f32,
pub end: f32,
#[serde(rename = "type")]
pub typ: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<serde_json::Value>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, JsonSchema)]
pub struct TranscriptPart {
pub conf: f32,
pub start: f32,
pub end: f32,
pub word: String,
pub suffix: Option<String>,
}
impl TranscriptPart {
pub fn duration(&self) -> f32 {
self.end - self.start
}
}
impl TypedValue for Media {
const NAME: &'static str = "oas.Media";
}
impl Mappable for Media {}
impl ElasticMapping for Media {
fn elastic_mapping() -> serde_json::Value {
json!({
"transcript": {
"type": "object",
"enabled": false
},
"contentUrl":{
"type": "keyword"
},
"duration": {
"type": "float"
},
"contentSize": {
"type": "float"
},
"encodingFormat": {
"type": "keyword"
},
"nlp": {
"type": "object"
},
"posts": {
"type": "keyword"
},
"feeds": {
"type": "keyword"
},
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize_duration() {
let source = r#"
{
"contentUrl": "foo",
"duration": "02:03"
}
"#;
let media: Media = serde_json::from_str(source).expect("failed to deserialize");
assert_eq!(media.duration, Some(123.));
let source = r#"
{
"contentUrl": "foo",
"duration": "02:03:01"
}
"#;
let media: Media = serde_json::from_str(source).expect("failed to deserialize");
assert_eq!(media.duration, Some(7381.));
let source = r#"
{
"contentUrl": "foo",
"duration": "64"
}
"#;
let media: Media = serde_json::from_str(source).expect("failed to deserialize");
assert_eq!(media.duration, Some(64.));
let source = r#"
{
"contentUrl": "foo",
"duration": 123
}
"#;
let media: Media = serde_json::from_str(source).expect("failed to deserialize");
assert_eq!(media.duration, Some(123.));
let source = r#"
{
"contentUrl": "foo",
"duration": 562.5011
}
"#;
let media: Media = serde_json::from_str(source).expect("failed to deserialize");
assert_eq!(media.duration, Some(562.5011));
let source = r#"
{
"contentUrl": "foo"
}
"#;
let media: Media = serde_json::from_str(source).expect("failed to deserialize");
assert_eq!(media.duration, None);
}
}