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
use crate::{
    decode::{DecodeBeatmap, DecodeState},
    util::{KeyValue, ParseNumberError, StrExt},
    Beatmap,
};

/// Struct containing all data from a `.osu` file's `[Metadata]` section.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Metadata {
    pub title: String,
    pub title_unicode: String,
    pub artist: String,
    pub artist_unicode: String,
    pub creator: String,
    pub version: String,
    pub source: String,
    pub tags: String,
    pub beatmap_id: i32,
    pub beatmap_set_id: i32,
}

impl Default for Metadata {
    #[allow(clippy::default_trait_access)]
    fn default() -> Self {
        Self {
            title: Default::default(),
            title_unicode: Default::default(),
            artist: Default::default(),
            artist_unicode: Default::default(),
            creator: Default::default(),
            version: Default::default(),
            source: Default::default(),
            tags: Default::default(),
            beatmap_id: -1,
            beatmap_set_id: Default::default(),
        }
    }
}

impl From<Metadata> for Beatmap {
    fn from(metadata: Metadata) -> Self {
        Self {
            title: metadata.title,
            title_unicode: metadata.title_unicode,
            artist: metadata.artist,
            artist_unicode: metadata.artist_unicode,
            creator: metadata.creator,
            version: metadata.version,
            source: metadata.source,
            tags: metadata.tags,
            beatmap_id: metadata.beatmap_id,
            beatmap_set_id: metadata.beatmap_set_id,
            ..Self::default()
        }
    }
}

section_keys! {
    /// All valid keys within a `.osu` file's `[Metadata]` section
    pub enum MetadataKey {
        Title,
        TitleUnicode,
        Artist,
        ArtistUnicode,
        Creator,
        Version,
        Source,
        Tags,
        BeatmapID,
        BeatmapSetID,
    }
}

thiserror! {
    /// All the ways that parsing a `.osu` file into [`Metadata`] can fail.
    #[derive(Debug)]
    pub enum ParseMetadataError {
        #[error("failed to parse number")]
        Number(#[from] ParseNumberError),
    }
}

/// The parsing state for [`Metadata`] in [`DecodeBeatmap`].
pub type MetadataState = Metadata;

impl DecodeState for MetadataState {
    fn create(_: i32) -> Self {
        Self::default()
    }
}

impl DecodeBeatmap for Metadata {
    type Error = ParseMetadataError;
    type State = Self;

    fn parse_general(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn parse_editor(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn parse_metadata(state: &mut Self::State, line: &str) -> Result<(), Self::Error> {
        let Ok(KeyValue { key, value }) = KeyValue::parse(line) else {
            return Ok(());
        };

        match key {
            MetadataKey::Title => state.title = value.to_owned(),
            MetadataKey::TitleUnicode => state.title_unicode = value.to_owned(),
            MetadataKey::Artist => state.artist = value.to_owned(),
            MetadataKey::ArtistUnicode => state.artist_unicode = value.to_owned(),
            MetadataKey::Creator => state.creator = value.to_owned(),
            MetadataKey::Version => state.version = value.to_owned(),
            MetadataKey::Source => state.source = value.to_owned(),
            MetadataKey::Tags => state.tags = value.to_owned(),
            MetadataKey::BeatmapID => state.beatmap_id = value.parse_num()?,
            MetadataKey::BeatmapSetID => state.beatmap_set_id = value.parse_num()?,
        }

        Ok(())
    }

    fn parse_difficulty(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn parse_events(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn parse_timing_points(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn parse_colors(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn parse_hit_objects(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn parse_variables(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn parse_catch_the_beat(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn parse_mania(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        Ok(())
    }
}