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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Copyright (C) 2026 Michael Wilson <mike@mdwn.dev>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//
use std::error::Error;
use std::time::Duration;
use super::super::tempo::{
TempoChange, TempoChangePosition, TempoMap, TempoTransition, TimeSignature, TransitionCurve,
};
use super::grammar::Rule;
use super::utils::{parse_measure_time, parse_time_string};
use pest::iterators::Pair;
pub(crate) fn parse_tempo_definition(pair: Pair<Rule>) -> Result<TempoMap, Box<dyn Error>> {
let mut start_offset = Duration::ZERO;
let mut bpm = crate::lighting::tempo::DEFAULT_BPM;
let mut time_signature = TimeSignature::new(4, 4); // Default
let mut changes = Vec::new();
// tempo = { "tempo" ~ "{" ~ tempo_content ~ "}" }
// tempo_content = { (tempo_start | tempo_bpm | tempo_time_signature | tempo_changes)* }
// So we need to iterate through tempo_content
for inner_pair in pair.into_inner() {
if inner_pair.as_rule() == Rule::tempo_content {
// tempo_content contains tempo_start, tempo_bpm, etc.
for content_pair in inner_pair.into_inner() {
match content_pair.as_rule() {
Rule::tempo_start => {
// tempo_start = { "start" ~ ":" ~ time_parameter }
// time_parameter is atomic (@{ time_value ~ time_unit })
// Since it's atomic, iterate through inner pairs to find it
for param_pair in content_pair.into_inner() {
if param_pair.as_rule() == Rule::time_parameter {
let time_str = param_pair.as_str().trim();
if time_str.is_empty() {
continue;
}
start_offset = parse_time_parameter(time_str).map_err(|e| {
format!("Failed to parse time_parameter '{}': {}", time_str, e)
})?;
break;
}
}
}
Rule::tempo_bpm => {
for value_pair in content_pair.into_inner() {
if value_pair.as_rule() == Rule::number_value {
let bpm_str = value_pair.as_str().trim();
bpm = bpm_str.parse().map_err(|e| {
format!("Failed to parse BPM '{}': {}", bpm_str, e)
})?;
}
}
}
Rule::tempo_time_signature => {
for value_pair in content_pair.into_inner() {
if value_pair.as_rule() == Rule::time_sig_value {
let (num, den) = parse_time_signature(value_pair.as_str())?;
time_signature = TimeSignature::new(num, den);
}
}
}
Rule::tempo_changes => {
// tempo_changes = { "changes" ~ ":" ~ "[" ~ tempo_change_list? ~ "]" }
// tempo_change_list = { tempo_change ~ ("," ~ tempo_change)* }
// We need to find tempo_change_list, which is optional
for list_pair in content_pair.into_inner() {
if list_pair.as_rule() == Rule::tempo_change_list {
// tempo_change_list contains tempo_change pairs separated by commas
for change_pair in list_pair.into_inner() {
if change_pair.as_rule() == Rule::tempo_change {
let change = parse_tempo_change(change_pair)?;
changes.push(change);
}
// Skip comma tokens
}
}
}
}
_ => {}
}
}
}
}
Ok(TempoMap::new(start_offset, bpm, time_signature, changes))
}
pub(crate) fn parse_tempo_change(pair: Pair<Rule>) -> Result<TempoChange, Box<dyn Error>> {
let mut position = TempoChangePosition::Time(Duration::ZERO);
let mut bpm = None;
let mut time_signature = None;
let mut transition = TempoTransition::Snap;
for inner_pair in pair.into_inner() {
match inner_pair.as_rule() {
Rule::time_string => {
let time = parse_time_string(inner_pair.as_str())?;
position = TempoChangePosition::Time(time);
}
Rule::measure_time => {
let (measure, beat) = parse_measure_time(inner_pair.as_str())?;
position = TempoChangePosition::MeasureBeat(measure, beat);
}
Rule::tempo_change_content => {
for param_pair in inner_pair.into_inner() {
// tempo_change_content contains tempo_change_param pairs
// tempo_change_param is a wrapper, so we need to get its inner rule
// The inner rule will be one of: tempo_change_bpm, tempo_change_time_signature, tempo_change_transition
for actual_param in param_pair.into_inner() {
match actual_param.as_rule() {
Rule::tempo_change_bpm => {
// tempo_change_bpm = { "bpm" ~ ":" ~ number_value }
// So actual_param contains "bpm", ":", and number_value
// We need to find number_value
for value_pair in actual_param.into_inner() {
if value_pair.as_rule() == Rule::number_value {
let bpm_str = value_pair.as_str().trim();
let bpm_value = bpm_str.parse()?;
bpm = Some(bpm_value);
break;
}
}
}
Rule::tempo_change_time_signature => {
for value_pair in actual_param.into_inner() {
if value_pair.as_rule() == Rule::time_sig_value {
let (num, den) = parse_time_signature(value_pair.as_str())?;
time_signature = Some(TimeSignature::new(num, den));
}
}
}
Rule::tempo_change_transition => {
// tempo_change_transition = { "transition" ~ ":" ~ tempo_transition_duration }
// tempo_transition_duration = { tempo_transition_measures | tempo_transition_beats | tempo_transition_snap }
// actual_param is tempo_change_transition, which contains "transition", ":", and tempo_transition_duration
// We need to find tempo_transition_duration
for inner_pair in actual_param.into_inner() {
match inner_pair.as_rule() {
Rule::tempo_transition_duration => {
// tempo_transition_duration is an OR of the three options
for trans_pair in inner_pair.into_inner() {
match trans_pair.as_rule() {
Rule::tempo_transition_snap => {
transition = TempoTransition::Snap;
}
Rule::tempo_transition_beats => {
// tempo_transition_beats = { number_value }
// So trans_pair contains number_value as inner pair
for value_pair in trans_pair.into_inner() {
if value_pair.as_rule()
== Rule::number_value
{
let beats = value_pair
.as_str()
.trim()
.parse()?;
transition = TempoTransition::Beats(
beats,
TransitionCurve::Linear,
);
break;
}
}
}
Rule::tempo_transition_measures => {
// tempo_transition_measures is atomic, so we can get the string directly
let measure_str =
trans_pair.as_str().trim();
let num_str =
measure_str.trim_end_matches('m');
let measures = num_str.trim().parse()?;
transition = TempoTransition::Measures(
measures,
TransitionCurve::Linear,
);
}
_ => {}
}
}
}
_ => {
// Skip "transition" and ":" tokens
}
}
}
}
_ => {
// Skip unexpected rules
}
}
}
}
}
_ => {}
}
}
// Determine original measure/beat if position is MeasureBeat
let original_measure_beat = match position {
TempoChangePosition::MeasureBeat(m, b) => Some((m, b)),
TempoChangePosition::Time(_) => None,
};
Ok(TempoChange {
position,
original_measure_beat,
bpm,
time_signature,
transition,
})
}
pub(crate) fn parse_time_signature(value: &str) -> Result<(u32, u32), Box<dyn Error>> {
let value = value.trim();
let parts: Vec<&str> = value.split('/').collect();
if parts.len() != 2 {
return Err(format!("Invalid time signature format: {}", value).into());
}
let numerator: u32 = parts[0].trim().parse()?;
let denominator: u32 = parts[1].trim().parse()?;
Ok((numerator, denominator))
}
fn parse_time_parameter(value: &str) -> Result<Duration, Box<dyn Error>> {
let trimmed = value.trim();
if trimmed.ends_with("ms") {
let num_str = trimmed.trim_end_matches("ms").trim();
let num = num_str
.parse::<u64>()
.map_err(|e| format!("Failed to parse '{}' as u64: {}", num_str, e))?;
Ok(Duration::from_millis(num))
} else if trimmed.ends_with('s') {
let num_str = trimmed.trim_end_matches('s').trim();
let num = num_str
.parse::<f64>()
.map_err(|e| format!("Failed to parse '{}' as f64: {}", num_str, e))?;
Ok(Duration::from_secs_f64(num))
} else {
// Assume seconds if no unit
let num = trimmed
.parse::<f64>()
.map_err(|e| format!("Failed to parse '{}' as f64: {}", trimmed, e))?;
Ok(Duration::from_secs_f64(num))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::lighting::parser::grammar::{LightingParser, Rule};
use pest::Parser;
// ── parse_time_signature ─────────────────────────────────────
#[test]
fn time_signature_4_4() {
let (num, den) = parse_time_signature("4/4").unwrap();
assert_eq!((num, den), (4, 4));
}
#[test]
fn time_signature_3_4() {
let (num, den) = parse_time_signature("3/4").unwrap();
assert_eq!((num, den), (3, 4));
}
#[test]
fn time_signature_6_8() {
let (num, den) = parse_time_signature("6/8").unwrap();
assert_eq!((num, den), (6, 8));
}
#[test]
fn time_signature_5_4() {
let (num, den) = parse_time_signature("5/4").unwrap();
assert_eq!((num, den), (5, 4));
}
#[test]
fn time_signature_with_whitespace() {
let (num, den) = parse_time_signature(" 4/4 ").unwrap();
assert_eq!((num, den), (4, 4));
}
#[test]
fn time_signature_missing_slash() {
assert!(parse_time_signature("44").is_err());
}
#[test]
fn time_signature_too_many_parts() {
assert!(parse_time_signature("4/4/4").is_err());
}
#[test]
fn time_signature_non_numeric() {
assert!(parse_time_signature("abc/4").is_err());
}
#[test]
fn time_signature_empty() {
assert!(parse_time_signature("").is_err());
}
#[test]
fn time_signature_slash_only() {
assert!(parse_time_signature("/").is_err());
}
#[test]
fn time_signature_zero_numerator() {
// Parses successfully — semantic validation is separate
let (num, den) = parse_time_signature("0/4").unwrap();
assert_eq!((num, den), (0, 4));
}
// ── parse_time_parameter ─────────────────────────────────────
#[test]
fn time_parameter_milliseconds() {
let d = parse_time_parameter("500ms").unwrap();
assert_eq!(d, Duration::from_millis(500));
}
#[test]
fn time_parameter_milliseconds_zero() {
let d = parse_time_parameter("0ms").unwrap();
assert_eq!(d, Duration::from_millis(0));
}
#[test]
fn time_parameter_seconds() {
let d = parse_time_parameter("2.5s").unwrap();
assert_eq!(d, Duration::from_secs_f64(2.5));
}
#[test]
fn time_parameter_seconds_integer() {
let d = parse_time_parameter("3s").unwrap();
assert_eq!(d, Duration::from_secs(3));
}
#[test]
fn time_parameter_seconds_zero() {
let d = parse_time_parameter("0.0s").unwrap();
assert_eq!(d, Duration::ZERO);
}
#[test]
fn time_parameter_bare_number() {
let d = parse_time_parameter("1.5").unwrap();
assert_eq!(d, Duration::from_secs_f64(1.5));
}
#[test]
fn time_parameter_with_whitespace() {
let d = parse_time_parameter(" 100ms ").unwrap();
assert_eq!(d, Duration::from_millis(100));
}
#[test]
fn time_parameter_invalid_ms() {
assert!(parse_time_parameter("abcms").is_err());
}
#[test]
fn time_parameter_invalid_seconds() {
assert!(parse_time_parameter("abcs").is_err());
}
#[test]
fn time_parameter_invalid_bare() {
assert!(parse_time_parameter("xyz").is_err());
}
// ── parse_tempo_definition (via grammar) ─────────────────────
fn parse_tempo_from_dsl(content: &str) -> TempoMap {
let mut pairs = LightingParser::parse(Rule::tempo, content).unwrap();
let pair = pairs.next().unwrap();
parse_tempo_definition(pair).unwrap()
}
#[test]
fn tempo_definition_defaults() {
let tm = parse_tempo_from_dsl("tempo { }");
assert_eq!(
tm.bpm_at_time(Duration::ZERO, 0.0),
crate::lighting::tempo::DEFAULT_BPM
);
}
#[test]
fn tempo_definition_bpm_only() {
let tm = parse_tempo_from_dsl(
r#"tempo {
bpm: 140
}"#,
);
assert_eq!(tm.bpm_at_time(Duration::ZERO, 0.0), 140.0);
}
#[test]
fn tempo_definition_full() {
let tm = parse_tempo_from_dsl(
r#"tempo {
start: 500ms
bpm: 90
time_signature: 3/4
}"#,
);
assert_eq!(tm.bpm_at_time(Duration::from_millis(500), 0.0), 90.0);
}
#[test]
fn tempo_definition_with_changes() {
let content = r#"tempo {
bpm: 120
time_signature: 4/4
changes: [
@8/1 { bpm: 140, transition: snap }
]
}"#;
let tm = parse_tempo_from_dsl(content);
assert_eq!(tm.bpm_at_time(Duration::ZERO, 0.0), 120.0);
}
#[test]
fn tempo_definition_empty_changes() {
let content = r#"tempo {
bpm: 120
time_signature: 4/4
changes: []
}"#;
let tm = parse_tempo_from_dsl(content);
assert_eq!(tm.bpm_at_time(Duration::ZERO, 0.0), 120.0);
}
// ── parse_tempo_change (via grammar) ─────────────────────────
fn parse_change_from_dsl(change_str: &str) -> TempoChange {
let mut pairs = LightingParser::parse(Rule::tempo_change, change_str).unwrap();
let pair = pairs.next().unwrap();
parse_tempo_change(pair).unwrap()
}
#[test]
fn tempo_change_bpm_snap() {
let change = parse_change_from_dsl("@8/1 { bpm: 140, transition: snap }");
assert_eq!(change.bpm, Some(140.0));
assert!(matches!(change.transition, TempoTransition::Snap));
assert_eq!(change.original_measure_beat, Some((8, 1.0)));
}
#[test]
fn tempo_change_time_signature() {
let change = parse_change_from_dsl("@16/1 { time_signature: 6/8 }");
assert!(change.time_signature.is_some());
assert_eq!(change.original_measure_beat, Some((16, 1.0)));
}
#[test]
fn tempo_change_beat_transition() {
let change = parse_change_from_dsl("@8/1 { bpm: 140, transition: 2.5 }");
assert_eq!(change.bpm, Some(140.0));
assert!(matches!(change.transition, TempoTransition::Beats(..)));
}
#[test]
fn tempo_change_measure_transition() {
let change = parse_change_from_dsl("@8/1 { bpm: 160, transition: 1.5m }");
assert_eq!(change.bpm, Some(160.0));
assert!(matches!(change.transition, TempoTransition::Measures(..)));
}
#[test]
fn tempo_change_absolute_time() {
let change = parse_change_from_dsl("@00:30.000 { bpm: 140 }");
assert_eq!(change.bpm, Some(140.0));
assert_eq!(change.original_measure_beat, None);
assert!(matches!(
change.position,
TempoChangePosition::Time(d) if d == Duration::from_secs(30)
));
}
#[test]
fn tempo_change_ss_mmm_time() {
let change = parse_change_from_dsl("@45.500 { bpm: 160 }");
assert_eq!(change.bpm, Some(160.0));
assert!(matches!(
change.position,
TempoChangePosition::Time(d) if d == Duration::from_secs_f64(45.5)
));
}
}