1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! A bin that outputs the input value whenever the trigger is a value
//! that is neither 0 nor `false`.
//!
//! ```text
//!   ┌────[blocker]────┐
//!  ⇒│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 = "blocker";

/// A blocker bin.
#[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)?;

        self.result_output = if trigger.counts_as_true() {
            input
        } else {
            Item::Nothing
        };
        Ok(())
    }
}

/// Description for the blocker 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 SourceNames for Description {
    fn source_names(&self) -> Result<IndexSet<String>> {
        Ok(source_names_output())
    }
}

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

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 {}