oximedia-clips 0.1.8

Professional clip management and logging system for OxiMedia
Documentation
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#![allow(dead_code)]
//! Playlist management for organized clip playback.
//!
//! This module provides playlist creation, ordering, shuffling, and
//! repeat-mode management for video clips. It supports named playlists
//! with metadata, duration tracking, and insertion/removal operations.

/// Unique identifier for a playlist.
pub type PlaylistId = u64;

/// Repeat mode for playlist playback.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RepeatMode {
    /// Play once and stop.
    None,
    /// Repeat the entire playlist.
    RepeatAll,
    /// Repeat the current clip.
    RepeatOne,
}

/// A single item in a playlist.
#[derive(Debug, Clone, PartialEq)]
pub struct PlaylistItem {
    /// Clip identifier or name.
    pub clip_id: String,
    /// Display title.
    pub title: String,
    /// Duration in milliseconds.
    pub duration_ms: u64,
    /// Start offset within the clip in milliseconds.
    pub start_offset_ms: u64,
    /// End offset within the clip in milliseconds (0 = full clip).
    pub end_offset_ms: u64,
}

impl PlaylistItem {
    /// Creates a new playlist item.
    pub fn new(clip_id: &str, title: &str, duration_ms: u64) -> Self {
        Self {
            clip_id: clip_id.to_string(),
            title: title.to_string(),
            duration_ms,
            start_offset_ms: 0,
            end_offset_ms: 0,
        }
    }

    /// Creates an item with custom in/out points.
    pub fn with_range(clip_id: &str, title: &str, duration_ms: u64, start: u64, end: u64) -> Self {
        Self {
            clip_id: clip_id.to_string(),
            title: title.to_string(),
            duration_ms,
            start_offset_ms: start,
            end_offset_ms: end,
        }
    }

    /// Returns the effective playback duration.
    pub fn effective_duration(&self) -> u64 {
        if self.end_offset_ms > self.start_offset_ms {
            self.end_offset_ms - self.start_offset_ms
        } else {
            self.duration_ms.saturating_sub(self.start_offset_ms)
        }
    }
}

/// A playlist containing ordered clip items.
#[derive(Debug, Clone)]
pub struct Playlist {
    /// Unique playlist ID.
    pub id: PlaylistId,
    /// Playlist name.
    pub name: String,
    /// Description.
    pub description: String,
    /// Items in the playlist.
    items: Vec<PlaylistItem>,
    /// Current playback index.
    current_index: usize,
    /// Repeat mode.
    repeat_mode: RepeatMode,
}

impl Playlist {
    /// Creates a new empty playlist.
    pub fn new(id: PlaylistId, name: &str) -> Self {
        Self {
            id,
            name: name.to_string(),
            description: String::new(),
            items: Vec::new(),
            current_index: 0,
            repeat_mode: RepeatMode::None,
        }
    }

    /// Sets the description.
    pub fn set_description(&mut self, desc: &str) {
        self.description = desc.to_string();
    }

    /// Sets the repeat mode.
    pub fn set_repeat_mode(&mut self, mode: RepeatMode) {
        self.repeat_mode = mode;
    }

    /// Returns the current repeat mode.
    pub fn repeat_mode(&self) -> RepeatMode {
        self.repeat_mode
    }

    /// Appends an item to the end of the playlist.
    pub fn push(&mut self, item: PlaylistItem) {
        self.items.push(item);
    }

    /// Inserts an item at a specific position.
    pub fn insert(&mut self, index: usize, item: PlaylistItem) {
        let pos = index.min(self.items.len());
        self.items.insert(pos, item);
        // Adjust current index if needed
        if pos <= self.current_index && !self.items.is_empty() {
            self.current_index += 1;
        }
    }

    /// Removes the item at the given index.
    pub fn remove(&mut self, index: usize) -> Option<PlaylistItem> {
        if index < self.items.len() {
            let item = self.items.remove(index);
            if self.current_index >= self.items.len() && !self.items.is_empty() {
                self.current_index = self.items.len() - 1;
            }
            Some(item)
        } else {
            None
        }
    }

    /// Returns the number of items.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Returns true if the playlist is empty.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Returns the total duration of all items in milliseconds.
    pub fn total_duration_ms(&self) -> u64 {
        self.items.iter().map(|i| i.effective_duration()).sum()
    }

    /// Returns a reference to the current item.
    pub fn current(&self) -> Option<&PlaylistItem> {
        self.items.get(self.current_index)
    }

    /// Returns the current playback index.
    pub fn current_index(&self) -> usize {
        self.current_index
    }

