Skip to main content

logicaffeine_kernel/interface/
error.rs

1//! Error types for the Vernacular interface.
2
3use crate::KernelError;
4use std::fmt;
5
6/// Errors that can occur in the interface layer.
7#[derive(Debug)]
8pub enum InterfaceError {
9    /// Parse error in command or term syntax.
10    Parse(ParseError),
11
12    /// Error from the kernel (type checking, etc.)
13    Kernel(KernelError),
14}
15
16/// Parse errors for the Vernacular.
17#[derive(Debug, Clone)]
18pub enum ParseError {
19    /// Unexpected end of input.
20    UnexpectedEof,
21
22    /// Unknown command keyword.
23    UnknownCommand(String),
24
25    /// Expected a specific token.
26    Expected { expected: String, found: String },
27
28    /// Invalid identifier.
29    InvalidIdent(String),
30
31    /// Invalid number literal.
32    InvalidNumber(String),
33
34    /// Missing required component.
35    Missing(String),
36
37    // ============================================================
38    // Literate Syntax Errors
39    // ============================================================
40
41    /// Missing "is either" clause in inductive definition.
42    MissingEitherClause,
43
44    /// Missing variant name in inductive definition.
45    MissingVariantName,
46
47    /// Invalid Consider/When/Yield syntax.
48    InvalidConsiderSyntax(String),
49
50    /// Missing Yield in function body.
51    MissingYield,
52
53    /// Missing When clause in Consider block.
54    MissingWhenClause,
55
56    /// Unclosed Consider block.
57    UnclosedConsiderBlock,
58}
59
60impl fmt::Display for InterfaceError {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match self {
63            InterfaceError::Parse(e) => write!(f, "Parse error: {}", e),
64            InterfaceError::Kernel(e) => write!(f, "Kernel error: {}", e),
65        }
66    }
67}
68
69impl fmt::Display for ParseError {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            ParseError::UnexpectedEof => write!(f, "Unexpected end of input"),
73            ParseError::UnknownCommand(cmd) => write!(f, "Unknown command: {}", cmd),
74            ParseError::Expected { expected, found } => {
75                write!(f, "Expected {}, found {}", expected, found)
76            }
77            ParseError::InvalidIdent(s) => write!(f, "Invalid identifier: {}", s),
78            ParseError::InvalidNumber(s) => write!(f, "Invalid number literal: {}", s),
79            ParseError::Missing(what) => write!(f, "Missing {}", what),
80            // Literate syntax errors
81            ParseError::MissingEitherClause => write!(f, "Missing 'is either' clause in type definition"),
82            ParseError::MissingVariantName => write!(f, "Missing variant name in type definition"),
83            ParseError::InvalidConsiderSyntax(msg) => write!(f, "Invalid Consider syntax: {}", msg),
84            ParseError::MissingYield => write!(f, "Missing 'Yield' in function body"),
85            ParseError::MissingWhenClause => write!(f, "Missing 'When' clause in Consider block"),
86            ParseError::UnclosedConsiderBlock => write!(f, "Unclosed Consider block"),
87        }
88    }
89}
90
91impl std::error::Error for InterfaceError {}
92impl std::error::Error for ParseError {}
93
94impl From<ParseError> for InterfaceError {
95    fn from(e: ParseError) -> Self {
96        InterfaceError::Parse(e)
97    }
98}
99
100impl From<KernelError> for InterfaceError {
101    fn from(e: KernelError) -> Self {
102        InterfaceError::Kernel(e)
103    }
104}