use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ImportBatchId(String);
impl ImportBatchId {
#[must_use]
pub fn new() -> Self {
Self(uuid::Uuid::new_v4().to_string())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Default for ImportBatchId {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for ImportBatchId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum IngestProgress {
Started {
total: usize,
},
DocumentSkipped {
uri: String,
},
DocumentDone {
uri: String,
entities: usize,
edges: usize,
},
DocumentFailed {
uri: String,
reason: String,
},
DocumentRejected {
uri: String,
reason: String,
},
Finished,
}
#[derive(Debug, Clone)]
pub struct IngestFailure {
pub uri: String,
pub reason: String,
}
#[derive(Debug, Clone)]
pub struct HubDegree {
pub entity: String,
pub degree: usize,
}
#[derive(Debug, Clone, Default)]
pub struct IngestReport {
pub batch_id: String,
pub documents_total: usize,
pub skipped: usize,
pub succeeded: usize,
pub rejected: usize,
pub failed: Vec<IngestFailure>,
pub entities_total: usize,
pub edges_total: usize,
pub dry_run: bool,
pub hub_degree: Vec<HubDegree>,
}
#[cfg(test)]
mod tests {
use super::{ImportBatchId, IngestFailure, IngestReport};
#[test]
fn import_batch_id_is_unique() {
let a = ImportBatchId::new();
let b = ImportBatchId::new();
assert_ne!(a, b);
assert_eq!(a.as_str().len(), 36);
assert_eq!(a.as_str().chars().filter(|&c| c == '-').count(), 4);
}
#[test]
fn ingest_report_rejected_arithmetic_invariant() {
let mut report = IngestReport {
documents_total: 4,
..IngestReport::default()
};
report.skipped += 1;
report.succeeded += 1;
report.rejected += 1;
report.failed.push(IngestFailure {
uri: "u".into(),
reason: "r".into(),
});
assert_eq!(
report.skipped + report.succeeded + report.rejected + report.failed.len(),
report.documents_total
);
}
}