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
113
114
#[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
    }
}