tabnas_proto/error.rs
1/* Copyright (c) 2026 Richard Rodger and other contributors, MIT License */
2
3//! What this crate refuses, and why.
4//!
5//! The package declares no error CODES of its own: input the grammar
6//! cannot recognise fails under the engine's base codes, and the two
7//! rejections the plugin makes itself, the syntax against edition version
8//! check and the nesting cap, are told apart by their messages. See the
9//! repository `AGENTS.md`, "Error codes".
10
11use std::fmt;
12
13use tabnas::TabnasError;
14
15/// A refused `.proto` document, or a plugin that could not be installed.
16#[derive(Debug, Clone)]
17pub enum ProtoError {
18 /// The engine rejected the source. Carries the engine's own error,
19 /// with its code, position and rendered report.
20 Parse(Box<TabnasError>),
21 /// The embedded compiled grammar could not be loaded or installed. Only a
22 /// broken build reaches this.
23 Grammar(String),
24 /// The plugin's own version check refused the document: an unknown
25 /// `syntax` or `edition` value, or a declaration disagreeing with the
26 /// supplied option.
27 Version(String),
28 /// The document nests deeper than [`crate::MAX_NESTING_DEPTH`].
29 ///
30 /// Every runtime refuses such a document before the engine runs, with
31 /// the same message and no code; only this port's walk also refuses a
32 /// tree that deep handed to it directly, because a Rust stack that
33 /// runs out aborts the process. See `DIVERGENCE.md`, section 2.
34 TooDeep(String),
35}
36
37impl ProtoError {
38 /// The engine error code, or `""` for a rejection this crate made
39 /// itself.
40 pub fn code(&self) -> &str {
41 match self {
42 ProtoError::Parse(error) => &error.code,
43 _ => "",
44 }
45 }
46
47 /// The 1-based row and column the engine reported, when there is one.
48 pub fn position(&self) -> Option<(usize, usize)> {
49 match self {
50 ProtoError::Parse(error) => Some((error.row, error.col)),
51 _ => None,
52 }
53 }
54}
55
56impl fmt::Display for ProtoError {
57 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
58 match self {
59 ProtoError::Parse(error) => error.fmt(formatter),
60 ProtoError::Grammar(message)
61 | ProtoError::Version(message)
62 | ProtoError::TooDeep(message) => formatter.write_str(message),
63 }
64 }
65}
66
67impl std::error::Error for ProtoError {}
68
69impl From<TabnasError> for ProtoError {
70 fn from(error: TabnasError) -> Self {
71 ProtoError::Parse(Box::new(error))
72 }
73}