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
/// Types for the `[General]` section.
pub mod general;

/// Types for the `[Editor]` section.
pub mod editor;

/// Types for the `[Difficulty]` section.
pub mod difficulty;

/// Types for the `[Metadata]` section.
pub mod metadata;

/// Types for the `[Events]` section.
pub mod events;

/// Types for the `[TimingPoints]` section.
pub mod timing_points;

/// Types for the `[Colours]` section.
pub mod colors;

/// Types for the `[HitObjects]` section.
pub mod hit_objects;

/// All sections in a `.osu` file.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Section {
    General,
    Editor,
    Metadata,
    Difficulty,
    Events,
    TimingPoints,
    Colors,
    HitObjects,
    Variables,
    CatchTheBeat,
    Mania,
}

impl Section {
    /// Try to parse a [`Section`].
    pub fn try_from_line(line: &str) -> Option<Self> {
        let section = line.strip_prefix('[')?.strip_suffix(']')?;

        let section = match section {
            "General" => Self::General,
            "Editor" => Self::Editor,
            "Metadata" => Self::Metadata,
            "Difficulty" => Self::Difficulty,
            "Events" => Self::Events,
            "TimingPoints" => Self::TimingPoints,
            "Colours" => Self::Colors,
            "HitObjects" => Self::HitObjects,
            "Variables" => Self::Variables,
            "CatchTheBeat" => Self::CatchTheBeat,
            "Mania" => Self::Mania,
            _ => return None,
        };

        Some(section)
    }
}

thiserror! {
    #[error("unknown key")]
    /// The error when failing to parse a section key.
    #[derive(Debug)]
    pub struct UnknownKeyError;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn finds_valid_sections() {
        assert_eq!(Section::try_from_line("[General]"), Some(Section::General));
        assert_eq!(
            Section::try_from_line("[Difficulty]"),
            Some(Section::Difficulty)
        );
        assert_eq!(
            Section::try_from_line("[HitObjects]"),
            Some(Section::HitObjects)
        );
    }

    #[test]
    fn requires_brackets() {
        assert_eq!(Section::try_from_line("General"), None);
        assert_eq!(Section::try_from_line("[General"), None);
        assert_eq!(Section::try_from_line("General]"), None);
    }

    #[test]
    fn denies_invalid_sections() {
        assert_eq!(Section::try_from_line("abc"), None);
        assert_eq!(Section::try_from_line("HitObject"), None);
    }
}