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
use std::{num::ParseIntError, str::FromStr};

use crate::{
    decode::{DecodeBeatmap, DecodeState},
    section::UnknownKeyError,
    util::{KeyValue, ParseNumberError, StrExt},
    Beatmap,
};

use super::{Color, CustomColor};

/// Struct containing all data from a `.osu` file's `[Colours]` section.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Colors {
    pub custom_combo_colors: Vec<Color>,
    pub custom_colors: Vec<CustomColor>,
}

impl From<Colors> for Beatmap {
    fn from(colors: Colors) -> Self {
        Self {
            custom_combo_colors: colors.custom_combo_colors,
            custom_colors: colors.custom_colors,
            ..Self::default()
        }
    }
}

impl Colors {
    pub const DEFAULT_COMBO_COLORS: [Color; 4] = [
        Color([255, 192, 0, 255]),
        Color([0, 202, 0, 255]),
        Color([18, 124, 255, 255]),
        Color([242, 24, 57, 255]),
    ];
}

/// All valid keys within a `.osu` file's `[Colours]` section
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ColorsKey {
    Combo,
    Name(String),
}

impl FromStr for ColorsKey {
    type Err = UnknownKeyError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.starts_with("Combo") {
            Ok(Self::Combo)
        } else {
            Ok(Self::Name(s.to_owned()))
        }
    }
}

thiserror! {
    /// All the ways that parsing a `.osu` file into [`Colors`] can fail.
    #[derive(Debug)]
    pub enum ParseColorsError {
        #[error("color specified in incorrect format (should be R,G,B or R,G,B,A)")]
        IncorrectColor,
        #[error("failed to parse number")]
        Number(#[source] ParseNumberError),
    }
}

impl From<ParseIntError> for ParseColorsError {
    fn from(err: ParseIntError) -> Self {
        Self::Number(ParseNumberError::InvalidInteger(err))
    }
}

/// The parsing state for [`Colors`] in [`DecodeBeatmap`].
pub type ColorsState = Colors;

impl DecodeState for ColorsState {
    fn create(_version: i32) -> Self {
        Self::default()
    }
}

impl DecodeBeatmap for Colors {
    type Error = ParseColorsError;
    type State = ColorsState;

    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(_: &mut Self::State, _: &str) -> Result<(), Self::Error> {
        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(state: &mut Self::State, line: &str) -> Result<(), Self::Error> {
        let Ok(KeyValue { key, value }) = KeyValue::parse(line.trim_comment()) else {
            return Ok(());
        };

        let color: Color = value.parse()?;

        match key {
            ColorsKey::Combo => state.custom_combo_colors.push(color),
            ColorsKey::Name(name) => {
                match state.custom_colors.iter_mut().find(|c| c.name == name) {
                    Some(old) => old.color = color,
                    None => state.custom_colors.push(CustomColor { name, color }),
                }
            }
        }

        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(())
    }
}