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
use itertools::Itertools;
use crate::{place_not::PnBlockParseError, Block, PnBlock, Row, RowBuf, Stage};
use self::class::FullClass;
pub mod class;
pub const LABEL_LEAD_END: &str = "LE";
#[derive(Debug, Clone)]
pub struct Method {
title: String,
name: String,
class: FullClass,
first_lead: Block<Option<String>>,
}
impl Method {
pub fn new(
title: String,
name: String,
class: FullClass,
first_lead: Block<Option<String>>,
) -> Self {
Self {
title,
name,
class,
first_lead,
}
}
pub fn with_name(name: String, first_lead: Block<Option<String>>) -> Self {
let class = FullClass::classify(&first_lead);
Self {
title: generate_title(&name, class, first_lead.stage()),
name,
class,
first_lead,
}
}
pub fn from_place_not_string(
name: String,
stage: Stage,
place_notation: &str,
) -> Result<Self, PnBlockParseError> {
Ok(Self::with_name(
name,
PnBlock::parse(place_notation, stage)?.to_block_from_rounds(),
))
}
pub fn with_lead_end(name: String, block: &PnBlock) -> Self {
let mut first_lead: Block<Option<String>> = block.to_block_from_rounds();
*first_lead.get_annot_mut(first_lead.len() - 1).unwrap() = Some(LABEL_LEAD_END.to_owned());
Self::with_name(name, first_lead)
}
#[deprecated(note = "Use `Method::first_lead` instead`")]
#[inline]
pub fn lead(&self) -> &Block<Option<String>> {
&self.first_lead
}
#[inline]
pub fn first_lead(&self) -> &Block<Option<String>> {
&self.first_lead
}
#[inline]
pub fn lead_head(&self) -> &Row {
self.first_lead.leftover_row()
}
#[inline]
pub fn lead_len(&self) -> usize {
self.first_lead.len()
}
#[inline]
pub fn stage(&self) -> Stage {
self.first_lead.stage()
}
#[inline]
pub fn class(&self) -> FullClass {
self.class
}
#[inline]
pub fn name(&self) -> &str {
&self.name
}
pub fn title(&self) -> &str {
self.title.as_ref()
}
pub fn row_in_plain_lead(&self, idx: usize) -> &Row {
&self.first_lead.row_vec()[idx]
}
pub fn row_in_plain_course(&self, idx: usize) -> RowBuf {
let num_leads = idx / self.lead_len();
let sub_lead_idx = idx % self.lead_len();
let lead_head = self.lead_head().pow_u(num_leads);
let row_within_lead = self.row_in_plain_lead(sub_lead_idx);
lead_head.as_row() * row_within_lead
}
pub fn plain_course(&self) -> Block<RowAnnot> {
let first_lead_with_indices = self
.first_lead
.clone_map_annots_with_index(|i, label| RowAnnot::new(i, label.as_deref()));
let mut plain_course = first_lead_with_indices;
while !plain_course.leftover_row().is_rounds() {
plain_course.extend_from_within(0..self.lead_len());
}
plain_course
}
pub fn set_label(&mut self, index: usize, label: Option<String>) {
*self.first_lead.get_annot_mut(index).unwrap() = label;
}
pub fn set_lead_end_label(&mut self) {
self.set_label(0, Some(LABEL_LEAD_END.to_owned()))
}
pub fn get_label(&self, index: usize) -> Option<&str> {
self.first_lead
.get_annot(index)
.unwrap()
.as_ref()
.map(String::as_str)
}
pub fn label_indices<'s>(&'s self, label: &'s str) -> impl Iterator<Item = usize> + 's {
self.first_lead
.annots()
.positions(move |l| l.as_ref().map(String::as_str) == Some(label))
}
}
pub fn generate_title(name: &str, class: FullClass, stage: Stage) -> String {
let mut s = String::new();
s.push_str(name);
if !name.is_empty() {
s.push(' ');
}
let len_before_class = s.len();
class.fmt_name(&mut s, true).unwrap();
if s.len() > len_before_class {
s.push(' ');
}
s.push_str(stage.name().expect("Stage was too big to generate a name"));
s
}
#[derive(Debug, Clone)]
pub struct RowAnnot<'meth> {
sub_lead_idx: usize,
label: Option<&'meth str>,
}
impl<'meth> RowAnnot<'meth> {
fn new(sub_lead_idx: usize, label: Option<&'meth str>) -> Self {
Self {
sub_lead_idx,
label,
}
}
pub fn sub_lead_idx(&self) -> usize {
self.sub_lead_idx
}
pub fn label(&self) -> Option<&'meth str> {
self.label
}
}