Skip to main content

inference_engine/reasoning/source/
diagnostic.rs

1use crate::reasoning::{
2    DatatypeConflict, DatatypeConflictSink, LexicalViolation, LiteralOccurrence,
3};
4use serde::{Deserialize, Serialize};
5use std::{error::Error, future::Future, num::NonZeroU64};
6
7/// Non-RDF evidence about the selected source, never target augmentation.
8#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
9#[serde(deny_unknown_fields)]
10pub enum Rdf12SourceDiagnostic<T> {
11    /// One member belongs to two disjoint recognized default datatype classes.
12    DatatypeConflict(DatatypeConflict<T>),
13    /// A recognized literal has no denotation under the selected default map.
14    /// Its entire payload passed transport/UTF-8 inspection through EOF.
15    IllTypedLiteral {
16        /// Exact source key, original root and recursive occurrence path.
17        occurrence: LiteralOccurrence<T>,
18        /// Exact complete lexical byte count, without retaining the payload.
19        lexical_bytes: u64,
20        /// First violation reported by the shared lexical semantics.
21        violation: LexicalViolation,
22    },
23}
24
25/// Caller-owned streamed/spilled source diagnostics, separate from RDF scratch.
26pub trait Rdf12SourceDiagnosticSink: Send {
27    /// Exact source term identity.
28    type Term: Clone + Eq + Send + Sync;
29    /// Storage failure; losing a diagnostic aborts the operation.
30    type Error: Error + Send + Sync + 'static;
31    /// Stage one diagnostic with caller-owned source snapshot/graph provenance.
32    /// Any later failure invalidates all partial diagnostics and inferred output.
33    fn record(
34        &mut self,
35        diagnostic: Rdf12SourceDiagnostic<Self::Term>,
36    ) -> impl Future<Output = Result<(), Self::Error>> + Send;
37}
38
39/// Explicit aggregate caps for source literal preflight, before inference.
40#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
41#[serde(deny_unknown_fields)]
42pub struct SourceLiteralLimits {
43    pub(super) occurrences: NonZeroU64,
44    pub(super) diagnostics: NonZeroU64,
45    pub(super) bytes: u64,
46}
47impl SourceLiteralLimits {
48    /// Set examined occurrences, semantic diagnostics and total lexical bytes.
49    /// Zero bytes admits only empty payloads, each verified through an EOF probe.
50    pub const fn new(occurrences: NonZeroU64, diagnostics: NonZeroU64, bytes: u64) -> Self {
51        Self {
52            occurrences,
53            diagnostics,
54            bytes,
55        }
56    }
57}
58
59pub(super) struct ConflictSink<'a, D>(pub &'a mut D);
60impl<D: Rdf12SourceDiagnosticSink> DatatypeConflictSink for ConflictSink<'_, D> {
61    type Term = D::Term;
62    type Error = D::Error;
63    async fn record(&mut self, conflict: DatatypeConflict<D::Term>) -> Result<(), D::Error> {
64        self.0
65            .record(Rdf12SourceDiagnostic::DatatypeConflict(conflict))
66            .await
67    }
68}