Documentation
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
// For format description, see:
// - https://bsmg.wiki/mapping/map-format.html
// - https://github.com/Kylemc1413/SongCore/blob/master/README.md
// TODO: use &refs in #[derive(Deserialize)] structs instead of owned types
#![allow(non_camel_case_types)]

use std::fmt;
use std::ops::Range;
use std::result::{Result as result_Result};
use std::sync::Arc;

use serde::{Deserialize, Deserializer};
use serde::de::{Error as de_Error, Visitor};
use serde_json::{Error as json_Error, Value};

use crate::asset::AssetManagerRc;
use crate::model::Color;

type Result<T> = result_Result<T, Error>;

#[derive(Debug)]
pub enum Error {
    ParseError(json_Error),
    BuildError(String),
}

impl From<json_Error> for Error {
    fn from(value: json_Error) -> Self {
        Error::ParseError(value)
    }
}

// SongInfo

pub struct SongInfo {
    asset_mgr: AssetManagerRc,
    dir: String,
    author: String,
    title: String,
    song_filename: String,
    bpm_selector: BPMSelector,
    color_schemes: Box<[ColorScheme]>,
    beatmap_infos: Box<[BeatmapInfo]>,
}

impl SongInfo {
    pub fn load<S: AsRef<str>>(asset_mgr: AssetManagerRc, dir: S) -> Result<Self> {
        // From https://docs.rs/serde_json/latest/serde_json/fn.from_reader.html :
        // Note that counter to intuition, this function is usually slower than reading a file completely into memory and then applying from_str or from_slice on it.

        let buf = asset_mgr.read_file(&format!("{}/Info.dat", dir.as_ref()));
        let value: Value = serde_json::from_str(&buf)?;

        match get_version(&value)? {
            "2.0.0" | "2.1.0" => {
                let info: SongInfo_V2 = serde_json::from_value(value)?;
                Ok(info.build(asset_mgr, dir)?)
            },
            "4.0.0" | "4.0.1" => {
                let info: SongInfo_V4 = serde_json::from_value(value)?;
                Ok(info.build(asset_mgr, dir)?)
            },
            version => Err(Error::BuildError(format!("Unsupported info version: {}", version)))
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn new<S: AsRef<str>>(asset_mgr: AssetManagerRc, dir: S, author: String, title: String, sub_title: String, song_filename: String, bpm_selector: BPMSelector, color_schemes: Vec<ColorScheme>, beatmap_infos: Vec<BeatmapInfo>) -> Self {
        let space = if sub_title.is_empty() {
            ""
        } else {
            " "
        };

        Self {
            asset_mgr,
            dir: dir.as_ref().to_string(),
            author,
            title: format!("{}{}{}", title, space, sub_title),
            song_filename: format!("{}/{}", dir.as_ref(), song_filename),
            bpm_selector,
            color_schemes: Box::from(color_schemes),
            beatmap_infos: Box::from(beatmap_infos),
        }
    }

    pub fn get_author(&self) -> &str {
        &self.author
    }

    pub fn get_title(&self) -> &str {
        &self.title
    }

    pub fn get_song_filename(&self) -> &str {
        &self.song_filename
    }

    pub fn get_bpm_info(&self) -> Result<BPMInfo> {
        Ok(match &self.bpm_selector {
            BPMSelector::Fixed(bpm) => BPMInfo::Fixed(*bpm),
            BPMSelector::Mapped(filename) => BPMInfo::Mapped(BPMMap::load(Arc::clone(&self.asset_mgr), &self.dir, filename)?),
        })
    }

    pub fn get_color_scheme(&self, index: u32) -> Option<&ColorScheme> {
        self.color_schemes.get(index as usize)
    }

    pub fn get_beatmap_infos(&self) -> &[BeatmapInfo] {
        &self.beatmap_infos
    }
}

enum BPMSelector {
    Fixed(f32),
    Mapped(String),
}

pub enum BPMInfo {
    Fixed(f32),
    Mapped(BPMMap),
}

pub struct ColorScheme {
    color_l: Color,
    color_r: Color,
}

impl ColorScheme {
    fn new(color_l: Color, color_r: Color) -> Self {
        Self {
            color_l,
            color_r,
        }
    }

    pub fn get_color_l(&self) -> &Color {
        &self.color_l
    }

    pub fn get_color_r(&self) -> &Color {
        &self.color_r
    }
}

impl Default for ColorScheme {
    fn default() -> Self {
        let color_l = Color::from_srgb_float(0.7843137, 0.07843138, 0.07843138); // See https://bsmg.wiki/mapping/lighting-defaults.html#_1-19-0-colors .
        let color_r = Color::from_srgb_float(0.1568627, 0.5568627, 0.8235294);

        ColorScheme::new(color_l, color_r)
    }
}

pub struct BeatmapInfo {
    asset_mgr: AssetManagerRc,
    dir: String,
    characteristic: String, // TODO: map
    difficulty: String, // TODO: map
    color_scheme_index_opt: Option<u32>,
    def_color_scheme: ColorScheme,
    filename: String,
    notejump_speed: f32,
    notejump_beatoffset: f32,
}

impl BeatmapInfo {
    #[allow(clippy::too_many_arguments)]
    fn new<S: AsRef<str>>(asset_mgr: AssetManagerRc, dir: S, characteristic: String, difficulty: String, color_scheme_index_opt: Option<u32>, def_color_scheme: ColorScheme, filename: String, notejump_speed: f32, notejump_beatoffset: f32) -> Self {
        Self {
            asset_mgr,
            dir: dir.as_ref().to_string(),
            characteristic,
            difficulty,
            color_scheme_index_opt,
            def_color_scheme,
            filename,
            notejump_speed,
            notejump_beatoffset,
        }
    }

    pub fn load(&self) -> Result<Beatmap> {
        Beatmap::load(Arc::clone(&self.asset_mgr), &self.dir, &self.filename)
    }

    pub fn get_characteristic(&self) -> &str {
        &self.characteristic
    }

    pub fn get_difficulty(&self) -> &str {
        &self.difficulty
    }

    pub fn get_color_scheme_index_opt(&self) -> Option<u32> {
        self.color_scheme_index_opt
    }

    pub fn get_def_color_scheme(&self) -> &ColorScheme {
        &self.def_color_scheme
    }

    #[allow(dead_code)] // TODO: remove
    fn get_notejump_speed(&self) -> f32 {
        self.notejump_speed
    }

    #[allow(dead_code)] // TODO: remove
    fn get_notejump_beatoffset(&self) -> f32 {
        self.notejump_beatoffset
    }
}

#[derive(Deserialize)]
struct SongInfo_V2 {
    #[serde(rename = "_songAuthorName")]
    author: String,
    #[serde(rename = "_songName")]
    title: String,
    #[serde(rename = "_songSubName")]
    sub_title: String,

    #[serde(rename = "_songFilename")]
    song_filename: String,
    #[serde(rename = "_beatsPerMinute")]
    bpm: f32, // TODO: validate > 0

    #[serde(rename = "_colorSchemes")]
    color_schemes: Option<Vec<SongInfo_V2_ColorScheme>>,

    #[serde(rename = "_difficultyBeatmapSets")]
    beatmap_info_sets: Vec<SongInfo_V2_BeatmapInfoSet>,
}

impl SongInfo_V2 {
    fn build<S: AsRef<str>>(self, asset_mgr: AssetManagerRc, dir: S) -> Result<SongInfo> {
        let mut color_schemes = Vec::new();
        if let Some(raw_color_schemes) = self.color_schemes {
            for raw_color_scheme in raw_color_schemes {
                let inner = raw_color_scheme.inner;
                let color_l = inner.color_l;
                let color_r = inner.color_r;
                let color_scheme = ColorScheme::new(Color::from_srgb_float(color_l.r, color_l.g, color_l.b), Color::from_srgb_float(color_r.r, color_r.g, color_r.b));
                color_schemes.push(color_scheme);
            }
        }

        let mut beatmap_infos = Vec::new();
        for raw_beatmap_info_set in self.beatmap_info_sets {
            let characteristic = raw_beatmap_info_set.characteristic;

            for raw_beatmap_info in raw_beatmap_info_set.beatmap_infos {
                let mut def_color_scheme = ColorScheme::default();

                if let Some(custom_data) = raw_beatmap_info.custom_data {
                    if let Some(color) = custom_data.color_l {
                        def_color_scheme.color_l = Color::from_srgb_float(color.r, color.g, color.b);
                    }
                    
                    if let Some(color) = custom_data.color_r {
                        def_color_scheme.color_r = Color::from_srgb_float(color.r, color.g, color.b);
                    }
                }

                let beatmap_info = BeatmapInfo::new(Arc::clone(&asset_mgr), dir.as_ref(), characteristic.clone(), raw_beatmap_info.difficulty, raw_beatmap_info.color_scheme_index_opt, def_color_scheme, raw_beatmap_info.filename, raw_beatmap_info.notejump_speed, raw_beatmap_info.notejump_beatoffset);
                beatmap_infos.push(beatmap_info);
            }
        }

        Ok(SongInfo::new(asset_mgr, dir, self.author, self.title, self.sub_title, self.song_filename, BPMSelector::Fixed(self.bpm), color_schemes, beatmap_infos))
    }
}

#[derive(Deserialize)]
struct SongInfo_V2_ColorScheme {
    #[serde(rename = "colorScheme")]
    inner: SongInfo_V2_ColorScheme_Inner,
}

#[derive(Deserialize)]
struct SongInfo_V2_ColorScheme_Inner {
    #[serde(rename = "saberAColor")]
    color_l: FloatColor,
    #[serde(rename = "saberBColor")]
    color_r: FloatColor,
}

#[derive(Deserialize)]
struct SongInfo_V2_BeatmapInfoSet {
    #[serde(rename = "_beatmapCharacteristicName")]
    characteristic: String,
    #[serde(rename = "_difficultyBeatmaps")]
    beatmap_infos: Vec<SongInfo_V2_BeatmapInfo>,
}

#[derive(Deserialize)]
struct SongInfo_V2_BeatmapInfo {
    #[serde(rename = "_difficulty")]
    difficulty: String,
    #[serde(rename = "_beatmapColorSchemeIdx")]
    color_scheme_index_opt: Option<u32>,
    #[serde(rename = "_beatmapFilename")]
    filename: String,
    #[serde(rename = "_noteJumpMovementSpeed")]
    notejump_speed: f32,
    #[serde(rename = "_noteJumpStartBeatOffset")]
    notejump_beatoffset: f32,
    #[serde(rename = "_customData")]
    custom_data: Option<SongInfo_V2_BeatmapInfo_CustomData>,
}

#[derive(Deserialize)]
struct SongInfo_V2_BeatmapInfo_CustomData {
    #[serde(rename = "_colorLeft")]
    color_l: Option<FloatColor>,
    #[serde(rename = "_colorRight")]
    color_r: Option<FloatColor>,
}

#[derive(Deserialize)]
struct SongInfo_V4 {
    song: SongInfo_V4_Song,
    audio: SongInfo_V4_Audio,
    #[serde(rename = "colorSchemes")]
    color_schemes: Option<Vec<SongInfo_V4_ColorScheme>>,
    #[serde(rename = "difficultyBeatmaps")]
    beatmap_infos: Vec<SongInfo_V4_BeatmapInfo>,
}

impl SongInfo_V4 {
    fn build<S: AsRef<str>>(self, asset_mgr: AssetManagerRc, dir: S) -> Result<SongInfo> {
        let bpm_selector = if let Some(filename) = self.audio.bpmmap_filename {
            BPMSelector::Mapped(filename)
        } else if let Some(bpm) = self.audio.bpm {
            BPMSelector::Fixed(bpm)
        } else {
            return Err(Error::BuildError("Either bpm or audioDataFilename is required".to_string()));
        };

        let mut color_schemes = Vec::new();
        if let Some(raw_color_schemes) = self.color_schemes {
            for raw_color_scheme in raw_color_schemes {
                let color_scheme = ColorScheme::new(raw_color_scheme.color_l, raw_color_scheme.color_r);
                color_schemes.push(color_scheme);
            }
        }

        let mut beatmap_infos = Vec::new();
        for raw_beatmap_info in self.beatmap_infos {
            let beatmap_info = BeatmapInfo::new(Arc::clone(&asset_mgr), dir.as_ref(), raw_beatmap_info.characteristic, raw_beatmap_info.difficulty, raw_beatmap_info.color_scheme_index_opt, ColorScheme::default(), raw_beatmap_info.filename, raw_beatmap_info.notejump_speed, raw_beatmap_info.notejump_beatoffset);
            beatmap_infos.push(beatmap_info);
        }

        Ok(SongInfo::new(asset_mgr, dir, self.song.author, self.song.title, self.song.sub_title, self.audio.song_filename, bpm_selector, color_schemes, beatmap_infos))
    }
}

#[derive(Deserialize)]
struct SongInfo_V4_Song {
    author: String,
    title: String,
    #[serde(rename = "subTitle")]
    sub_title: String,
}

#[derive(Deserialize)]
struct SongInfo_V4_Audio {
    #[serde(rename = "songFilename")]
    song_filename: String,
    bpm: Option<f32>, // TODO: validate > 0
    #[serde(rename = "audioDataFilename")]
    bpmmap_filename: Option<String>,
}

#[derive(Deserialize)]
struct SongInfo_V4_ColorScheme {
    #[serde(rename = "saberAColor")]
    color_l: Color,
    #[serde(rename = "saberBColor")]
    color_r: Color,
}

#[derive(Deserialize)]
struct SongInfo_V4_BeatmapInfo {
    characteristic: String,
    difficulty: String,
    #[serde(rename = "beatmapColorSchemeIdx")]
    color_scheme_index_opt: Option<u32>,
    #[serde(rename = "beatmapDataFilename")]
    filename: String,
    #[serde(rename = "noteJumpMovementSpeed")]
    notejump_speed: f32,
    #[serde(rename = "noteJumpStartBeatOffset")]
    notejump_beatoffset: f32,
}

// BPMMap

pub struct BPMMap {
    ranges: Box<[BPMRange]>,
}

impl BPMMap {
    fn load<S: AsRef<str>>(asset_mgr: AssetManagerRc, dir: S, filename: S) -> Result<Self> {
        let buf = asset_mgr.read_file(&format!("{}/{}", dir.as_ref(), filename.as_ref()));
        let value: Value = serde_json::from_str(&buf)?;

        match get_version(&value)? {
            "2.0.0" => {
                let bpmmap: BPMMap_V2 = serde_json::from_value(value)?;
                Ok(bpmmap.build())
            },
            "4.0.0" => {
                let bpmmap: BPMMap_V4 = serde_json::from_value(value)?;
                Ok(bpmmap.build())
            },
            version => Err(Error::BuildError(format!("Unsupported bpmmap version: {}", version)))
        }
    }

    fn new(mut ranges: Vec<BPMRange>) -> Self {
        ranges.sort_by(|range1, range2| range1.bpm.start.partial_cmp(&range2.bpm.start).expect("Unable to compare"));

        Self {
            ranges: Box::from(ranges),
        }
    }

    pub fn get_ts(&self, bpm: f32) -> Option<f32> {
        let index = self.ranges.partition_point(|range| range.bpm.start <= bpm); // First index, where range.bpm.start > bpm
        if index == 0 {
            return None;
        }

        let range = &self.ranges[index - 1];
        if bpm >= range.bpm.end {
            return None;
        }

        Some((bpm - range.bpm.start) / (range.bpm.end - range.bpm.start) * (range.ts.end - range.ts.start) + range.ts.start) // Map bpm to timestamp
    }
}

struct BPMRange {
    ts: Range<f32>,
    bpm: Range<f32>,
}

impl BPMRange {
    fn new(ts: Range<f32>, bpm: Range<f32>) -> Self {
        Self {
            ts,
            bpm,
        }
    }
}

#[derive(Deserialize)]
struct BPMMap_V2 {
    #[serde(rename = "_songFrequency")]
    sample_rate: u32,
    #[serde(rename = "_regions")]
    ranges: Vec<BPMMap_V2_Range>,
}

impl BPMMap_V2 {
    fn build(self) -> BPMMap {
        let ranges = Vec::from_iter(self.ranges.into_iter().map(|range| BPMRange::new(range.start_sample_pos as f32 / self.sample_rate as f32..range.end_sample_pos as f32 / self.sample_rate as f32, range.start_bpm..range.end_bpm)));
        BPMMap::new(ranges)
    }
}

#[derive(Deserialize)]
struct BPMMap_V2_Range { // TODO: impl validity checks
    #[serde(rename = "_startSampleIndex")]
    start_sample_pos: u32,
    #[serde(rename = "_endSampleIndex")]
    end_sample_pos: u32,
    #[serde(rename = "_startBeat")]
    start_bpm: f32,
    #[serde(rename = "_endBeat")]
    end_bpm: f32,
}

#[derive(Deserialize)]
struct BPMMap_V4 {
    #[serde(rename = "songFrequency")]
    sample_rate: u32,
    #[serde(rename = "bpmData")]
    ranges: Vec<BPMMap_V4_Range>,
}

impl BPMMap_V4 {
    fn build(self) -> BPMMap {
        let ranges = Vec::from_iter(self.ranges.into_iter().map(|range| BPMRange::new(range.start_sample_pos as f32 / self.sample_rate as f32..range.end_sample_pos as f32 / self.sample_rate as f32, range.start_bpm..range.end_bpm)));
        BPMMap::new(ranges)
    }
}

#[derive(Deserialize)]
struct BPMMap_V4_Range { // TODO: impl validity checks
    #[serde(rename = "si")]
    start_sample_pos: u32,
    #[serde(rename = "ei")]
    end_sample_pos: u32,
    #[serde(rename = "sb")]
    start_bpm: f32,
    #[serde(rename = "eb")]
    end_bpm: f32,
}

// Beatmap

pub struct Beatmap {
    notes: Box<[Note]>
}

impl Beatmap {
    fn load<S: AsRef<str>>(asset_mgr: AssetManagerRc, dir: S, filename: S) -> Result<Self> {
        let buf = asset_mgr.read_file(&format!("{}/{}", dir.as_ref(), filename.as_ref()));
        let value: Value = serde_json::from_str(&buf)?;

        match get_version(&value)? {
            "2.0.0" | "2.2.0" => {
                let beatmap: Beatmap_V2 = serde_json::from_value(value)?;
                beatmap.build()
            },
            "3.0.0" | "3.3.0" => {
                let beatmap: Beatmap_V3 = serde_json::from_value(value)?;
                beatmap.build()
            },
            "4.0.0" | "4.1.0" => {
                let beatmap: Beatmap_V4 = serde_json::from_value(value)?;
                beatmap.build()
            },
            version => Err(Error::BuildError(format!("Unsupported beatmap version: {}", version)))
        }
    }

    fn new(mut notes: Vec<Note>) -> Self {
        notes.sort_by(|note1, note2| note1.bpm_pos.partial_cmp(&note2.bpm_pos).expect("Unable to compare"));

        Self {
            notes: Box::from(notes),
        }
    }

    pub fn get_notes(&self) -> &[Note] {
        &self.notes
    }
}

pub struct Note {
    bpm_pos: f32,
    x: u8,
    y: u8,
    note_type: NoteType,
    cut_dir: NoteCutDir,
}

#[derive(Clone, Copy)]
pub enum NoteType {
    Left,
    Right,
}

#[derive(Clone, Copy)]
pub enum NoteCutDir {
    Up,
    Down,
    Left,
    Right,
    UpLeft,
    UpRight,
    DownLeft,
    DownRight,
    Any,
}

impl Note {
    fn new(bpm_pos: f32, x: u8, y: u8, note_type: NoteType, cut_dir: NoteCutDir) -> Result<Self> {
        if x > 3 || y > 2 {
            return Err(Error::BuildError("either note x or y invalid".to_string()));
        }

        Ok(Self {
            bpm_pos,
            x,
            y,
            note_type,
            cut_dir,
        })
    }

    pub fn get_bpm_pos(&self) -> f32 {
        self.bpm_pos
    }

    pub fn get_x(&self) -> u8 {
        self.x
    }

    pub fn get_y(&self) -> u8 {
        self.y
    }

    pub fn get_note_type(&self) -> NoteType {
        self.note_type
    }

    pub fn get_cut_dir(&self) -> NoteCutDir {
        self.cut_dir
    }
}

#[derive(Deserialize)]
struct Beatmap_V2 {
    #[serde(rename = "_notes")]
    notes: Vec<Beatmap_V2_Note>,
}

impl Beatmap_V2 {
    fn build(self) -> Result<Beatmap> {
        let mut notes = Vec::new();

        for raw_note in self.notes {
            if let Some(note_type) = get_note_type(raw_note.note_type) {
                let note = Note::new(raw_note.bpm_pos, raw_note.x, raw_note.y, note_type, raw_note.cut_dir)?;
                notes.push(note);
            }
        }

        Ok(Beatmap::new(notes))
    }
}

#[derive(Deserialize)]
struct Beatmap_V2_Note { // TODO: impl validate
    #[serde(rename = "_time")]
    bpm_pos: f32,
    #[serde(rename = "_lineIndex")]
    x: u8,
    #[serde(rename = "_lineLayer")]
    y: u8,
    #[serde(rename = "_type")]
    note_type: u8,
    #[serde(rename = "_cutDirection")]
    cut_dir: NoteCutDir,
}

#[derive(Deserialize)]
struct Beatmap_V3 {
    #[serde(rename = "colorNotes")]
    notes: Vec<Beatmap_V3_Note>,
}

impl Beatmap_V3 {
    fn build(self) -> Result<Beatmap> {
        let mut notes = Vec::new();

        for raw_note in self.notes {
            if let Some(note_type) = get_note_type(raw_note.note_type) {
                let note = Note::new(raw_note.bpm_pos, raw_note.x, raw_note.y, note_type, raw_note.cut_dir)?;
                notes.push(note);
            }
        }

        Ok(Beatmap::new(notes))
    }
}

#[derive(Deserialize)]
struct Beatmap_V3_Note { // TODO: impl validate
    #[serde(rename = "b")]
    bpm_pos: f32,
    x: u8,
    y: u8,
    #[serde(rename = "c")]
    note_type: u8,
    #[serde(rename = "d")]
    cut_dir: NoteCutDir,
}

#[derive(Deserialize)]
struct Beatmap_V4 {
    #[serde(rename = "colorNotes")]
    notes: Vec<Beatmap_V4_Note>,
    #[serde(rename = "colorNotesData")]
    note_datas: Vec<Beatmap_V4_NoteData>,
}

impl Beatmap_V4 {
    fn build(self) -> Result<Beatmap> {
        let mut notes = Vec::new();

        for raw_note in self.notes {
            if let Some(raw_note_data) = self.note_datas.get(raw_note.data_index as usize) &&
               let Some(note_type) = get_note_type(raw_note_data.note_type) {
                let note = Note::new(raw_note.bpm_pos, raw_note_data.x, raw_note_data.y, note_type, raw_note_data.cut_dir)?;
                notes.push(note);
            }
        }

        Ok(Beatmap::new(notes))
    }
}

#[derive(Deserialize)]
struct Beatmap_V4_Note { // TODO: impl validate
    #[serde(rename = "b")]
    bpm_pos: f32,
    #[serde(rename = "i")]
    data_index: u32,
}

#[derive(Deserialize)]
struct Beatmap_V4_NoteData { // TODO: impl validate
    x: u8,
    y: u8,
    #[serde(rename = "c")]
    note_type: u8,
    #[serde(rename = "d")]
    cut_dir: NoteCutDir,
}

// FloatColor

#[derive(Deserialize)]
struct FloatColor { // TODO: validate: 0 <= value <= 1
    r: f32,
    g: f32,
    b: f32,
}

// Color

impl<'de> Deserialize<'de> for Color {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> result_Result<Self, D::Error> {
        deserializer.deserialize_str(ColorVisitor)
    }
}

struct ColorVisitor;

impl<'de> Visitor<'de> for ColorVisitor {
    type Value = Color;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("valid color")
    }

    fn visit_str<E: de_Error>(self, v: &str) -> result_Result<Self::Value, E> {
        let raw_color = u32::from_str_radix(v, 16).map_err(|_| E::custom("invalid color"))?; // TODO: validate: leading +, number of digits, etc.
        Ok(Color::from_srgb_byte((raw_color >> 24) as u8, (raw_color >> 16) as u8, (raw_color >> 8) as u8))
    } 
}

// NoteCutDir

impl<'de> Deserialize<'de> for NoteCutDir {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> result_Result<Self, D::Error> {
        deserializer.deserialize_u64(NoteCutDirVisitor)
    }
}

struct NoteCutDirVisitor;

impl<'de> Visitor<'de> for NoteCutDirVisitor {
    type Value = NoteCutDir;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("valid cut direction")
    }

