Skip to main content

tensorlogic_trustformers/rule_guided_decoder/
error.rs

1//! Error types for the rule-guided decoder.
2//!
3//! We keep these types local to the module to avoid polluting the crate-wide
4//! [`crate::error::TrustformerError`] with decoder-specific variants.  A
5//! [`From`] impl bridges the two when the decoder is invoked from higher-level
6//! code.
7
8use thiserror::Error;
9
10use tensorlogic_infer::beam_search::BeamSearchError;
11
12/// Error cases specific to rule-guided decoding.
13#[derive(Debug, Error)]
14pub enum RuleGuidedError {
15    /// Compiling a `TLExpr` into a runtime representation failed.
16    #[error("rule-guided decoder compilation error: {0}")]
17    CompilationError(String),
18
19    /// The caller supplied an invalid configuration (e.g. lambda < 0).
20    #[error("rule-guided decoder configuration error: {0}")]
21    InvalidConfig(String),
22
23    /// The caller's scoring function returned an error (forwarded from
24    /// `BeamSearchDecoder`).
25    #[error("beam search failure: {0}")]
26    BeamSearch(#[from] BeamSearchError),
27
28    /// The wrapped score function returned a logits row whose length did not
29    /// match the configured vocabulary size.
30    #[error("logits row width mismatch: expected {expected}, got {got}")]
31    LogitsWidthMismatch { expected: usize, got: usize },
32}
33
34/// Result alias for rule-guided decoder operations.
35pub type RuleGuidedResult<T> = Result<T, RuleGuidedError>;
36
37impl From<RuleGuidedError> for crate::error::TrustformerError {
38    fn from(err: RuleGuidedError) -> Self {
39        crate::error::TrustformerError::CompilationError(err.to_string())
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn display_contains_context() {
49        let err = RuleGuidedError::InvalidConfig("lambda must be non-negative".into());
50        let msg = err.to_string();
51        assert!(msg.contains("lambda"));
52        assert!(msg.contains("configuration"));
53    }
54
55    #[test]
56    fn bridges_into_trustformer_error() {
57        let err = RuleGuidedError::CompilationError("oops".into());
58        let bridged: crate::error::TrustformerError = err.into();
59        assert!(bridged.to_string().contains("oops"));
60    }
61}