oximedia-cli 0.1.6

Command-line interface 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
//! Clip extraction, management, and organization CLI commands.
//!
//! Provides commands for creating, listing, exporting, trimming, merging,
//! and tagging video clips for professional logging workflows.

use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

// ---------------------------------------------------------------------------
// Command definitions
// ---------------------------------------------------------------------------

/// Clips command subcommands.
#[derive(Subcommand, Debug)]
pub enum ClipsCommand {
    /// Create a new clip from a source media file
    Create {
        /// Source media file
        #[arg(short, long)]
        input: PathBuf,

        /// Clip name
        #[arg(short, long)]
        name: String,

        /// In-point timecode (e.g., 00:01:30:00)
        #[arg(long)]
        tc_in: Option<String>,

        /// Out-point timecode (e.g., 00:02:00:00)
        #[arg(long)]
        tc_out: Option<String>,

        /// Rating: 0-5 stars
        #[arg(long)]
        rating: Option<u8>,

        /// Comma-separated keywords
        #[arg(long)]
        keywords: Option<String>,

        /// Clip database path (JSON file)
        #[arg(long)]
        db: PathBuf,
    },

    /// List clips in the database
    List {
        /// Clip database path (JSON file)
        #[arg(long)]
        db: PathBuf,

        /// Filter by keyword
        #[arg(long)]
        keyword: Option<String>,

        /// Filter by minimum rating (0-5)
        #[arg(long)]
        min_rating: Option<u8>,

        /// Sort by: name, rating, date
        #[arg(long, default_value = "name")]
        sort: String,

        /// Maximum results
        #[arg(long)]
        limit: Option<u32>,
    },

    /// Export clips to a file
    Export {
        /// Clip database path (JSON file)
        #[arg(long)]
        db: PathBuf,

        /// Output file path
        #[arg(short, long)]
        output: PathBuf,

        /// Export format: json, csv, edl
        #[arg(long, default_value = "json")]
        format: String,

        /// Filter by keyword
        #[arg(long)]
        keyword: Option<String>,
    },

    /// Trim an existing clip's in/out points
    Trim {
        /// Clip ID to trim
        #[arg(short, long)]
        clip_id: String,

        /// New in-point timecode
        #[arg(long)]
        tc_in: Option<String>,

        /// New out-point timecode
        #[arg(long)]
        tc_out: Option<String>,

        /// Clip database path (JSON file)
        #[arg(long)]
        db: PathBuf,
    },

    /// Merge multiple clips into a single sequence
    Merge {
        /// Comma-separated clip IDs to merge
        #[arg(short, long)]
        clip_ids: String,

        /// Merged clip name
        #[arg(short, long)]
        name: String,

        /// Clip database path (JSON file)
        #[arg(long)]
        db: PathBuf,
    },

    /// Add or remove tags on a clip
    Tag {
        /// Clip ID
        #[arg(short, long)]
        clip_id: String,

        /// Comma-separated keywords to add
        #[arg(long)]
        add: Option<String>,

        /// Comma-separated keywords to remove
        #[arg(long)]
        remove: Option<String>,

        /// Set rating (0-5)
        #[arg(long)]
        rating: Option<u8>,

        /// Clip database path (JSON file)
        #[arg(long)]
        db: PathBuf,
    },
}