    /// Advances to the next item. Returns `true` if playback continues.
    pub fn next(&mut self) -> bool {
        if self.items.is_empty() {
            return false;
        }
        match self.repeat_mode {
            RepeatMode::RepeatOne => true,
            RepeatMode::RepeatAll => {
                self.current_index = (self.current_index + 1) % self.items.len();
                true
            }
            RepeatMode::None => {
                if self.current_index + 1 < self.items.len() {
                    self.current_index += 1;
                    true
                } else {
                    false
                }
            }
        }
    }

    /// Goes to the previous item. Returns `true` if successful.
    pub fn previous(&mut self) -> bool {
        if self.items.is_empty() {
            return false;
        }
        match self.repeat_mode {
            RepeatMode::RepeatOne => true,
            RepeatMode::RepeatAll => {
                if self.current_index == 0 {
                    self.current_index = self.items.len() - 1;
                } else {
                    self.current_index -= 1;
                }
                true
            }
            RepeatMode::None => {
                if self.current_index > 0 {
                    self.current_index -= 1;
                    true
                } else {
                    false
                }
            }
        }
    }

    /// Jumps to a specific index. Returns `true` if valid.
    pub fn jump_to(&mut self, index: usize) -> bool {
        if index < self.items.len() {
            self.current_index = index;
            true
        } else {
            false
        }
    }

    /// Resets playback to the beginning.
    pub fn reset(&mut self) {
        self.current_index = 0;
    }

    /// Moves an item from one position to another.
    pub fn move_item(&mut self, from: usize, to: usize) -> bool {
        if from >= self.items.len() || to >= self.items.len() {
            return false;
        }
        let item = self.items.remove(from);
        self.items.insert(to, item);
        true
    }

    /// Reverses the order of items.
    pub fn reverse(&mut self) {
        self.items.reverse();
        if !self.items.is_empty() {
            self.current_index = 0;
        }
    }

    /// Returns a reference to the items slice.
    pub fn items(&self) -> &[PlaylistItem] {
        &self.items
    }

    /// Finds items matching a title substring.
    pub fn find_by_title(&self, query: &str) -> Vec<usize> {
        let query_lower = query.to_lowercase();
        self.items
            .iter()
            .enumerate()
            .filter(|(_, item)| item.title.to_lowercase().contains(&query_lower))
            .map(|(i, _)| i)
            .collect()
    }

    /// Clears all items and resets playback.
    pub fn clear(&mut self) {
        self.items.clear();
        self.current_index = 0;
    }
}

/// A collection of playlists.
#[derive(Debug)]
pub struct PlaylistCollection {
    /// All playlists in the collection.
    playlists: Vec<Playlist>,
    /// Next ID to assign.
    next_id: PlaylistId,
}

impl PlaylistCollection {
    /// Creates a new empty collection.
    pub fn new() -> Self {
        Self {
            playlists: Vec::new(),
            next_id: 1,
        }
    }

    /// Creates a new playlist and returns its ID.
    pub fn create_playlist(&mut self, name: &str) -> PlaylistId {
        let id = self.next_id;
        self.next_id += 1;
        self.playlists.push(Playlist::new(id, name));
        id
    }

    /// Returns a reference to a playlist by ID.
    pub fn get(&self, id: PlaylistId) -> Option<&Playlist> {
        self.playlists.iter().find(|p| p.id == id)
    }

    /// Returns a mutable reference to a playlist by ID.
    pub fn get_mut(&mut self, id: PlaylistId) -> Option<&mut Playlist> {
        self.playlists.iter_mut().find(|p| p.id == id)
    }

    /// Returns the number of playlists.
    pub fn count(&self) -> usize {
        self.playlists.len()
    }
}

impl Default for PlaylistCollection {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_playlist_item_new() {
        let item = PlaylistItem::new("c1", "Intro", 5000);
        assert_eq!(item.clip_id, "c1");
        assert_eq!(item.title, "Intro");
        assert_eq!(item.duration_ms, 5000);
    }

    #[test]
    fn test_playlist_item_effective_duration() {
        let item = PlaylistItem::new("c1", "Full", 10000);
        assert_eq!(item.effective_duration(), 10000);

        let ranged = PlaylistItem::with_range("c2", "Partial", 10000, 2000, 7000);
        assert_eq!(ranged.effective_duration(), 5000);
    }

    #[test]
    fn test_playlist_item_effective_duration_with_start_only() {
        let mut item = PlaylistItem::new("c1", "Trimmed", 10000);
        item.start_offset_ms = 3000;
        assert_eq!(item.effective_duration(), 7000);
    }

    #[test]
    fn test_playlist_new() {
        let pl = Playlist::new(1, "My Playlist");
        assert_eq!(pl.id, 1);
        assert_eq!(pl.name, "My Playlist");
        assert!(pl.is_empty());
        assert_eq!(pl.len(), 0);
    }

