rsynko_media/
processing.rs1use crate::ProcessingSorts;
2use alux_ext::ext;
3use ambassador::delegatable_trait;
4
5#[delegatable_trait]
7pub trait ProcessingProgramAlg: ProcessingSorts {
8 fn processor(&self, id: impl Into<String>) -> Self::Processor;
10 fn processing_step(&self, stage: ProcessingStage, processor: Self::Processor) -> Self::Step;
12 fn empty_processing(&self) -> Self::Program;
14 fn processing(&self, steps: impl IntoIterator<Item = Self::Step>) -> Self::Program;
16 fn then_processing(&self, first: Self::Program, next: Self::Program) -> Self::Program;
18}
19
20pub trait ProcessingProgramViewAlg: ProcessingSorts {
22 fn processing_steps<'a>(program: &'a Self::Program) -> impl Iterator<Item = &'a Self::Step>
24 where
25 Self::Step: 'a;
26 fn processing_stage(step: &Self::Step) -> ProcessingStage;
28}
29
30pub trait ProcessingApplyAlg: ProcessingSorts {
32 type Error;
34
35 fn apply_processing_step(&mut self, step: &Self::Step) -> Result<(), Self::Error>;
41}
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
45pub enum ProcessingStage {
46 PreProcess,
48 AfterFilter,
50 Video,
52 BeforeDownload,
54 PostProcess,
56 AfterMove,
58 AfterVideo,
60 Playlist,
62}
63
64#[ext(name = ProcessingProgramExt)]
66pub impl<This> This
67where
68 This: ProcessingProgramAlg,
69{
70 fn process_with(&self, stage: ProcessingStage, processor: impl Into<String>) -> This::Program {
72 let processor = self.processor(processor);
73 let step = self.processing_step(stage, processor);
74 self.processing([step])
75 }
76}
77
78#[ext(name = ProcessingExt)]
80pub impl<This, Step, Program> This
81where
82 This: ProcessingApplyAlg<Step = Step> + ProcessingProgramViewAlg<Step = Step, Program = Program>,
83{
84 fn run_processing_program(&mut self, program: &Program) -> Result<(), This::Error> {
90 This::processing_steps(program).try_for_each(|step| self.apply_processing_step(step))
91 }
92
93 fn run_processing_stage(&mut self, program: &Program, stage: ProcessingStage) -> Result<(), This::Error> {
99 This::processing_steps(program)
100 .filter(|step| This::processing_stage(step) == stage)
101 .try_for_each(|step| self.apply_processing_step(step))
102 }
103}