Skip to main content

qualia_core_db/nlp/contracts/
error.rs

1//! Typed contract failures. None of these convert to empty success.
2
3use core::fmt;
4
5/// Typed processor outcome. Empty success is [`AnalysisState::Complete`] with
6/// zero counts; it is not an error and is not an unavailable processor.
7///
8/// v1 of this module constructs only [`AnalysisState::Complete`]. Other
9/// variants are reserved for later processors (partial coverage, cancelled,
10/// …) and must not be treated as currently emitted.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[repr(u8)]
13pub enum AnalysisState {
14    Complete = 0,
15    Partial = 1,
16    Unsupported = 2,
17    NotRequested = 3,
18    BudgetExceeded = 4,
19    Cancelled = 5,
20    InvalidInput = 6,
21}
22
23/// Which caller slice ran out of room.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25#[repr(u8)]
26pub enum BufferChannel {
27    Tokens = 0,
28    Sentences = 1,
29    Hits = 2,
30    Norms = 3,
31    Plans = 4,
32}
33
34/// Failures for the buffered contract API. Never converted into empty success.
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum NlpContractError {
37    SourceTooLarge {
38        bytes: usize,
39        max: usize,
40    },
41    OutputBufferFull {
42        needed: usize,
43        capacity: usize,
44        channel: BufferChannel,
45    },
46    SpanInvalid {
47        start: u32,
48        end: u32,
49    },
50}
51
52impl fmt::Display for NlpContractError {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match *self {
55            Self::SourceTooLarge { bytes, max } => {
56                write!(f, "NLP source is {bytes} bytes; max is {max}")
57            }
58            Self::OutputBufferFull {
59                needed,
60                capacity,
61                channel,
62            } => write!(
63                f,
64                "NLP {channel:?} buffer needs {needed} slots; had {capacity}"
65            ),
66            Self::SpanInvalid { start, end } => {
67                write!(f, "NLP span {start}..{end} is not a UTF-8 source slice")
68            }
69        }
70    }
71}
72
73impl std::error::Error for NlpContractError {}