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
115
116
117
118
119
120
121
122
123
124
125
126
use std::{error::Error, fmt};
use crate::consts::{CHOICE_MARKER, KNOT_MARKER, STICKY_CHOICE_MARKER};
#[derive(Debug)]
pub enum ParseError {
Empty,
KnotError(KnotError),
LineError(LineError),
}
impl Error for ParseError {}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ParseError::*;
match self {
Empty => write!(f, "Tried to read from an empty file or string"),
KnotError(err) => write!(f, "{}", err),
LineError(err) => write!(f, "{}", err),
}
}
}
impl From<KnotError> for ParseError {
fn from(err: KnotError) -> Self {
ParseError::KnotError(err)
}
}
impl From<LineError> for ParseError {
fn from(err: LineError) -> Self {
ParseError::LineError(err)
}
}
#[derive(Debug)]
pub enum KnotError {
Empty,
InvalidName { line: String, kind: KnotNameError },
}
impl fmt::Display for KnotError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use KnotError::*;
use KnotNameError::*;
write!(f, "Could not parse a knot: ")?;
match self {
Empty => write!(f, "knot has no name"),
InvalidName { line, kind } => {
write!(f, "could not read knot name: ")?;
match kind {
ContainsWhitespace => {
write!(f, "name contains whitespace characters")?;
}
CouldNotRead => {
write!(f, "line does not start with '{}'", KNOT_MARKER)?;
}
}
write!(f, " (line: {})", line)
}
}
}
}
#[derive(Debug)]
pub enum KnotNameError {
ContainsWhitespace,
CouldNotRead,
}
#[derive(Debug)]
pub enum LineError {
BadCondition {
condition: String,
full_line: String,
},
NoDisplayText,
MultipleChoiceType { line: String },
UnmatchedBrackets { line: String },
}
impl fmt::Display for LineError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use LineError::*;
write!(f, "Invalid line: ")?;
match self {
BadCondition {
condition,
full_line,
} => write!(
f,
"could not parse a condition from '{}' (full line: {})",
condition, full_line,
),
NoDisplayText => write!(
f,
"line has choice markers ({}, {}) but is empty",
CHOICE_MARKER, STICKY_CHOICE_MARKER
),
MultipleChoiceType { line } => write!(
f,
"line has multiple types of choice markers (line: {})",
line
),
UnmatchedBrackets { line } => write!(f, "line has unmatched brackets (line: {})", line),
}
}
}