    fn visit_u64<E: de_Error>(self, v: u64) -> result_Result<Self::Value, E> {
        match v {
            0 => Ok(NoteCutDir::Up),
            1 => Ok(NoteCutDir::Down),
            2 => Ok(NoteCutDir::Left),
            3 => Ok(NoteCutDir::Right),
            4 => Ok(NoteCutDir::UpLeft),
            5 => Ok(NoteCutDir::UpRight),
            6 => Ok(NoteCutDir::DownLeft),
            7 => Ok(NoteCutDir::DownRight),
            8 => Ok(NoteCutDir::Any),
            _ => Err(E::custom("invalid cut direction")),
        }
    }    
}

fn get_version(top_value: &Value) -> Result<&str> {
    if let Value::Object(top) = top_value {
        for key in ["_version", "version"] {
            if let Some(version_value) = top.get(key) {
                if let Value::String(version) = version_value {
                    return Ok(version);
                } else {
                    return Err(Error::BuildError("version should be a string".to_string()));
                }
            }
        }

        Err(Error::BuildError("version not found".to_string()))
    } else {
        Err(Error::BuildError("object expected at top-level".to_string()))
    }
}

fn get_note_type(raw_note_type: u8) -> Option<NoteType> {
    match raw_note_type {
        0 => Some(NoteType::Left),
        1 => Some(NoteType::Right),
        _ => None,
    }
}