#[macro_use]
extern crate serde_derive;

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>>;
}
