use alloc::string::String;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DataSource {
LiteratureCitation {
bibkey: String,
doi: Option<String>,
},
BundledFile {
path: String,
},
External {
url: String,
},
Computed {
name: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Provenance {
pub source: Option<DataSource>,
pub version: Option<String>,
pub retrieved_at: Option<String>,
pub checksum: Option<[u8; 32]>,
pub notes: Option<String>,
}
impl Provenance {
#[must_use]
pub const fn new() -> Self {
Self {
source: None,
version: None,
retrieved_at: None,
checksum: None,
notes: None,
}
}
#[must_use]
pub fn bundled_file(path: impl Into<String>) -> Self {
Self {
source: Some(DataSource::BundledFile { path: path.into() }),
..Self::new()
}
}
#[must_use]
pub fn cited(bibkey: impl Into<String>) -> Self {
Self {
source: Some(DataSource::LiteratureCitation {
bibkey: bibkey.into(),
doi: None,
}),
..Self::new()
}
}
#[must_use]
pub fn computed(name: impl Into<String>) -> Self {
Self {
source: Some(DataSource::Computed { name: name.into() }),
..Self::new()
}
}
#[must_use]
pub fn with_version(mut self, version: impl Into<String>) -> Self {
self.version = Some(version.into());
self
}
#[must_use]
pub fn with_notes(mut self, notes: impl Into<String>) -> Self {
self.notes = Some(notes.into());
self
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum TableSource {
EmbeddedBytes(&'static [u8]),
EmbeddedSlices {
data: &'static [f64],
len: usize,
},
ExternalPath(String),
Generated {
description: String,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn provenance_builders_compose() {
let provenance = Provenance::bundled_file("data/fit.csv")
.with_version("v1")
.with_notes("validated");
assert_eq!(provenance.version.as_deref(), Some("v1"));
assert_eq!(provenance.notes.as_deref(), Some("validated"));
}
}