    #[test]
    fn test_playlist_push_and_len() {
        let mut pl = Playlist::new(1, "Test");
        pl.push(PlaylistItem::new("c1", "A", 1000));
        pl.push(PlaylistItem::new("c2", "B", 2000));
        assert_eq!(pl.len(), 2);
        assert!(!pl.is_empty());
    }

    #[test]
    fn test_playlist_total_duration() {
        let mut pl = Playlist::new(1, "Test");
        pl.push(PlaylistItem::new("c1", "A", 3000));
        pl.push(PlaylistItem::new("c2", "B", 4000));
        assert_eq!(pl.total_duration_ms(), 7000);
    }

    #[test]
    fn test_playlist_navigation_none() {
        let mut pl = Playlist::new(1, "Nav");
        pl.push(PlaylistItem::new("c1", "A", 1000));
        pl.push(PlaylistItem::new("c2", "B", 1000));
        pl.push(PlaylistItem::new("c3", "C", 1000));

        assert_eq!(pl.current_index(), 0);
        assert!(pl.next());
        assert_eq!(pl.current_index(), 1);
        assert!(pl.next());
        assert_eq!(pl.current_index(), 2);
        assert!(!pl.next()); // end
    }

    #[test]
    fn test_playlist_navigation_repeat_all() {
        let mut pl = Playlist::new(1, "Rep");
        pl.set_repeat_mode(RepeatMode::RepeatAll);
        pl.push(PlaylistItem::new("c1", "A", 1000));
        pl.push(PlaylistItem::new("c2", "B", 1000));

        assert!(pl.next()); // 0->1
        assert!(pl.next()); // 1->0 (wrap)
        assert_eq!(pl.current_index(), 0);
    }

    #[test]
    fn test_playlist_navigation_repeat_one() {
        let mut pl = Playlist::new(1, "One");
        pl.set_repeat_mode(RepeatMode::RepeatOne);
        pl.push(PlaylistItem::new("c1", "A", 1000));
        assert!(pl.next());
        assert_eq!(pl.current_index(), 0); // stays
    }

    #[test]
    fn test_playlist_previous() {
        let mut pl = Playlist::new(1, "Prev");
        pl.push(PlaylistItem::new("c1", "A", 1000));
        pl.push(PlaylistItem::new("c2", "B", 1000));
        pl.jump_to(1);
        assert!(pl.previous());
        assert_eq!(pl.current_index(), 0);
        assert!(!pl.previous()); // can't go before 0
    }

    #[test]
    fn test_playlist_remove() {
        let mut pl = Playlist::new(1, "Del");
        pl.push(PlaylistItem::new("c1", "A", 1000));
        pl.push(PlaylistItem::new("c2", "B", 1000));
        let removed = pl.remove(0);
        assert!(removed.is_some());
        assert_eq!(removed.expect("value should be valid").clip_id, "c1");
        assert_eq!(pl.len(), 1);
    }

    #[test]
    fn test_playlist_move_item() {
        let mut pl = Playlist::new(1, "Mv");
        pl.push(PlaylistItem::new("c1", "A", 1000));
        pl.push(PlaylistItem::new("c2", "B", 1000));
        pl.push(PlaylistItem::new("c3", "C", 1000));
        assert!(pl.move_item(0, 2));
        assert_eq!(pl.items()[0].clip_id, "c2");
        assert_eq!(pl.items()[2].clip_id, "c1");
    }

    #[test]
    fn test_playlist_find_by_title() {
        let mut pl = Playlist::new(1, "Search");
        pl.push(PlaylistItem::new("c1", "Interview Part 1", 1000));
        pl.push(PlaylistItem::new("c2", "B-Roll", 1000));
        pl.push(PlaylistItem::new("c3", "Interview Part 2", 1000));
        let matches = pl.find_by_title("interview");
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0], 0);
        assert_eq!(matches[1], 2);
    }

    #[test]
    fn test_playlist_collection() {
        let mut col = PlaylistCollection::new();
        let id1 = col.create_playlist("Favorites");
        let id2 = col.create_playlist("Recent");
        assert_eq!(col.count(), 2);
        assert!(col.get(id1).is_some());
        assert!(col.get(id2).is_some());
    }

    #[test]
    fn test_playlist_clear() {
        let mut pl = Playlist::new(1, "Clr");
        pl.push(PlaylistItem::new("c1", "A", 1000));
        pl.push(PlaylistItem::new("c2", "B", 2000));
        pl.clear();
        assert!(pl.is_empty());
        assert_eq!(pl.current_index(), 0);
    }
}