arnalisa 0.3.3

Pipeline system for calculating values
Documentation
#[macro_use]
extern crate serde_derive;

extern crate chrono;
extern crate decorum;
extern crate float_cmp;
extern crate log;
extern crate serde;
extern crate serde_cbor;
extern crate serde_json;
extern crate snafu;

pub mod bins;
pub mod error;
mod item;
mod run;
mod sortable_float;

pub use self::item::Item;
pub use crate::error::Error;
pub use crate::run::{run_bin, run_bin_with_calibration};
pub use decorum::{R32, R64};

use indexmap::IndexMap;
use std::fmt;

#[derive(Clone)]
pub enum Scope {
    Base,
    File(String),
    Sub { parent: Box<Scope>, name: String },
}

impl Default for Scope {
    fn default() -> Self {
        Scope::Base
    }
}

impl Scope {
    pub fn enter<N: AsRef<str>>(&self, name: N) -> Self {
        Scope::Sub {
            parent: Box::new(self.clone()),
            name: name.as_ref().to_string(),
        }
    }
}

impl fmt::Debug for Scope {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Scope::Base => write!(f, "in-memory"),
            Scope::File(ref file) => write!(f, "file \"{}\"", file),
            Scope::Sub {
                ref parent,
                ref name,
            } => write!(f, "{:?}\"{}\"", parent, name),
        }
    }
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "source", content = "data")]
pub enum CalibrationSource {
    File { path: String },
    Embedded { curve: Vec<(f64, f64)> },
    Identifier { id: String },
}

#[derive(PartialEq, Debug, Clone)]
#[must_use]
pub enum Proceed {
    Stop,
    Continue,
}

pub trait GetCalibration {
    fn calibration(
        &mut self,
        source: &CalibrationSource,
    ) -> Result<IndexMap<R64, R64>>;
}

type DateTime = ::chrono::DateTime<::chrono::offset::FixedOffset>;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CalibrationPoint {
    pub coordinates: (f64, f64),
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub results: IndexMap<String, Item>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Calibration {
    pub points: Vec<CalibrationPoint>,
    pub timestamp: DateTime,
}

impl Calibration {
    pub fn points_map(&self) -> IndexMap<R64, R64> {
        let mut p = self
            .points
            .iter()
            .map(|p| {
                let (x, y) = p.coordinates;
                (R64::from(x), R64::from(y))
            })
            .collect::<IndexMap<R64, R64>>();
        p.sort_keys();
        p
    }
}