arnalisa 0.6.8

Pipeline system for calculating values
Documentation
//! A bin that stores the input value whenever the trigger is a value
//! that is neither 0 nor `false`.
//!
//! Whenever the `trigger` value counts as active, the input value
//! gets stored and propagated to output. This is also true when the
//! input value is `Nothing`. When the `trigger value counts as inactive,
//! the last stored value continues to get propagated to the output.
//!
//! ```text
//!   ┌────[storage]────┐
//!  ⇒│input      output│⇒
//!  ⇒│trigger          │
//!   └─────────────────┘
//! ```

use super::{
    sink_names_input_trigger, source_names_output, BinBuildEnvironment,
    BinDescription, Calculator, FetchItem, GetCalibration, Item,
    Iteration, Result, Scope, SinkBin, SinkNames, SourceBin, SourceId,
    SourceNames, SourceSinkBinDescription, WriteDotSimple, SINK_INPUT,
    SINK_TRIGGER, SOURCE_OUTPUT,
};
use crate::error;
use indexmap::IndexSet;

static BIN_TYPE: &str = "storage";

/// A bin that stores the input value on trigger.
#[derive(Debug)]
pub struct Bin {
    scope: Scope,

    source_input: Box<dyn FetchItem>,
    source_trigger: Box<dyn FetchItem>,

    result_output: Item,
}

impl SinkBin for Bin {}

impl SourceBin for Bin {
    fn get_source_data(&self, source: &SourceId) -> Result<Item> {
        if source.id == SOURCE_OUTPUT {
            Ok(self.result_output.clone())
        } else {
            error::MissingSourceName {
                scope: self.scope.clone(),
                name: source.id.to_string(),
                bin_type: BIN_TYPE.to_string(),
            }
            .fail()
        }
    }
}

impl Calculator for Bin {
    fn calculate(&mut self, _iteration: &Iteration) -> Result<()> {
        let input = self.source_input.fetch_item(&self.scope)?;
        let trigger = self.source_trigger.fetch_item(&self.scope)?;

        if trigger.counts_as_true() {
            self.result_output = input;
        }
        Ok(())
    }
}

/// Description for the storage bin.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Description;

impl BinDescription for Description {
    type Bin = Bin;

    fn check_validity(
        &self,
        _scope: &Scope,
        _get_calibration: &mut dyn GetCalibration,
    ) -> Result<()> {
        Ok(())
    }

    fn bin_type(&self) -> &'static str {
        BIN_TYPE
    }
}

impl SinkNames for Description {
    fn sink_names(&self) -> IndexSet<String> {
        sink_names_input_trigger()
    }
}

impl SourceNames for Description {
    fn source_names(&self) -> Result<IndexSet<String>> {
        Ok(source_names_output())
    }
}

impl SourceSinkBinDescription for Description {
    fn build_bin(
        &self,
        scope: &Scope,
        env: &mut dyn BinBuildEnvironment,
    ) -> Result<Self::Bin> {
        Ok(Bin {
            scope: scope.clone(),
            source_input: env.resolve(SINK_INPUT)?,
            source_trigger: env.resolve(SINK_TRIGGER)?,
            result_output: Item::Nothing,
        })
    }
}

impl WriteDotSimple for Description {}

#[cfg(test)]
mod tests {
    use super::Description;
    use crate::bins::{directsource, verificationsink};
    use crate::{run_bin, Result};
    use indexmap::indexset;

    #[test]
    fn simulate() -> Result<()> {
        use crate::Item::*;

        let input = directsource::Description {
            columns: indexset!["input".to_string(), "trigger".to_string()],
            rows: vec![
                vec![Nothing, Nothing],
                vec![Nothing, U8(1)],
                vec![U8(1), Nothing],
                vec![U8(1), U8(1)],
                vec![U8(0), U8(1)],
                vec![U8(4), U8(1)],
                vec![U8(1), U8(0)],
                vec![U8(0), U8(0)],
                vec![U8(2), U8(0)],
                vec![U8(1), U8(2)],
                vec![U8(2), U8(1)],
                vec![U8(2), U8(2)],
                vec![Nothing, Nothing],
                vec![Nothing, Nothing],
                vec![Nothing, U8(1)],
            ]
            .into(),
        };
        let verification = verificationsink::Description {
            columns: indexset!["output".to_string()],
            expected: vec![
                vec![Nothing],
                vec![Nothing],
                vec![Nothing],
                vec![U8(1)],
                vec![U8(0)],
                vec![U8(4)],
                vec![U8(4)],
                vec![U8(4)],
                vec![U8(4)],
                vec![U8(1)],
                vec![U8(2)],
                vec![U8(2)],
                vec![U8(2)],
                vec![U8(2)],
                vec![Nothing],
            ]
            .into(),
        };

        run_bin(&input, &Description {}, &verification)
    }
}