subx_cli/core/formats/
mod.rs

1//! Core subtitle formats 模組
2#![allow(dead_code)]
3
4pub mod ass;
5pub mod converter;
6pub mod encoding;
7pub mod manager;
8pub mod srt;
9pub mod styling;
10pub mod sub;
11pub mod transformers;
12pub mod vtt;
13
14use std::time::Duration;
15
16/// 支援的字幕格式類型
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum SubtitleFormatType {
19    Srt,
20    Ass,
21    Vtt,
22    Sub,
23}
24
25impl SubtitleFormatType {
26    /// 取得格式對應字串
27    pub fn as_str(&self) -> &'static str {
28        match self {
29            SubtitleFormatType::Srt => "srt",
30            SubtitleFormatType::Ass => "ass",
31            SubtitleFormatType::Vtt => "vtt",
32            SubtitleFormatType::Sub => "sub",
33        }
34    }
35}
36
37impl std::fmt::Display for SubtitleFormatType {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        write!(f, "{}", self.as_str())
40    }
41}
42
43/// 統一字幕資料結構
44#[derive(Debug, Clone)]
45pub struct Subtitle {
46    pub entries: Vec<SubtitleEntry>,
47    pub metadata: SubtitleMetadata,
48    pub format: SubtitleFormatType,
49}
50
51/// 單條字幕項目
52#[derive(Debug, Clone)]
53pub struct SubtitleEntry {
54    pub index: usize,
55    pub start_time: Duration,
56    pub end_time: Duration,
57    pub text: String,
58    pub styling: Option<StylingInfo>,
59}
60
61/// 字幕元資料
62#[derive(Debug, Clone)]
63pub struct SubtitleMetadata {
64    pub title: Option<String>,
65    pub language: Option<String>,
66    pub encoding: String,
67    pub frame_rate: Option<f32>,
68    pub original_format: SubtitleFormatType,
69}
70
71/// 樣式資訊
72#[derive(Debug, Clone, Default)]
73pub struct StylingInfo {
74    pub font_name: Option<String>,
75    pub font_size: Option<u32>,
76    pub color: Option<String>,
77    pub bold: bool,
78    pub italic: bool,
79    pub underline: bool,
80}
81
82/// 字幕格式 Trait 定義
83pub trait SubtitleFormat {
84    /// 解析字幕內容
85    fn parse(&self, content: &str) -> crate::Result<Subtitle>;
86
87    /// 序列化為字幕格式
88    fn serialize(&self, subtitle: &Subtitle) -> crate::Result<String>;
89
90    /// 檢測是否為此格式
91    fn detect(&self, content: &str) -> bool;
92
93    /// 格式名稱
94    fn format_name(&self) -> &'static str;
95
96    /// 支援的副檔名
97    fn file_extensions(&self) -> &'static [&'static str];
98}