captionrs/processors/
base.rs1use 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 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 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 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 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 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 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 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 async fn process_async(
88 &self,
89 srt: SubRipFile,
90 language: Option<&str>,
91 ) -> Result<(SubRipFile, bool), SubtitleError>;
92}