moxcms/conversions/katana/
stages.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 6/2025. All rights reserved.
3 * //
4 * // Redistribution and use in source and binary forms, with or without modification,
5 * // are permitted provided that the following conditions are met:
6 * //
7 * // 1.  Redistributions of source code must retain the above copyright notice, this
8 * // list of conditions and the following disclaimer.
9 * //
10 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
11 * // this list of conditions and the following disclaimer in the documentation
12 * // and/or other materials provided with the distribution.
13 * //
14 * // 3.  Neither the name of the copyright holder nor the names of its
15 * // contributors may be used to endorse or promote products derived from
16 * // this software without specific prior written permission.
17 * //
18 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29use crate::{CmsError, TransformExecutor};
30use std::marker::PhantomData;
31
32/// W storage working data type
33/// I input/output data type
34pub(crate) trait KatanaInitialStage<W, I> {
35    fn to_pcs(&self, input: &[I]) -> Result<Vec<W>, CmsError>;
36}
37
38/// W storage working data type
39/// I input/output data type
40pub(crate) trait KatanaFinalStage<W, I> {
41    fn to_output(&self, src: &mut [W], dst: &mut [I]) -> Result<(), CmsError>;
42}
43
44/// W storage working data type
45pub(crate) trait KatanaIntermediateStage<W> {
46    fn stage(&self, input: &mut Vec<W>) -> Result<Vec<W>, CmsError>;
47}
48
49pub(crate) struct BlackholeIntermediateStage<W> {
50    pub(crate) _phantom: PhantomData<W>,
51}
52
53impl<W> KatanaIntermediateStage<W> for BlackholeIntermediateStage<W> {
54    fn stage(&self, input: &mut Vec<W>) -> Result<Vec<W>, CmsError> {
55        Ok(std::mem::take(input))
56    }
57}
58
59/// I input/output data type
60pub(crate) trait KatanaPostFinalizationStage<I> {
61    fn finalize(&self, src: &[I], dst: &mut [I]) -> Result<(), CmsError>;
62}
63
64/// W storage working data type
65/// I input/output data type
66pub(crate) struct Katana<W, I> {
67    pub(crate) initial_stage: Box<dyn KatanaInitialStage<W, I> + Send + Sync>,
68    pub(crate) final_stage: Box<dyn KatanaFinalStage<W, I> + Sync + Send>,
69    pub(crate) stages: Vec<Box<dyn KatanaIntermediateStage<W> + Send + Sync>>,
70    pub(crate) post_finalization: Vec<Box<dyn KatanaPostFinalizationStage<I> + Send + Sync>>,
71}
72
73impl<W, I: Copy + Default> TransformExecutor<I> for Katana<W, I> {
74    fn transform(&self, src: &[I], dst: &mut [I]) -> Result<(), CmsError> {
75        let mut working_vec = self.initial_stage.to_pcs(src)?;
76        for stage in self.stages.iter() {
77            working_vec = stage.stage(&mut working_vec)?;
78        }
79        self.final_stage.to_output(&mut working_vec, dst)?;
80        for finalization in self.post_finalization.iter() {
81            finalization.finalize(src, dst)?;
82        }
83        Ok(())
84    }
85}