Skip to main content

ass_editor/utils/formats/
converter.rs

1//! Format converter entry points.
2//!
3//! Hosts the [`FormatConverter`] type and its public `import`/`export`
4//! dispatchers, which delegate to the per-format helpers in sibling modules.
5
6use super::types::{ConversionOptions, SubtitleFormat};
7use crate::core::{EditorDocument, Result};
8
9#[cfg(not(feature = "std"))]
10use alloc::string::{String, ToString};
11
12/// Format converter for subtitle import/export
13pub struct FormatConverter;
14
15impl FormatConverter {
16    /// Import subtitle content from various formats into ASS
17    pub fn import(content: &str, format: Option<SubtitleFormat>) -> Result<String> {
18        let detected_format = format.unwrap_or_else(|| SubtitleFormat::from_content(content));
19
20        match detected_format {
21            SubtitleFormat::ASS | SubtitleFormat::SSA => {
22                // Already in ASS/SSA format, just return
23                Ok(content.to_string())
24            }
25            SubtitleFormat::SRT => Self::import_srt(content),
26            SubtitleFormat::WebVTT => Self::import_webvtt(content),
27            SubtitleFormat::PlainText => Self::import_plain_text(content),
28        }
29    }
30
31    /// Export ASS content to another subtitle format
32    pub fn export(
33        document: &EditorDocument,
34        format: SubtitleFormat,
35        options: &ConversionOptions,
36    ) -> Result<String> {
37        match format {
38            SubtitleFormat::ASS => Ok(document.text()),
39            SubtitleFormat::SSA => Self::export_ssa(document, options),
40            SubtitleFormat::SRT => Self::export_srt(document, options),
41            SubtitleFormat::WebVTT => Self::export_webvtt(document, options),
42            SubtitleFormat::PlainText => Self::export_plain_text(document, options),
43        }
44    }
45}