// ---------------------------------------------------------------------------
// Data model
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ClipRecord {
    id: String,
    name: String,
    source_path: String,
    tc_in: Option<String>,
    tc_out: Option<String>,
    rating: u8,
    keywords: Vec<String>,
    created_at: String,
    notes: String,
    merged_from: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
struct ClipDb {
    version: u32,
    clips: Vec<ClipRecord>,
}

// ---------------------------------------------------------------------------
// Persistence helpers
// ---------------------------------------------------------------------------

fn load_db(path: &PathBuf) -> Result<ClipDb> {
    if !path.exists() {
        return Ok(ClipDb {
            version: 1,
            ..ClipDb::default()
        });
    }
    let data = std::fs::read_to_string(path).context("Failed to read clip database")?;
    let db: ClipDb = serde_json::from_str(&data).context("Failed to parse clip database")?;
    Ok(db)
}

fn save_db(path: &PathBuf, db: &ClipDb) -> Result<()> {
    if let Some(parent) = path.parent() {
        if !parent.exists() {
            std::fs::create_dir_all(parent).context("Failed to create database directory")?;
        }
    }
    let data = serde_json::to_string_pretty(db).context("Failed to serialize clip database")?;
    std::fs::write(path, data).context("Failed to write clip database")?;
    Ok(())
}

fn generate_id() -> String {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default();
    format!("clip-{:016x}", now.as_nanos())
}

fn now_timestamp() -> String {
    let dur = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default();
    format!("{}", dur.as_secs())
}

fn parse_keywords(kw: &Option<String>) -> Vec<String> {
    kw.as_ref()
        .map(|k| {
            k.split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect()
        })
        .unwrap_or_default()
}

// ---------------------------------------------------------------------------
// Command handler
// ---------------------------------------------------------------------------

/// Handle clips command dispatch.
pub async fn handle_clips_command(command: ClipsCommand, json_output: bool) -> Result<()> {
    match command {
        ClipsCommand::Create {
            input,
            name,
            tc_in,
            tc_out,
            rating,
            keywords,
            db,
        } => {
            run_create(
                &input,
                &name,
                &tc_in,
                &tc_out,
                rating,
                &keywords,
                &db,
                json_output,
            )
            .await
        }
        ClipsCommand::List {
            db,
            keyword,
            min_rating,
            sort,
            limit,
        } => run_list(&db, &keyword, min_rating, &sort, limit, json_output).await,
        ClipsCommand::Export {
            db,
            output,
            format,
            keyword,
        } => run_export(&db, &output, &format, &keyword, json_output).await,
        ClipsCommand::Trim {
            clip_id,
            tc_in,
            tc_out,
            db,
        } => run_trim(&clip_id, &tc_in, &tc_out, &db, json_output).await,
        ClipsCommand::Merge { clip_ids, name, db } => {
            run_merge(&clip_ids, &name, &db, json_output).await
        }
        ClipsCommand::Tag {
            clip_id,
            add,
            remove,
            rating,
            db,
        } => run_tag(&clip_id, &add, &remove, rating, &db, json_output).await,
    }
}

// ---------------------------------------------------------------------------
// Create
// ---------------------------------------------------------------------------

async fn run_create(
    input: &PathBuf,
    name: &str,
    tc_in: &Option<String>,
    tc_out: &Option<String>,
    rating: Option<u8>,
    keywords: &Option<String>,
    db_path: &PathBuf,
    json_output: bool,
) -> Result<()> {
    let mut db = load_db(db_path)?;

    if !input.exists() {
        return Err(anyhow::anyhow!(
            "Source file not found: {}",
            input.display()
        ));
    }

    let clip_id = generate_id();
    let kw = parse_keywords(keywords);
    let r = rating.unwrap_or(0).min(5);

    let clip = ClipRecord {
        id: clip_id.clone(),
        name: name.to_string(),
        source_path: input.to_string_lossy().to_string(),
        tc_in: tc_in.clone(),
        tc_out: tc_out.clone(),
        rating: r,
        keywords: kw.clone(),
        created_at: now_timestamp(),
        notes: String::new(),
        merged_from: Vec::new(),
    };

    db.clips.push(clip);
    save_db(db_path, &db)?;

    if json_output {
        let result = serde_json::json!({
            "command": "clips_create",
            "clip_id": clip_id,
            "name": name,
            "source": input.display().to_string(),
            "tc_in": tc_in,
            "tc_out": tc_out,
            "rating": r,
            "keywords": kw,
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Clip Created".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Clip ID:", clip_id);
        println!("{:20} {}", "Name:", name);
        println!("{:20} {}", "Source:", input.display());
        if let Some(ref tc) = tc_in {
            println!("{:20} {}", "TC In:", tc);
        }
        if let Some(ref tc) = tc_out {
            println!("{:20} {}", "TC Out:", tc);
        }
        println!("{:20} {}", "Rating:", r);
        if !kw.is_empty() {
            println!("{:20} {}", "Keywords:", kw.join(", "));
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// List
// ---------------------------------------------------------------------------

async fn run_list(
    db_path: &PathBuf,
    keyword: &Option<String>,
    min_rating: Option<u8>,
    sort: &str,
    limit: Option<u32>,
    json_output: bool,
) -> Result<()> {
    let db = load_db(db_path)?;
    let max_results = limit.unwrap_or(100) as usize;

    let mut clips: Vec<&ClipRecord> = db
        .clips
        .iter()
        .filter(|c| {
            let kw_ok = keyword.as_ref().map_or(true, |kw| {
                let kwl = kw.to_lowercase();
                c.keywords.iter().any(|k| k.to_lowercase().contains(&kwl))
                    || c.name.to_lowercase().contains(&kwl)
            });
            let rating_ok = min_rating.map_or(true, |mr| c.rating >= mr);
            kw_ok && rating_ok
        })
        .collect();

    match sort {
        "name" => clips.sort_by(|a, b| a.name.cmp(&b.name)),
        "rating" => clips.sort_by(|a, b| b.rating.cmp(&a.rating)),
        "date" => clips.sort_by(|a, b| b.created_at.cmp(&a.created_at)),
        _ => {}
    }

    let total = clips.len();
    clips.truncate(max_results);

    if json_output {
        let result = serde_json::json!({
            "command": "clips_list",
            "total": total,
            "returned": clips.len(),
            "clips": clips.iter().map(|c| serde_json::json!({
                "id": c.id,
                "name": c.name,
                "source": c.source_path,
                "tc_in": c.tc_in,
                "tc_out": c.tc_out,
                "rating": c.rating,
                "keywords": c.keywords,
            })).collect::<Vec<_>>(),
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Clip List".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {} (showing {})", "Total clips:", total, clips.len());
        println!();
        for c in &clips {
            let stars = "*".repeat(c.rating as usize);
            println!(
                "  {} {} [{}] {}",
                ">".cyan(),
                c.name,
                stars,
                c.source_path.dimmed()
            );
            if let (Some(ref ti), Some(ref to)) = (&c.tc_in, &c.tc_out) {
                println!("    Range: {} - {}", ti, to);
            }
            if !c.keywords.is_empty() {
                println!("    Keywords: {}", c.keywords.join(", ").dimmed());
            }
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Export
// ---------------------------------------------------------------------------

async fn run_export(
    db_path: &PathBuf,
    output: &PathBuf,
    format: &str,
    keyword: &Option<String>,
    json_output: bool,
) -> Result<()> {
    let db = load_db(db_path)?;

    let clips: Vec<&ClipRecord> = db
        .clips
        .iter()
        .filter(|c| {
            keyword.as_ref().map_or(true, |kw| {
                let kwl = kw.to_lowercase();
                c.keywords.iter().any(|k| k.to_lowercase().contains(&kwl))
            })
        })
        .collect();

    let export_data = match format {
        "json" => {
            let data: Vec<serde_json::Value> = clips
                .iter()
                .map(|c| {
                    serde_json::json!({
                        "id": c.id,
                        "name": c.name,
                        "source": c.source_path,
                        "tc_in": c.tc_in,
                        "tc_out": c.tc_out,
                        "rating": c.rating,
                        "keywords": c.keywords,
                    })
                })
                .collect();
            serde_json::to_string_pretty(&data).context("Failed to serialize export")?
        }
        "csv" => {
            let mut csv = String::from("id,name,source,tc_in,tc_out,rating,keywords\n");
            for c in &clips {
                csv.push_str(&format!(
                    "{},{},{},{},{},{},{}\n",
                    c.id,
                    c.name.replace(',', ";"),
                    c.source_path.replace(',', ";"),
                    c.tc_in.as_deref().unwrap_or(""),
                    c.tc_out.as_deref().unwrap_or(""),
                    c.rating,
                    c.keywords.join(";")
                ));
            }
            csv
        }
        "edl" => {
            let mut edl = String::from("TITLE: OxiMedia Clip Export\n\n");
            for (i, c) in clips.iter().enumerate() {
                let tc_in = c.tc_in.as_deref().unwrap_or("00:00:00:00");
                let tc_out = c.tc_out.as_deref().unwrap_or("00:00:00:00");
                edl.push_str(&format!(
                    "{:03}  {} V     C        {} {} {} {}\n",
                    i + 1,
                    c.name.replace(' ', "_"),
                    tc_in,
                    tc_out,
                    tc_in,
                    tc_out
                ));
            }
            edl
        }
        _ => return Err(anyhow::anyhow!("Unsupported export format: {format}")),
    };

    if let Some(parent) = output.parent() {
        if !parent.exists() {
            std::fs::create_dir_all(parent).context("Failed to create output directory")?;
        }
    }
    std::fs::write(output, &export_data).context("Failed to write export file")?;

    if json_output {
        let result = serde_json::json!({
            "command": "clips_export",
            "output": output.display().to_string(),
            "format": format,
            "clip_count": clips.len(),
            "size_bytes": export_data.len(),
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Clips Exported".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Output:", output.display());
        println!("{:20} {}", "Format:", format);
        println!("{:20} {}", "Clips:", clips.len());
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Trim
// ---------------------------------------------------------------------------

async fn run_trim(
    clip_id: &str,
    tc_in: &Option<String>,
    tc_out: &Option<String>,
    db_path: &PathBuf,
    json_output: bool,
) -> Result<()> {
    let mut db = load_db(db_path)?;

    let clip = db
        .clips
        .iter_mut()
        .find(|c| c.id == clip_id)
        .ok_or_else(|| anyhow::anyhow!("Clip not found: {clip_id}"))?;

    if let Some(ref tc) = tc_in {
        clip.tc_in = Some(tc.clone());
    }
    if let Some(ref tc) = tc_out {
        clip.tc_out = Some(tc.clone());
    }

    let saved_tc_in = clip.tc_in.clone();
    let saved_tc_out = clip.tc_out.clone();

    save_db(db_path, &db)?;

    if json_output {
        let result = serde_json::json!({
            "command": "clips_trim",
            "clip_id": clip_id,
            "tc_in": saved_tc_in,
            "tc_out": saved_tc_out,
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Clip Trimmed".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Clip ID:", clip_id);
        if let Some(ref tc) = saved_tc_in {
            println!("{:20} {}", "TC In:", tc);
        }
        if let Some(ref tc) = saved_tc_out {
            println!("{:20} {}", "TC Out:", tc);
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Merge
// ---------------------------------------------------------------------------

async fn run_merge(
    clip_ids_str: &str,
    name: &str,
    db_path: &PathBuf,
    json_output: bool,
) -> Result<()> {
    let mut db = load_db(db_path)?;
    let ids: Vec<String> = clip_ids_str
        .split(',')
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();

    if ids.len() < 2 {
        return Err(anyhow::anyhow!("Need at least 2 clip IDs to merge"));
    }

    // Verify all clips exist and collect source
    let mut source_path = String::new();
    for id in &ids {
        let clip = db
            .clips
            .iter()
            .find(|c| c.id == *id)
            .ok_or_else(|| anyhow::anyhow!("Clip not found: {id}"))?;
        if source_path.is_empty() {
            source_path.clone_from(&clip.source_path);
        }
    }

    let merged_id = generate_id();
    let merged = ClipRecord {
        id: merged_id.clone(),
        name: name.to_string(),
        source_path,
        tc_in: None,
        tc_out: None,
        rating: 0,
        keywords: vec!["merged".to_string()],
        created_at: now_timestamp(),
        notes: format!("Merged from: {}", ids.join(", ")),
        merged_from: ids.clone(),
    };

    db.clips.push(merged);
    save_db(db_path, &db)?;

    if json_output {
        let result = serde_json::json!({
            "command": "clips_merge",
            "merged_id": merged_id,
            "name": name,
            "source_clips": ids,
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Clips Merged".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Merged ID:", merged_id);
        println!("{:20} {}", "Name:", name);
        println!("{:20} {}", "Source clips:", ids.join(", "));
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tag
// ---------------------------------------------------------------------------

async fn run_tag(
    clip_id: &str,
    add: &Option<String>,
    remove: &Option<String>,
    rating: Option<u8>,
    db_path: &PathBuf,
    json_output: bool,
) -> Result<()> {
    let mut db = load_db(db_path)?;

    let clip = db
        .clips
        .iter_mut()
        .find(|c| c.id == clip_id)
        .ok_or_else(|| anyhow::anyhow!("Clip not found: {clip_id}"))?;

    let to_add = parse_keywords(add);
    let to_remove = parse_keywords(remove);

    for kw in &to_add {
        if !clip.keywords.contains(kw) {
            clip.keywords.push(kw.clone());
        }
    }
    clip.keywords.retain(|k| !to_remove.contains(k));

    if let Some(r) = rating {
        clip.rating = r.min(5);
    }

    let saved_keywords = clip.keywords.clone();
    let saved_rating = clip.rating;

    save_db(db_path, &db)?;

    if json_output {
        let result = serde_json::json!({
            "command": "clips_tag",
            "clip_id": clip_id,
            "keywords": saved_keywords,
            "rating": saved_rating,
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Clip Tagged".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Clip ID:", clip_id);
        println!("{:20} {}", "Keywords:", saved_keywords.join(", "));
        println!("{:20} {}", "Rating:", saved_rating);
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_generate_id() {
        let id = generate_id();
        assert!(id.starts_with("clip-"));
    }

    #[test]
    fn test_parse_keywords() {
        let kw = Some("interview, raw, john-doe".to_string());
        let result = parse_keywords(&kw);
        assert_eq!(result, vec!["interview", "raw", "john-doe"]);
    }

    #[test]
    fn test_parse_keywords_none() {
        let result = parse_keywords(&None);
        assert!(result.is_empty());
    }

    #[test]
    fn test_clip_record_serialization() {
        let clip = ClipRecord {
            id: "clip-001".to_string(),
            name: "Take 1".to_string(),
            source_path: "/tmp/video.mov".to_string(),
            tc_in: Some("01:00:00:00".to_string()),
            tc_out: Some("01:00:30:00".to_string()),
            rating: 4,
            keywords: vec!["interview".to_string()],
            created_at: "12345".to_string(),
            notes: String::new(),
            merged_from: Vec::new(),
        };
        let json = serde_json::to_string(&clip);
        assert!(json.is_ok());
        let parsed: Result<ClipRecord, _> = serde_json::from_str(&json.expect("should serialize"));
        assert!(parsed.is_ok());
        let p = parsed.expect("should deserialize");
        assert_eq!(p.rating, 4);
    }

    #[test]
    fn test_db_roundtrip() {
        let db = ClipDb {
            version: 1,
            clips: vec![ClipRecord {
                id: "c1".to_string(),
                name: "Test".to_string(),
                source_path: "/tmp/a.mov".to_string(),
                tc_in: None,
                tc_out: None,
                rating: 3,
                keywords: vec!["raw".to_string()],
                created_at: "0".to_string(),
                notes: String::new(),
                merged_from: Vec::new(),
            }],
        };
        let json = serde_json::to_string(&db).expect("serialize");
        let parsed: ClipDb = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(parsed.clips.len(), 1);
    }
}