1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::{error::Error, fmt};
use crate::{
consts::{CHOICE_MARKER, STICKY_CHOICE_MARKER},
error::{
parse::{condition::ConditionError, expression::ExpressionError},
utils::{write_line_information, MetaData},
},
};
#[derive(Debug)]
pub struct LineError {
pub line: String,
pub kind: LineErrorKind,
pub meta_data: MetaData,
}
#[derive(Debug)]
pub enum LineErrorKind {
ConditionError(ConditionError),
EmptyDivert,
EmptyExpression,
ExpectedEndOfLine { tail: String },
ExpressionError(ExpressionError),
FoundTunnel,
InvalidAddress { address: String },
StickyAndNonSticky,
UnmatchedBraces,
UnmatchedBrackets,
}
impl Error for LineError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.kind)
}
}
impl Error for LineErrorKind {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self {
LineErrorKind::ExpressionError(err) => Some(err),
LineErrorKind::ConditionError(err) => Some(err),
_ => None,
}
}
}
impl_from_error![
LineErrorKind;
[ConditionError, ConditionError],
[ExpressionError, ExpressionError]
];
impl fmt::Display for LineError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write_line_information(f, &self.meta_data)?;
write!(f, "{}", self.kind)
}
}
impl fmt::Display for LineErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use LineErrorKind::*;
match &self {
ConditionError(err) => write!(f, "could not parse a condition: {}", err),
EmptyDivert => write!(f, "encountered a divert statement with no address",),
EmptyExpression => write!(f, "found an empty embraced expression ('{{}})'"),
ExpectedEndOfLine { tail } => write!(
f,
"expected no more content after a divert statement address but found '{}'",
tail
),
ExpressionError(err) => write!(f, "could not parse an expression: {}", err),
FoundTunnel => write!(
f,
"Found multiple divert markers. In the `Ink` language this indicates \
a `tunnel` for the story to pass through, but these are not yet implemented \
in `inkling`."
),
InvalidAddress { address } => write!(
f,
"found an invalid address to knot, stitch or variable '{}': \
contains invalid characters",
address
),
StickyAndNonSticky => write!(
f,
"Encountered a line which has both non-sticky ('{}') and sticky ('{}') \
choice markers. This is not allowed.",
CHOICE_MARKER, STICKY_CHOICE_MARKER
),
UnmatchedBraces => write!(f, "line has unmatched curly '{{}}' braces"),
UnmatchedBrackets => write!(f, "choice line has unmatched square '[]' brackets"),
}
}
}