osu_file_parser/osu_file/editor/
mod.rs

1pub mod error;
2
3use std::num::{IntErrorKind, ParseIntError};
4
5use nom::{bytes::complete::take_till, combinator::map_res, multi::separated_list0, Finish};
6use crate::osu_file::types::Decimal;
7
8use crate::parsers::comma;
9
10use super::Integer;
11use crate::helper::macros::*;
12
13pub use error::*;
14
15versioned_field!(Bookmarks, Vec<Integer>, no_versions, |s| {
16    let bookmark = map_res(take_till(|c| c == ','), |s: &str| s.parse::<Integer>());
17    let mut bookmarks = separated_list0(comma::<nom::error::Error<_>>(), bookmark);
18    let input_len = s.len();
19
20    let (s, bookmarks) = bookmarks(s).finish().unwrap();
21
22    if s.is_empty() {
23        Ok(bookmarks)
24    } else {
25        let (_, s) = {
26            let s = if s.len() < input_len {
27                match s.strip_prefix(',') {
28                    Some(s) => s,
29                    None => s,
30                }
31            } else {
32                s
33            };
34
35            take_till::<_, _, nom::error::Error<_>>(|c| c == ',')(s).unwrap()
36        };
37    
38        // re-parse to get error
39        let err = s.parse::<Integer>().unwrap_err();
40
41        let err = if let IntErrorKind::Empty = err.kind() {
42            ParseError::InvalidCommaList
43        } else {
44            ParseError::ParseIntError(err)
45        };
46
47        Err(err)
48    }
49} -> ParseError, |v| { v.iter().map(|v| v.to_string())
50    .collect::<Vec<_>>().join(",") },);
51versioned_field!(DistanceSpacing, Decimal, no_versions, |s| { s.parse() } -> (),,);
52versioned_field!(BeatDivisor, Decimal, no_versions, |s| { s.parse() } -> (),,);
53versioned_field!(GridSize, Integer, no_versions, |s| { s.parse() } -> ParseIntError,,);
54versioned_field!(TimelineZoom, Decimal, no_versions, |s| { s.parse() } -> (),,);
55versioned_field!(CurrentTime, Integer, no_versions, |s| { s.parse() } -> ParseIntError,
56|s, version| {
57    if version != 10 {
58        None
59    } else {
60        Some(s.to_string())
61    }
62}
63,);
64
65general_section!(
66    /// A struct representing the editor section of the .osu file.
67    pub struct Editor {
68        /// Time in milliseconds of bookmarks.
69        pub bookmarks: Bookmarks,
70        /// Distance snap multiplier.
71        pub distance_spacing: DistanceSpacing,
72        /// Beat snap divisor.
73        pub beat_divisor: BeatDivisor,
74        /// Grid size.
75        pub grid_size: GridSize,
76        /// Scale factor for the objecct timeline.
77        pub timeline_zoom: TimelineZoom,
78        /// Deprecated.
79        pub current_time: CurrentTime,
80    },
81    ParseError,
82    " ",
83);