ass_editor/utils/formats/
converter.rs1use super::types::{ConversionOptions, SubtitleFormat};
7use crate::core::{EditorDocument, Result};
8
9#[cfg(not(feature = "std"))]
10use alloc::string::{String, ToString};
11
12pub struct FormatConverter;
14
15impl FormatConverter {
16 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 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 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}