crisol-core 1.3.0

Crisol core: an egui-free data model, themes, metrics, localization, commands, state, and storage.
Documentation
//! Serializable data lineage: where a dataset came from and what changed it.

use std::collections::BTreeMap;

/// One transformation applied to a dataset.
///
/// # Example
///
/// ```
/// use crisol_core::data::LineageStep;
///
/// let step = LineageStep::new("input.csv", "csv");
/// assert_eq!(step.operation, "csv");
/// ```
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LineageStep {
    /// Source name or path.
    pub source: String,
    /// Operation, such as `csv`, `filter`, or `join`.
    pub operation: String,
    /// Operation parameters.
    pub parameters: BTreeMap<String, String>,
}

impl LineageStep {
    /// A step with no parameters.
    pub fn new(source: impl Into<String>, operation: impl Into<String>) -> Self {
        Self {
            source: source.into(),
            operation: operation.into(),
            parameters: BTreeMap::new(),
        }
    }

    /// Add a parameter.
    pub fn parameter(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.parameters.insert(key.into(), value.into());
        self
    }
}

/// The ordered history of a dataset.
///
/// # Example
///
/// ```
/// use crisol_core::data::Lineage;
///
/// let lineage = Lineage::new();
/// assert!(lineage.steps.is_empty());
/// ```
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Lineage {
    /// Steps in application order.
    pub steps: Vec<LineageStep>,
}

impl Lineage {
    /// An empty lineage.
    pub fn new() -> Self {
        Self::default()
    }

    /// Append a step.
    pub fn record(&mut self, step: LineageStep) {
        self.steps.push(step);
    }

    /// The distinct sources named by the steps.
    pub fn sources(&self) -> Vec<&str> {
        let mut sources: Vec<&str> = self.steps.iter().map(|step| step.source.as_str()).collect();
        sources.sort_unstable();
        sources.dedup();
        sources
    }

    /// The distinct operations named by the steps.
    pub fn operations(&self) -> Vec<&str> {
        let mut operations: Vec<&str> = self.steps.iter().map(|step| step.operation.as_str()).collect();
        operations.sort_unstable();
        operations.dedup();
        operations
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lineage_records_sources_and_operations() {
        let mut lineage = Lineage::new();
        lineage.record(LineageStep::new("input.csv", "csv").parameter("delimiter", ","));
        lineage.record(LineageStep::new("input.csv", "filter").parameter("where", "v > 0"));
        assert_eq!(lineage.sources(), ["input.csv"]);
        assert_eq!(lineage.operations(), ["csv", "filter"]);
        assert_eq!(lineage.steps[1].parameters["where"], "v > 0");
    }
}