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(
27 "duplicate node title '{0}' (add distinct `when:` headers for an intentional node group)"
28 )]
29 DuplicateNode(String),
30 /// A validation failure detected after all sources are merged.
31 #[error("validation error: {0}")]
32 Validation(String),
33 /// The [`Runner`](crate::runtime::Runner) API was called in the wrong
34 /// order (e.g. [`next_event`](crate::runtime::Runner::next_event) while
35 /// awaiting an option, or
36 /// [`select_option`](crate::runtime::Runner::select_option) when not
37 /// awaiting).
38 ///
39 /// Embedders can match on this variant to detect integration bugs
40 /// without parsing the error message string.
41 #[error("protocol violation: {0}")]
42 ProtocolViolation(String),
43 /// A type mismatch in an expression (e.g. adding a number to a string).
44 ///
45 /// `expected` and `got` are human-readable descriptions of the involved
46 /// types; `context` is the operation that triggered the mismatch.
47 #[error("type mismatch in {context}: expected {expected}, got {got}")]
48 TypeMismatch {
49 /// The type the operation expected.
50 expected: String,
51 /// The type actually encountered.
52 got: String,
53 /// The operation that produced the mismatch (e.g. `"+"`).
54 context: String,
55 },
56 /// An unknown variable was referenced.
57 #[error("undefined variable '{0}'")]
58 UndefinedVariable(String),
59 /// A function call failed.
60 #[error("function '{name}' error: {message}")]
61 Function {
62 /// Function name.
63 name: String,
64 /// Description.
65 message: String,
66 },
67}