Skip to main content

captionrs/processors/
base.rs

1use std::fs;
2use std::path::Path;
3
4use crate::subripfile::{SubRipFile, SubtitleError};
5
6#[cfg(feature = "async")]
7use tokio::fs as async_fs;
8
9#[allow(clippy::wrong_self_convention)]
10pub trait BaseProcessor {
11    /// Applies subtitle cleanup or transformation passes to [`SubRipFile`] values.
12    fn from_srt(
13        &self,
14        srt: SubRipFile,
15        language: Option<&str>,
16    ) -> Result<(SubRipFile, bool), SubtitleError> {
17        self.process(srt, language)
18    }
19
20    /// Reads an SRT file and processes it synchronously.
21    fn from_file<P: AsRef<Path>>(
22        &self,
23        file: P,
24        language: Option<&str>,
25    ) -> Result<(SubRipFile, bool), SubtitleError> {
26        let content = fs::read_to_string(file)?;
27        self.from_string(&content, language)
28    }
29
30    /// Parses an SRT string and processes it synchronously.
31    fn from_string(
32        &self,
33        data: &str,
34        language: Option<&str>,
35    ) -> Result<(SubRipFile, bool), SubtitleError> {
36        let srt = SubRipFile::from_string(data)?;
37        self.process(srt, language)
38    }
39
40    /// Processes a [`SubRipFile`] and reports whether the output changed.
41    fn process(
42        &self,
43        srt: SubRipFile,
44        language: Option<&str>,
45    ) -> Result<(SubRipFile, bool), SubtitleError>;
46}
47
48#[cfg(feature = "async")]
49#[async_trait::async_trait]
50#[allow(clippy::wrong_self_convention)]
51pub trait AsyncBaseProcessor: Send + Sync {
52    /// Applies subtitle cleanup or transformation passes in async applications.
53    ///
54    /// The async helpers perform file I/O asynchronously and move CPU-heavy parsing or
55    /// processing off the runtime thread when necessary.
56    async fn from_srt_async(
57        &self,
58        srt: SubRipFile,
59        language: Option<&str>,
60    ) -> Result<(SubRipFile, bool), SubtitleError> {
61        self.process_async(srt, language).await
62    }
63
64    /// Reads an SRT file asynchronously and processes it.
65    async fn from_file_async<P: AsRef<Path> + Send>(
66        &self,
67        file: P,
68        language: Option<&str>,
69    ) -> Result<(SubRipFile, bool), SubtitleError> {
70        let content = async_fs::read_to_string(file).await?;
71        self.from_string_async(&content, language).await
72    }
73
74    /// Parses an SRT string and processes it in an async context.
75    async fn from_string_async(
76        &self,
77        data: &str,
78        language: Option<&str>,
79    ) -> Result<(SubRipFile, bool), SubtitleError> {
80        let owned_data = data.to_string();
81        let srt =
82            crate::async_utils::run_blocking(move || SubRipFile::from_string(&owned_data)).await?;
83        self.process_async(srt, language).await
84    }
85
86    /// Processes a [`SubRipFile`] and reports whether the output changed.
87    async fn process_async(
88        &self,
89        srt: SubRipFile,
90        language: Option<&str>,
91    ) -> Result<(SubRipFile, bool), SubtitleError>;
92}