Skip to main content

bubbles/
error.rs

1//! Shared error and result types for the crate.
2
3use thiserror::Error;
4
5/// Alias for `Result<T, DialogueError>`.
6pub type Result<T> = core::result::Result<T, DialogueError>;
7
8/// All errors that can be produced by compilation or runtime execution.
9#[non_exhaustive]
10#[derive(Debug, Error, Clone)]
11pub enum DialogueError {
12    /// A parse-time error, optionally localised to a source span.
13    #[error("parse error at {file}:{line}: {message}")]
14    Parse {
15        /// Source file name or `"<source>"`.
16        file: String,
17        /// 1-based line number.
18        line: usize,
19        /// Human-readable description.
20        message: String,
21    },
22    /// A reference to an unknown node.
23    #[error("unknown node '{0}'")]
24    UnknownNode(String),
25    /// A duplicate node title was found across merged sources.
26    #[error("duplicate node title '{0}'")]
27    DuplicateNode(String),
28    /// A validation failure detected after all sources are merged.
29    #[error("validation error: {0}")]
30    Validation(String),
31    /// The [`Runner`](crate::runtime::Runner) API was called in the wrong
32    /// order (e.g. [`next_event`](crate::runtime::Runner::next_event) while
33    /// awaiting an option, or
34    /// [`select_option`](crate::runtime::Runner::select_option) when not
35    /// awaiting).
36    ///
37    /// Embedders can match on this variant to detect integration bugs
38    /// without parsing the error message string.
39    #[error("protocol violation: {0}")]
40    ProtocolViolation(String),
41    /// A type mismatch in an expression (e.g. adding a number to a string).
42    ///
43    /// `expected` and `got` are human-readable descriptions of the involved
44    /// types; `context` is the operation that triggered the mismatch.
45    #[error("type mismatch in {context}: expected {expected}, got {got}")]
46    TypeMismatch {
47        /// The type the operation expected.
48        expected: String,
49        /// The type actually encountered.
50        got: String,
51        /// The operation that produced the mismatch (e.g. `"+"`).
52        context: String,
53    },
54    /// An unknown variable was referenced.
55    #[error("undefined variable '{0}'")]
56    UndefinedVariable(String),
57    /// A function call failed.
58    #[error("function '{name}' error: {message}")]
59    Function {
60        /// Function name.
61        name: String,
62        /// Description.
63        message: String,
64    },
65}