Skip to main content

qualia_core_db/nlp/coref/
error.rs

1//! Typed coreference failures. None of these convert to empty success.
2
3use core::fmt;
4
5/// Recoverable coreference failure. Host adapters map budget/cancel/output
6/// variants to `E400` and the rest to `E100`.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum CorefError {
9    SourceTooLarge { bytes: usize, max: usize },
10    TooManyMentions { count: usize, max: usize },
11    MentionTextTooLarge { bytes: usize, max: usize },
12    TooManyChains { count: usize, max: usize },
13    WorkspaceTooSmall { required: usize, provided: usize },
14    NormalizationBudget { used: u64, max: u64 },
15    ComparisonBudget { used: u64, max: u64 },
16    AntecedentBudget { used: u64, max: u64 },
17    OutputBufferFull,
18    Cancelled,
19    NegativeOffset,
20    OffsetOverflow,
21    SpanEmpty,
22    SpanOutOfRange,
23    SpanNotUtf8Boundary,
24    TextMismatch,
25    UnsortedMentions,
26    InvalidKind,
27    InvalidInput,
28}
29
30impl CorefError {
31    /// Budget, cancellation, and output-capacity failures.
32    pub fn is_resource_error(self) -> bool {
33        matches!(
34            self,
35            Self::SourceTooLarge { .. }
36                | Self::TooManyMentions { .. }
37                | Self::MentionTextTooLarge { .. }
38                | Self::TooManyChains { .. }
39                | Self::WorkspaceTooSmall { .. }
40                | Self::NormalizationBudget { .. }
41                | Self::ComparisonBudget { .. }
42                | Self::AntecedentBudget { .. }
43                | Self::OutputBufferFull
44                | Self::Cancelled
45        )
46    }
47}
48
49impl fmt::Display for CorefError {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match *self {
52            Self::SourceTooLarge { bytes, max } => {
53                write!(f, "coref source is {bytes} bytes; max is {max}")
54            }
55            Self::TooManyMentions { count, max } => {
56                write!(f, "coref received {count} mentions; max is {max}")
57            }
58            Self::MentionTextTooLarge { bytes, max } => {
59                write!(f, "coref mention text totals {bytes} bytes; max is {max}")
60            }
61            Self::TooManyChains { count, max } => {
62                write!(f, "coref would emit {count} chains; max is {max}")
63            }
64            Self::WorkspaceTooSmall { required, provided } => {
65                write!(f, "coref workspace needs {required} bytes; had {provided}")
66            }
67            Self::NormalizationBudget { used, max } => {
68                write!(f, "coref normalization used {used} bytes; max is {max}")
69            }
70            Self::ComparisonBudget { used, max } => {
71                write!(f, "coref comparisons used {used} bytes; max is {max}")
72            }
73            Self::AntecedentBudget { used, max } => {
74                write!(f, "coref antecedent checks {used}; max is {max}")
75            }
76            Self::OutputBufferFull => f.write_str("coref output buffer is full"),
77            Self::Cancelled => f.write_str("coref cancelled"),
78            Self::NegativeOffset => f.write_str("coref span offset is negative"),
79            Self::OffsetOverflow => f.write_str("coref span offset exceeds u32"),
80            Self::SpanEmpty => f.write_str("coref span is empty or reversed"),
81            Self::SpanOutOfRange => f.write_str("coref span is outside the source"),
82            Self::SpanNotUtf8Boundary => f.write_str("coref span is not on a UTF-8 boundary"),
83            Self::TextMismatch => f.write_str("coref mention text does not match the source span"),
84            Self::UnsortedMentions => f.write_str("coref mentions are not in document order"),
85            Self::InvalidKind => f.write_str("coref mention kind is not supported"),
86            Self::InvalidInput => f.write_str("coref input is missing or wrongly typed"),
87        }
88    }
89}
90
91impl std::error::Error for CorefError {}