rosu_map/section/events/
mod.rs

1use std::str::FromStr;
2
3pub use self::decode::{Events, EventsState, ParseEventsError};
4
5pub(crate) mod decode; // pub(crate) for intradoc-links
6
7/// A break section during a [`Beatmap`].
8///
9/// [`Beatmap`]: crate::beatmap::Beatmap
10#[derive(Copy, Clone, Debug, Default, PartialEq)]
11pub struct BreakPeriod {
12    pub start_time: f64,
13    pub end_time: f64,
14}
15
16impl BreakPeriod {
17    /// The minimum duration required for a break to have any effect.
18    pub const MIN_BREAK_DURATION: f64 = 650.0;
19
20    /// The duration of the break.
21    pub fn duration(&self) -> f64 {
22        self.end_time - self.start_time
23    }
24
25    /// Whether the break has any effect.
26    pub fn has_effect(&self) -> bool {
27        self.duration() >= Self::MIN_BREAK_DURATION
28    }
29}
30
31/// The type of an event.
32#[derive(Copy, Clone, Debug, PartialEq, Eq)]
33pub enum EventType {
34    Background,
35    Video,
36    Break,
37    Color,
38    Sprite,
39    Sample,
40    Animation,
41}
42
43impl FromStr for EventType {
44    type Err = ParseEventTypeError;
45
46    fn from_str(s: &str) -> Result<Self, Self::Err> {
47        match s {
48            "0" | "Background" => Ok(Self::Background),
49            "1" | "Video" => Ok(Self::Video),
50            "2" | "Break" => Ok(Self::Break),
51            "3" | "Colour" => Ok(Self::Color),
52            "4" | "Sprite" => Ok(Self::Sprite),
53            "5" | "Sample" => Ok(Self::Sample),
54            "6" | "Animation" => Ok(Self::Animation),
55            _ => Err(ParseEventTypeError),
56        }
57    }
58}
59
60thiserror! {
61    #[error("invalid event type")]
62    /// Error when failing to parse an [`EventType`].
63    #[derive(Debug)]
64    pub struct ParseEventTypeError;
65}