use super::{
sink_names_a_b, source_names_output, BinBuildEnvironment,
BinDescription, Calculator, FetchItem, GetCalibration, Item,
Iteration, Result, Scope, SinkBin, SinkNames, SourceBin, SourceId,
SourceNames, SourceSinkBinDescription, WriteDotSimple, SINK_A, SINK_B,
SOURCE_OUTPUT,
};
use crate::error;
use crate::item::TwoItemsExt;
use indexmap::IndexSet;
use std;
static BIN_TYPE: &str = "add";
#[derive(Debug)]
pub struct Bin {
scope: Scope,
source_a: Box<dyn FetchItem>,
source_b: 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 a = self.source_a.fetch_item(&self.scope)?;
let b = self.source_b.fetch_item(&self.scope)?;
self.result_output = match (a, b).common_type() {
(Item::B(a), Item::B(b)) => logic(a as u8, b as u8),
(Item::I8(a), Item::I8(b)) => logic(a, b),
(Item::I16(a), Item::I16(b)) => logic(a, b),
(Item::I32(a), Item::I32(b)) => logic(a, b),
(Item::I64(a), Item::I64(b)) => logic(a, b),
(Item::U8(a), Item::U8(b)) => logic(a, b),
(Item::U16(a), Item::U16(b)) => logic(a, b),
(Item::U32(a), Item::U32(b)) => logic(a, b),
(Item::U64(a), Item::U64(b)) => logic(a, b),
(Item::F32(a), Item::F32(b)) => logic(a, b),
(Item::F64(a), Item::F64(b)) => logic(a, b),
_ => Item::Nothing,
};
Ok(())
}
}
fn logic<T: std::ops::Add + Into<Item>>(a: T, b: T) -> Item
where
T: std::ops::Add,
Item: From<<T as std::ops::Add>::Output>,
{
(a + b).into()
}
#[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_a_b()
}
}
impl SourceSinkBinDescription for Description {
fn build_bin(
&self,
scope: &Scope,
env: &mut dyn BinBuildEnvironment,
) -> Result<Self::Bin> {
Ok(Bin {
scope: scope.clone(),
source_a: env.resolve(SINK_A)?,
source_b: env.resolve(SINK_B)?,
result_output: Item::Nothing,
})
}
}
impl WriteDotSimple for Description {}
#[cfg(test)]
mod tests {
use super::Description;
use crate::bins::{directsource, verificationsink};
use crate::Item;
use crate::{run_bin, Result};
use indexmap::indexset;
#[test]
fn simulate() -> Result<()> {
use crate::Item::*;
let input = directsource::Description {
columns: indexset!["a".to_string(), "b".to_string()],
rows: vec![
vec![Nothing, Nothing],
vec![Item::from(0f64), Nothing],
vec![Nothing, Item::from(0f64)],
vec![Item::from(0i64), Item::from(0f64)],
vec![Item::from(1.6f64), Item::from(0f64)],
vec![Item::from(0u64), Item::from(1.7f64)],
vec![Item::from(1.4f64), Item::from(1.6f64)],
vec![Item::from(1.3f64), Item::from(false)],
vec![Item::from(true), Item::from(1f64)],
vec![Item::from(false), Item::from(1f64)],
vec![Item::from(5u8), Item::from(false)],
vec![Item::from(9u16), Item::from(true)],
vec![Item::from(false), Item::from(false)],
vec![Item::from(false), Item::from(true)],
vec![Item::from(true), Item::from(false)],
vec![Item::from(true), Item::from(true)],
]
.into(),
};
let verification = verificationsink::Description {
columns: indexset!["output".to_string()],
expected: vec![
vec![Nothing],
vec![Nothing],
vec![Nothing],
vec![Item::from(0f64)],
vec![Item::from(1.6f64)],
vec![Item::from(1.7f64)],
vec![Item::from(3f64)],
vec![Item::from(1.3f64)],
vec![Item::from(2f64)],
vec![Item::from(1f64)],
vec![Item::from(5u8)],
vec![Item::from(10u16)],
vec![Item::from(0u8)],
vec![Item::from(1u8)],
vec![Item::from(1u8)],
vec![Item::from(2u8)],
]
.into(),
};
run_bin(&input, &Description {}, &verification)
}
}