rosu_map/section/
mod.rs

1/// Types for the `[General]` section.
2pub mod general;
3
4/// Types for the `[Editor]` section.
5pub mod editor;
6
7/// Types for the `[Difficulty]` section.
8pub mod difficulty;
9
10/// Types for the `[Metadata]` section.
11pub mod metadata;
12
13/// Types for the `[Events]` section.
14pub mod events;
15
16/// Types for the `[TimingPoints]` section.
17pub mod timing_points;
18
19/// Types for the `[Colours]` section.
20pub mod colors;
21
22/// Types for the `[HitObjects]` section.
23pub mod hit_objects;
24
25/// All sections in a `.osu` file.
26#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
27pub enum Section {
28    General,
29    Editor,
30    Metadata,
31    Difficulty,
32    Events,
33    TimingPoints,
34    Colors,
35    HitObjects,
36    Variables,
37    CatchTheBeat,
38    Mania,
39}
40
41impl Section {
42    /// Try to parse a [`Section`].
43    pub fn try_from_line(line: &str) -> Option<Self> {
44        let section = line.strip_prefix('[')?.strip_suffix(']')?;
45
46        let section = match section {
47            "General" => Self::General,
48            "Editor" => Self::Editor,
49            "Metadata" => Self::Metadata,
50            "Difficulty" => Self::Difficulty,
51            "Events" => Self::Events,
52            "TimingPoints" => Self::TimingPoints,
53            "Colours" => Self::Colors,
54            "HitObjects" => Self::HitObjects,
55            "Variables" => Self::Variables,
56            "CatchTheBeat" => Self::CatchTheBeat,
57            "Mania" => Self::Mania,
58            _ => return None,
59        };
60
61        Some(section)
62    }
63}
64
65thiserror! {
66    #[error("unknown key")]
67    /// The error when failing to parse a section key.
68    #[derive(Debug)]
69    pub struct UnknownKeyError;
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn finds_valid_sections() {
78        assert_eq!(Section::try_from_line("[General]"), Some(Section::General));
79        assert_eq!(
80            Section::try_from_line("[Difficulty]"),
81            Some(Section::Difficulty)
82        );
83        assert_eq!(
84            Section::try_from_line("[HitObjects]"),
85            Some(Section::HitObjects)
86        );
87    }
88
89    #[test]
90    fn requires_brackets() {
91        assert_eq!(Section::try_from_line("General"), None);
92        assert_eq!(Section::try_from_line("[General"), None);
93        assert_eq!(Section::try_from_line("General]"), None);
94    }
95
96    #[test]
97    fn denies_invalid_sections() {
98        assert_eq!(Section::try_from_line("abc"), None);
99        assert_eq!(Section::try_from_line("HitObject"), None);
100    }
101}