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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Error types for the different ways that Monument can fail.
use std::{
fmt::{Display, Formatter},
ops::RangeInclusive,
};
use bellframe::{Mask, PlaceNot, RowBuf, Stage};
use crate::builder::OptionalRangeInclusive;
#[allow(unused_imports)] // Only used for doc comments
use crate::builder::{Call, Method, MusicType, SearchBuilder};
/// Alias for `Result<T, monument::Error>`.
pub type Result<T> = std::result::Result<T, Error>;
/// The different ways that Monument can fail.
#[derive(Debug)]
pub enum Error {
/* QUERY BUILD ERRORS */
/// The given `title` couldn't be found in the Central Council library. Each `suggestions` is
/// paired with its 'edit distance' (i.e. a metric of how different it is from the requested
/// `title`)
MethodNotFound {
title: String,
suggestions: Vec<(String, usize)>,
},
/// Some method's place notation failed to parse
MethodPnParse {
name: String,
place_notation_string: String,
error: bellframe::place_not::PnBlockParseError,
},
/// Some course mask couldn't be parsed.
CustomCourseMaskParse {
method_title: String,
mask_str: String,
error: bellframe::mask::ParseError,
},
/* QUERY VERIFICATION ERRORS */
/// Different start/end rows were specified in a multi-part
DifferentStartEndRowInMultipart,
/// Some [`Call`] refers to a label that doesn't exist
UndefinedLabel { call_name: String, label: String },
/// The [`SearchBuilder`] didn't define any [`Method`]s
NoMethods,
/// Two [`Method`]s use the same shorthand
DuplicateShorthand {
shorthand: String,
title1: String,
title2: String,
},
NoCourseHeadInPart {
mask_in_first_part: Mask,
part_head: RowBuf,
mask_in_other_part: Mask,
},
/// Some [`Call`] doesn't have enough calling positions to cover the [`Stage`]
WrongCallingPositionsLength {
call_name: String,
calling_position_len: usize,
stage: Stage,
},
/// Two [`Call`]s have the same lead location and name
DuplicateCall {
symbol: String,
label: String,
pn1: PlaceNot,
pn2: PlaceNot,
},
/* GRAPH BUILD ERRORS */
/// The given maximum graph size limit was reached
SizeLimit(usize),
/// The same chunk of ringing could start at two different strokes, and some
/// [`MusicType`] relies on the strokes always being the same
InconsistentStroke,
/* LENGTH PROVING ERRORS */
/// The requested length range isn't achievable
UnachievableLength {
requested_range: RangeInclusive<usize>,
next_shorter_len: Option<usize>,
next_longer_len: Option<usize>,
},
/// Some method range isn't achievable
UnachievableMethodCount {
method_name: String,
requested_range: OptionalRangeInclusive,
next_shorter_len: Option<usize>,
next_longer_len: Option<usize>,
},
/// The total of the minimum method counts is longer than the composition
TooMuchMethodCount {
min_total_method_count: usize,
max_length: usize,
},
TooLittleMethodCount {
max_total_method_count: usize,
min_length: usize,
},
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
/* QUERY BUILD ERRORS */
Error::MethodNotFound { title, suggestions } => write!(
f,
"No method called {:?} in the Central Council library. Do you mean {:?}?",
title, suggestions[0]
),
Error::MethodPnParse {
name,
place_notation_string: _,
error,
} => write!(f, "Error parsing place notation for {:?}: {}", name, error),
Error::CustomCourseMaskParse {
method_title,
mask_str,
error,
} => write!(
f,
"Error parsing course mask {} for method {:?}: {}",
mask_str, method_title, error
),
/* QUERY VERIFICATION ERRORS */
Error::DifferentStartEndRowInMultipart => {
write!(f, "Start/end rows must be the same for multipart comps")
}
Error::NoMethods => write!(f, "Can't have a composition with no methods"),
Error::WrongCallingPositionsLength {
call_name,
calling_position_len,
stage,
} => write!(
f,
"Call {:?} only specifies {} calling positions, but the stage has {} bells",
call_name,
calling_position_len,
stage.num_bells()
),
Error::DuplicateShorthand {
shorthand,
title1,
title2,
} => write!(
f,
"Methods {:?} and {:?} share a shorthand ({})",
title1, title2, shorthand
),
Error::UndefinedLabel { call_name, label } => write!(
f,
"Call {:?} refers to a label {:?}, which doesn't exist",
call_name, label
), // TODO: Suggest one that does exist
Error::NoCourseHeadInPart {
mask_in_first_part,
part_head,
mask_in_other_part,
} => {
writeln!(
f,
"course head `{}` becomes `{}` in the part starting `{}`, which isn't in `course_heads`.",
mask_in_first_part, mask_in_other_part, part_head
)?;
write!(
f,
" help: consider adding `{}` to `course_heads`",
mask_in_other_part
)
}
Error::DuplicateCall {
symbol,
label,
pn1,
pn2,
} => write!(
f,
"Call symbol {:?} (at {:?}) is used for both {} and {}",
symbol, label, pn1, pn2
),
/* GRAPH BUILD ERRORS */
Error::SizeLimit(limit) => write!(
f,
"Graph size limit of {} chunks reached. You can set it \
higher with `--graph-size-limit <n>`.",
limit
),
Error::InconsistentStroke => write!(
f,
"The same chunk of ringing can be at multiple strokes, probably \
because you're using a method with odd-length leads"
),
/* LENGTH PROVING ERRORS */
Error::UnachievableLength {
requested_range,
next_shorter_len,
next_longer_len,
} => {
write!(f, "No compositions can fit the required length range (")?;
write_range(
f,
"length",
Some(*requested_range.start()),
Some(*requested_range.end()),
)?;
write!(f, "). ")?;
// Describe the nearest composition length(s)
match (next_shorter_len, next_longer_len) {
(Some(l1), Some(l2)) => write!(f, "The nearest lengths are {l1} and {l2}."),
(Some(l), None) | (None, Some(l)) => write!(f, "The nearest length is {l}."),
// TODO: Give this its own error?
(None, None) => write!(f, "No compositions are possible."),
}
}
Error::UnachievableMethodCount {
method_name,
requested_range,
next_shorter_len,
next_longer_len,
} => {
assert_ne!((requested_range.min, requested_range.max), (None, None));
write!(
f,
"No method counts for {:?} satisfy the requested range (",
method_name,
)?;
write_range(f, "count", requested_range.min, requested_range.max)?;
write!(f, "). ")?;
// Describe the nearest method counts
match (next_shorter_len, next_longer_len) {
(Some(l1), Some(l2)) => write!(f, "The nearest counts are {l1} and {l2}."),
(Some(l), None) | (None, Some(l)) => write!(f, "The nearest count is {l}."),
(None, None) => unreachable!(), // Method count of 0 is always possible
}
}
Error::TooMuchMethodCount {
min_total_method_count,
max_length,
} => {
write!(f, "Too much method counts; the method counts need at least")?;
write!(
f,
" {min_total_method_count} rows, but at most {max_length} rows are available."
)
}
Error::TooLittleMethodCount {
max_total_method_count,
min_length,
} => {
write!(
f,
"Not enough method counts; the composition needs at least {min_length} rows"
)?;
write!(
f,
" but the methods can make at most {max_total_method_count}."
)
}
}
}
}
/// Prettily format a (possibly open) inclusive range as an inequality (e.g. `300 <= count <= 500`)
fn write_range<T: Ord + Display>(
f: &mut impl std::fmt::Write,
name: &str,
min: Option<T>,
max: Option<T>,
) -> std::fmt::Result {
match (min, max) {
// Write e.g. `224 <= count <= 224` as `count == 224`
(Some(min), Some(max)) if min == max => write!(f, "{name} == {min}")?,
// Otherwise write everything as an inequality
(min, max) => {
if let Some(min) = min {
write!(f, "{min} <= ")?;
}
write!(f, "{name}")?;
if let Some(max) = max {
write!(f, " <= {max}")?;
}
}
}
Ok(())
}
impl std::error::Error for Error {}