Skip to main content

weavatrix_graph/model/
provenance.rs

1use super::SourceSpan;
2use crate::String;
3use crate::{EvidenceKind, GraphError, Result};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
7#[serde(rename_all = "snake_case")]
8#[non_exhaustive]
9pub enum Confidence {
10    Exact,
11    High,
12    Medium,
13    Low,
14}
15
16#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
17pub struct Provenance {
18    pub extractor: String,
19    pub evidence: EvidenceKind,
20    pub confidence: Confidence,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub span: Option<SourceSpan>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub detail: Option<String>,
25}
26
27impl Provenance {
28    /// Creates evidence provenance with a non-empty extractor identity.
29    ///
30    /// # Errors
31    ///
32    /// Returns an error when the extractor is empty.
33    pub fn new(
34        extractor: impl Into<String>,
35        evidence: EvidenceKind,
36        confidence: Confidence,
37    ) -> Result<Self> {
38        let extractor = extractor.into();
39        if extractor.is_empty() {
40            return Err(GraphError::EmptyExtractor);
41        }
42        Ok(Self {
43            extractor,
44            evidence,
45            confidence,
46            span: None,
47            detail: None,
48        })
49    }
50
51    #[must_use]
52    pub fn with_span(mut self, span: SourceSpan) -> Self {
53        self.span = Some(span);
54        self
55    }
56
57    #[must_use]
58    pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
59        self.detail = Some(detail.into());
60        self
61    }
62}