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

use nom::{
    bytes::complete::take_until,
    character::complete::{multispace0, multispace1},
    multi::separated_list1,
    IResult,
};

use crate::{
    global_event::GlobalEvent, song::Song, sync_track_event::SyncTrackEvent, track::Track,
};

#[derive(Debug)]
pub struct Chart<'a> {
    song: Song<'a>,
    synctrack: Vec<SyncTrackEvent>,
    global_events: Vec<GlobalEvent<'a>>,
    tracks: Vec<Track<'a>>,
}

impl<'a> Chart<'a> {
    pub fn new(
        song: Song<'a>,
        synctrack: Vec<SyncTrackEvent>,
        global_events: Vec<GlobalEvent<'a>>,
        tracks: Vec<Track<'a>>,
    ) -> Self {
        Self {
            song,
            synctrack,
            global_events,
            tracks,
        }
    }

    pub fn multiply(&mut self, factor: u32) {
        self.song.multiply(factor);
        for item in &mut self.synctrack {
            item.multiply(factor);
        }
        for item in &mut self.global_events {
            item.multiply(factor);
        }
        for item in &mut self.tracks {
            item.multiply(factor);
        }
    }
}

impl<'a> Display for Chart<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "[Song]
{{
{}}}
[SyncTrack]
{{
{}}}
[Events]
{{
{}}}
{}",
            self.song,
            self.synctrack
                .iter()
                .map(SyncTrackEvent::to_string)
                .collect::<String>(),
            self.global_events
                .iter()
                .map(GlobalEvent::to_string)
                .collect::<String>(),
            self.tracks.iter().map(Track::to_string).collect::<String>()
        )
    }
}

pub fn chart(input: &str) -> IResult<&str, Chart> {
    let (input, _) = take_until("[")(input)?;
    let (input, song) = Song::parse(input)?;
    let (input, _) = multispace0(input)?;
    let (input, synctrack) = SyncTrackEvent::parse_section(input)?;
    let (input, _) = multispace0(input)?;
    let (input, global_events) = GlobalEvent::parse_section(input)?;
    let (input, _) = multispace0(input)?;
    let (input, tracks) = separated_list1(multispace1, Track::parse)(input)?;
    Ok((input, Chart::new(song, synctrack, global_events, tracks)))
}