arnalisa 0.6.8

Pipeline system for calculating values
Documentation
//! A bin that writes it's input values to a writer in CBOR format.
//!
//! ```text
//!   ┌──[cborsink]──┐
//!  ⇒│<input 0>     │
//!  ⇒│<input 1>     │
//!  ⇒│<input 2>     │
//!  ⇒┊…             ┊
//!  ⇒│<input n>     │
//!   └──────────────┘
//! ```

use serde_cbor;

use super::{
    BinBuildEnvironment, BinDescription, Calculator, FetchItem,
    GetCalibration, Item, Iteration, Result, Scope, SinkBin, SinkNames,
    SinkOnlyBin, SinkOnlyBinDescription,
};
use crate::error;
use indexmap::{IndexMap, IndexSet};
use snafu::ResultExt;
use std::fs::File;
use std::io::{LineWriter, Stdout, Write};

static BIN_TYPE: &str = "cborsink";

/// A bin that writes it's input values to a writer in CBOR format.
#[derive(Debug)]
pub struct Bin {
    scope: Scope,
    output: Output,
    sources: IndexMap<String, Box<dyn FetchItem>>,
}

#[derive(Debug)]
enum Output {
    File(LineWriter<File>),
    Stdout(LineWriter<Stdout>),
}

impl Output {
    fn writer(&mut self) -> &mut dyn Write {
        match self {
            Output::File(w) => w,
            Output::Stdout(w) => w,
        }
    }
}

impl SinkBin for Bin {}

impl Calculator for Bin {
    fn calculate(&mut self, _iteration: &Iteration) -> Result<()> {
        let items = self
            .sources
            .iter()
            .map(|(s, ds)| {
                ds.fetch_item(&self.scope).map(|d| (s.to_string(), d))
            })
            .collect::<Result<IndexMap<String, Item>>>()?;

        serde_cbor::to_writer(
            self.output.writer(),
            &items.values().collect::<Vec<_>>(),
        )
        .context(error::SerdeCbor)?;
        Ok(())
    }
}

impl SinkOnlyBin for Bin {}

/// Description for the CBOR sink bin.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Description {
    /// The set of sink names.
    pub sinks: IndexSet<String>,

    /// The path of the output file.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub file_path: Option<String>,
}

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> {
        self.sinks.clone()
    }
}

impl SinkOnlyBinDescription for Description {
    fn build_bin(
        &self,
        scope: &Scope,
        env: &mut dyn BinBuildEnvironment,
    ) -> Result<Self::Bin> {
        let mut output = if let Some(ref path) = self.file_path {
            let file =
                File::create(&path.to_string()).context(error::Io)?;
            Output::File(LineWriter::new(file))
        } else {
            Output::Stdout(LineWriter::new(std::io::stdout()))
        };
        serde_cbor::to_writer(output.writer(), &self.sinks)
            .context(error::SerdeCbor)?;

        Ok(Bin {
            scope: scope.clone(),
            output,
            sources: self
                .sinks
                .iter()
                .map(|s| env.resolve(s).map(|ds| (s.to_string(), ds)))
                .collect::<Result<IndexMap<_, _>>>()?,
        })
    }
}