#![deny(
missing_docs,
missing_debug_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces,
unused_qualifications
)]
#[macro_use]
extern crate serde_derive;
use 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 crate::sortable_float::{Direction, HasDirection, HasReverse};
pub use decorum::{R32, R64};
use indexmap::IndexMap;
use snafu::OptionExt;
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>>;
}
pub trait IsCalibration {
fn calibration(&self) -> Result<IndexMap<R64, R64>>;
}
impl<T: IsCalibration> GetCalibration for IndexMap<String, T> {
fn calibration(
&mut self,
mode: &CalibrationSource,
) -> Result<IndexMap<R64, R64>> {
match *mode {
CalibrationSource::File { .. } => error::GetCalibration {
message: "IndexMap of calibrations cannot \
get calibration from file"
.to_string(),
}
.fail(),
CalibrationSource::Embedded { ref curve } => Ok(curve
.iter()
.map(|&(x, y)| (R64::from(x), R64::from(y)))
.collect::<IndexMap<R64, R64>>()),
CalibrationSource::Identifier { ref id } => Ok(self
.get(id)
.context(error::GetCalibration {
message: format!("Calibration {:?} not present.", id),
})?
.calibration()?),
}
}
}
type DateTime = ::chrono::DateTime<::chrono::offset::Utc>;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
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)]
#[serde(rename_all = "camelCase")]
pub struct Calibration {
pub points: Vec<CalibrationPoint>,
pub timestamp: DateTime,
}
impl Calibration {
pub fn sort_points(&mut self) {
self.points.sort_by(|a, b| {
a.coordinates.0.partial_cmp(&b.coordinates.0).unwrap()
});
}
}
impl IsCalibration for Calibration {
fn calibration(&self) -> Result<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();
Ok(p)
}
}