-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcue.rs
More file actions
118 lines (103 loc) · 3.24 KB
/
Copy pathcue.rs
File metadata and controls
118 lines (103 loc) · 3.24 KB
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
//! The [`Cue`] value type — a single timed text segment.
use serde::{Deserialize, Serialize};
/// A single timed caption/subtitle segment.
///
/// Times are in milliseconds from the start of the media. `end_ms` is the
/// inclusive on-screen end of the cue; `end_ms == start_ms` represents an
/// instantaneous cue.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Cue {
/// Cue start time in milliseconds.
pub start_ms: u64,
/// Cue end time in milliseconds.
pub end_ms: u64,
/// The text shown for this cue. May contain newlines.
pub text: String,
}
impl Cue {
/// Construct a new cue.
pub fn new(start_ms: u64, end_ms: u64, text: impl Into<String>) -> Self {
Self {
start_ms,
end_ms,
text: text.into(),
}
}
/// On-screen duration of the cue in milliseconds. Saturates at zero if
/// `end_ms < start_ms` (which should not occur in well-formed input but
/// can be encountered in adversarial captions data).
pub fn duration_ms(&self) -> u64 {
self.end_ms.saturating_sub(self.start_ms)
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn new_constructs_cue() {
let cue = Cue::new(0, 1000, "hello");
assert_eq!(cue.start_ms, 0);
assert_eq!(cue.end_ms, 1000);
assert_eq!(cue.text, "hello");
}
#[test]
fn new_accepts_string_and_str() {
let from_str = Cue::new(0, 100, "x");
let from_string = Cue::new(0, 100, String::from("x"));
assert_eq!(from_str, from_string);
}
#[test]
fn duration_ms_basic() {
let cue = Cue::new(500, 1500, "x");
assert_eq!(cue.duration_ms(), 1000);
}
#[test]
fn duration_ms_zero_length() {
let cue = Cue::new(500, 500, "x");
assert_eq!(cue.duration_ms(), 0);
}
#[test]
fn duration_ms_saturates_when_inverted() {
let cue = Cue::new(2000, 1000, "x");
assert_eq!(cue.duration_ms(), 0);
}
#[test]
fn equality_compares_all_fields() {
let a = Cue::new(0, 100, "hi");
let b = Cue::new(0, 100, "hi");
let c = Cue::new(0, 100, "bye");
let d = Cue::new(1, 100, "hi");
assert_eq!(a, b);
assert_ne!(a, c);
assert_ne!(a, d);
}
#[test]
fn serde_round_trip_json() {
let cue = Cue::new(1234, 5678, "hello\nworld");
let json = serde_json::to_string(&cue).expect("serialize");
let back: Cue = serde_json::from_str(&json).expect("deserialize");
assert_eq!(cue, back);
}
#[test]
fn serde_field_names_are_snake_case() {
let cue = Cue::new(1, 2, "x");
let json = serde_json::to_value(&cue).expect("serialize");
assert!(json.get("start_ms").is_some());
assert!(json.get("end_ms").is_some());
assert!(json.get("text").is_some());
}
#[test]
fn debug_impl_present() {
let cue = Cue::new(0, 100, "hi");
let dbg = format!("{cue:?}");
assert!(dbg.contains("Cue"));
assert!(dbg.contains("hi"));
}
#[test]
fn clone_independent() {
let a = Cue::new(0, 100, "hi");
let b = a.clone();
assert_eq!(a, b);
}
}