crisol_core/data/
lineage.rs1use std::collections::BTreeMap;
4
5#[non_exhaustive]
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct LineageStep {
19 pub source: String,
21 pub operation: String,
23 pub parameters: BTreeMap<String, String>,
25}
26
27impl LineageStep {
28 pub fn new(source: impl Into<String>, operation: impl Into<String>) -> Self {
30 Self {
31 source: source.into(),
32 operation: operation.into(),
33 parameters: BTreeMap::new(),
34 }
35 }
36
37 pub fn parameter(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
39 self.parameters.insert(key.into(), value.into());
40 self
41 }
42}
43
44#[non_exhaustive]
55#[derive(Clone, Debug, Default, PartialEq, Eq)]
56#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
57pub struct Lineage {
58 pub steps: Vec<LineageStep>,
60}
61
62impl Lineage {
63 pub fn new() -> Self {
65 Self::default()
66 }
67
68 pub fn record(&mut self, step: LineageStep) {
70 self.steps.push(step);
71 }
72
73 pub fn sources(&self) -> Vec<&str> {
75 let mut sources: Vec<&str> = self.steps.iter().map(|step| step.source.as_str()).collect();
76 sources.sort_unstable();
77 sources.dedup();
78 sources
79 }
80
81 pub fn operations(&self) -> Vec<&str> {
83 let mut operations: Vec<&str> = self.steps.iter().map(|step| step.operation.as_str()).collect();
84 operations.sort_unstable();
85 operations.dedup();
86 operations
87 }
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn lineage_records_sources_and_operations() {
96 let mut lineage = Lineage::new();
97 lineage.record(LineageStep::new("input.csv", "csv").parameter("delimiter", ","));
98 lineage.record(LineageStep::new("input.csv", "filter").parameter("where", "v > 0"));
99 assert_eq!(lineage.sources(), ["input.csv"]);
100 assert_eq!(lineage.operations(), ["csv", "filter"]);
101 assert_eq!(lineage.steps[1].parameters["where"], "v > 0");
102 }
103}