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
use std::fmt::Display;

use nom::{
    branch::alt,
    bytes::complete::tag,
    character::complete::{multispace0, multispace1, space1},
    combinator::{map, opt},
    multi::separated_list1,
    sequence::{delimited, preceded, tuple},
    IResult,
};

#[derive(Debug)]
pub enum SyncTrackEvent {
    Bpm {
        time: u32,
        value: u32,
    },
    TimeSignature {
        time: u32,
        value1: u32,
        value2: Option<u32>,
    },
    Anchor {
        time: u32,
        value: u32,
    },
}

impl Display for SyncTrackEvent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SyncTrackEvent::Bpm { time, value } => writeln!(f, "  {time} = B {value}"),
            SyncTrackEvent::TimeSignature {
                time,
                value1,
                value2: Some(value2),
            } => writeln!(f, "  {time} = TS {value1} {value2}"),
            SyncTrackEvent::TimeSignature {
                time,
                value1,
                value2: None,
            } => writeln!(f, "  {time} = TS {value1}"),
            SyncTrackEvent::Anchor { time, value } => writeln!(f, "  {time} = A {value}"),
        }
    }
}

impl SyncTrackEvent {
    pub fn multiply(&mut self, factor: u32) {
        match self {
            SyncTrackEvent::Bpm { time, .. }
            | SyncTrackEvent::TimeSignature { time, .. }
            | SyncTrackEvent::Anchor { time, .. } => *time *= factor,
        }
    }

    pub fn parse(input: &str) -> IResult<&str, SyncTrackEvent> {
        let (input, time) = nom::character::complete::u32(input)?;
        let (input, _) = tag(" = ")(input)?;
        let (input, result) = alt((
            map(
                tuple((
                    tag("TS"),
                    preceded(multispace1, nom::character::complete::u32),
                    opt(preceded(space1, nom::character::complete::u32)),
                )),
                |(_, value1, value2)| SyncTrackEvent::TimeSignature {
                    time,
                    value1,
                    value2,
                },
            ),
            map(
                tuple((
                    tag("B"),
                    preceded(multispace1, nom::character::complete::u32),
                )),
                |(_, value)| SyncTrackEvent::Bpm { time, value },
            ),
            map(
                tuple((
                    tag("A"),
                    preceded(multispace1, nom::character::complete::u32),
                )),
                |(_, value)| SyncTrackEvent::Anchor { time, value },
            ),
        ))(input)?;
        Ok((input, result))
    }

    pub fn parse_section(input: &str) -> IResult<&str, Vec<SyncTrackEvent>> {
        preceded(
            preceded(tag("[SyncTrack]"), multispace0),
            delimited(
                preceded(tag("{"), multispace0),
                separated_list1(multispace1, SyncTrackEvent::parse),
                preceded(multispace0, tag("}")),
            ),
        )(input)
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::*;

    #[test]
    fn test_sync_track_event() {
        SyncTrackEvent::parse("0 = TS 6").unwrap();
        SyncTrackEvent::parse("0 = B 152525").unwrap();
    }

    #[test]
    fn test_sync_track() {
        SyncTrackEvent::parse_section(
            "[SyncTrack]
{
  0 = TS 6
  0 = B 152525
  1152 = TS 4
  4224 = B 160187
  10368 = B 160000
  154752 = B 158662
  156288 = B 180000
  168576 = B 160000
  173184 = B 160866
  174720 = B 160000
}",
        )
        .unwrap();
    }
}