arnalisa 0.6.8

Pipeline system for calculating values
Documentation
//! A bin that calculates the minimum of all inputs.
//!
//! ```text
//!   ┌────[minimum]────┐
//!  ⇒│input      output│⇒
//!   └─────────────────┘
//! ```

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

static BIN_TYPE: &str = "minimum";

/// A bin that calculates the maximum of all inputs.
#[derive(Debug)]
pub struct Bin {
    scope: Scope,
    source_input: Box<dyn FetchItem>,
    changed: bool,
    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 if source.id == SOURCE_CHANGED {
            Ok(Item::from(self.changed))
        } 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)?;

        match (self.result_output.to_float(), input.to_float()) {
            (Ok(m), Ok(v)) if v < m => {
                self.changed = self.result_output != input;
                self.result_output = input;
            }
            (Err(_), Ok(_v)) => {
                self.changed = self.result_output != input;
                self.result_output = input;
            }
            _ => {
                self.changed = false;
            }
        }
        Ok(())
    }
}

/// Description for the maximum 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()
    }
}

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

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)?,
            changed: false,
            result_output: Item::Nothing,
        })
    }
}

impl WriteDotSimple for Description {}