1pub mod general;
3
4pub mod editor;
6
7pub mod difficulty;
9
10pub mod metadata;
12
13pub mod events;
15
16pub mod timing_points;
18
19pub mod colors;
21
22pub mod hit_objects;
24
25#[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 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 #[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}