bms-rs 1.0.0

The BMS format parser.
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
//! Prompting interface and utilities.
//!
//! An object implementing [`Prompter`] is required by [`super::Bms::from_token_stream`]. It is used to handle conflicts and prompt workarounds on parsing the BMS file.

use std::path::Path;

use strict_num_extended::{FinF64, NonNegativeF64, PositiveF64};

use crate::bms::{
    command::{
        ObjId, StringValue,
        channel::Channel,
        time::{ObjTime, Track},
    },
    model::{
        bmp::{AtBgaDef, BgaDef, Bmp},
        judge::ExRankDef,
        wav::ExWavDef,
    },
    parse::{ParseWarning, Result},
};

use crate::bms::model::obj::{
    BgaObj, BgmVolumeObj, BpmChangeObj, JudgeObj, KeyVolumeObj, ScrollingFactorObj,
    SectionLenChangeObj, SpeedObj, StopObj, TextObj,
};

use crate::bms::{
    command::{
        graphics::Argb,
        minor_command::{StpEvent, SwBgaEvent, WavCmdEvent},
    },
    model::obj::{BgaArgbObj, BgaKeyboundObj, BgaOpacityObj, OptionObj, SeekObj},
};

/// An interface to prompt about handling conflicts on the BMS file.
pub trait Prompter {
    /// Determines a [`DuplicationWorkaround`] for [`DefDuplication`].
    fn handle_def_duplication(&self, duplication: DefDuplication) -> DuplicationWorkaround;
    /// Determines a [`DuplicationWorkaround`] for [`TrackDuplication`].
    fn handle_track_duplication(&self, duplication: TrackDuplication) -> DuplicationWorkaround;
    /// Determines a [`DuplicationWorkaround`] for [`ChannelDuplication`].
    fn handle_channel_duplication(&self, duplication: ChannelDuplication) -> DuplicationWorkaround;
}

/// It represents that there is a duplicated definition on the BMS file.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum DefDuplication<'a> {
    /// BMP definition is duplicated.
    Bmp {
        /// Duplicated BMP object id.
        id: ObjId,
        /// Existing definition.
        older: &'a Bmp,
        /// Incoming definition.
        newer: &'a Bmp,
    },
    /// BPM definition is duplicated.
    BpmChange {
        /// Duplicated BPM object id.
        id: ObjId,
        /// Existing definition.
        older: &'a StringValue<PositiveF64>,
        /// Incoming definition.
        newer: &'a StringValue<PositiveF64>,
    },
    /// OPTION definition is duplicated.
    ChangeOption {
        /// Duplicated OPTION object id.
        id: ObjId,
        /// Existing definition.
        older: &'a str,
        /// Incoming definition.
        newer: &'a str,
    },
    /// SPEED definition is duplicated.
    SpeedFactorChange {
        /// Duplicated SPEED object id.
        id: ObjId,
        /// Existing definition.
        older: &'a StringValue<PositiveF64>,
        /// Incoming definition.
        newer: &'a StringValue<PositiveF64>,
    },
    /// SCROLL definition is duplicated.
    ScrollingFactorChange {
        /// Duplicated SCROLL object id.
        id: ObjId,
        /// Existing definition.
        older: &'a StringValue<FinF64>,
        /// Incoming definition.
        newer: &'a StringValue<FinF64>,
    },
    /// TEXT is duplicated.
    Text {
        /// Duplicated TEXT object id.
        id: ObjId,
        /// Existing definition.
        older: &'a str,
        /// Incoming definition.
        newer: &'a str,
    },
    /// WAV definition is duplicated.
    Wav {
        /// Duplicated WAV object id.
        id: ObjId,
        /// Existing definition.
        older: &'a Path,
        /// Incoming definition.
        newer: &'a Path,
    },
    /// @BGA definition is duplicated.
    AtBga {
        /// Duplicated @BGA object id.
        id: ObjId,
        /// Existing definition.
        older: &'a AtBgaDef,
        /// Incoming definition.
        newer: &'a AtBgaDef,
    },
    /// BGA definition is duplicated.
    Bga {
        /// Duplicated BGA object id.
        id: ObjId,
        /// Existing definition.
        older: &'a BgaDef,
        /// Incoming definition.
        newer: &'a BgaDef,
    },
    /// EXRANK definition is duplicated.
    ExRank {
        /// Duplicated EXRANK object id.
        id: ObjId,
        /// Existing definition.
        older: &'a ExRankDef,
        /// Incoming definition.
        newer: &'a ExRankDef,
    },
    /// EXWAV definition is duplicated.
    ExWav {
        /// Duplicated EXWAV object id.
        id: ObjId,
        /// Existing definition.
        older: &'a ExWavDef,
        /// Incoming definition.
        newer: &'a ExWavDef,
    },
    /// STOP definition is duplicated.
    Stop {
        /// Duplicated STOP object id.
        id: ObjId,
        /// Existing definition.
        older: &'a StringValue<NonNegativeF64>,
        /// Incoming definition.
        newer: &'a StringValue<NonNegativeF64>,
    },
    /// BGA ARGB color definition is duplicated.
    BgaArgb {
        /// Duplicated BGA ARGB object id.
        id: ObjId,
        /// Existing definition.
        older: &'a Argb,
        /// Incoming definition.
        newer: &'a Argb,
    },
    /// `WAVCMD` event is duplicated.
    WavCmdEvent {
        /// Duplicated `WAVCMD` event `wav_index`.
        wav_index: ObjId,
        /// Existing definition.
        older: &'a WavCmdEvent,
        /// Incoming definition.
        newer: &'a WavCmdEvent,
    },
    /// SWBGA event is duplicated.
    SwBgaEvent {
        /// Duplicated SWBGA event id.
        id: ObjId,
        /// Existing definition.
        older: &'a SwBgaEvent,
        /// Incoming definition.
        newer: &'a SwBgaEvent,
    },
    /// Seek event is duplicated.
    SeekEvent {
        /// Duplicated Seek event id.
        id: ObjId,
        /// Existing definition.
        older: &'a StringValue<FinF64>,
        /// Incoming definition.
        newer: &'a StringValue<FinF64>,
    },
}

/// It represents that there is a duplicated track object on the BMS file.
pub enum TrackDuplication<'a> {
    /// Section length change event is duplicated.
    SectionLenChangeEvent {
        /// Duplicated section length change track.
        track: Track,
        /// Existing definition.
        older: &'a SectionLenChangeObj,
        /// Incoming definition.
        newer: &'a SectionLenChangeObj,
    },
}

/// It represents that there is a duplicated channel object on the BMS file.
pub enum ChannelDuplication<'a> {
    /// BPM change event is duplicated.
    BpmChangeEvent {
        /// Duplicated BPM change time.
        time: ObjTime,
        /// Existing definition.
        older: &'a BpmChangeObj,
        /// Incoming definition.
        newer: &'a BpmChangeObj,
    },
    /// Scrolling factor change event is duplicated.
    ScrollingFactorChangeEvent {
        /// Duplicated scrolling factor change time.
        time: ObjTime,
        /// Existing definition.
        older: &'a ScrollingFactorObj,
        /// Incoming definition.
        newer: &'a ScrollingFactorObj,
    },
    /// Speed factor change event is duplicated.
    SpeedFactorChangeEvent {
        /// Duplicated speed factor change time.
        time: ObjTime,
        /// Existing definition.
        older: &'a SpeedObj,
        /// Incoming definition.
        newer: &'a SpeedObj,
    },
    /// BGA change event is duplicated.
    BgaChangeEvent {
        /// Duplicated BGA change time.
        time: ObjTime,
        /// Existing definition.
        older: &'a BgaObj,
        /// Incoming definition.
        newer: &'a BgaObj,
    },
    /// BGA opacity change event is duplicated.
    BgaOpacityChangeEvent {
        /// Duplicated BGA opacity change time.
        time: ObjTime,
        /// Existing definition.
        older: &'a BgaOpacityObj,
        /// Incoming definition.
        newer: &'a BgaOpacityObj,
    },
    /// BGA ARGB color change event is duplicated.
    BgaArgbChangeEvent {
        /// Duplicated BGA ARGB change time.
        time: ObjTime,
        /// Existing definition.
        older: &'a BgaArgbObj,
        /// Incoming definition.
        newer: &'a BgaArgbObj,
    },
    /// STP event is duplicated.
    StpEvent {
        /// Duplicated STP event time.
        time: ObjTime,
        /// Existing definition.
        older: &'a StpEvent,
        /// Incoming definition.
        newer: &'a StpEvent,
    },
    /// Stop event is duplicated.
    StopEvent {
        /// Duplicated stop time.
        time: ObjTime,
        /// Existing definition.
        older: &'a StopObj,
        /// Incoming definition.
        newer: &'a StopObj,
    },
    /// BGM volume change event is duplicated.
    BgmVolumeChangeEvent {
        /// Duplicated BGM volume change time.
        time: ObjTime,
        /// Existing definition.
        older: &'a BgmVolumeObj,
        /// Incoming definition.
        newer: &'a BgmVolumeObj,
    },
    /// KEY volume change event is duplicated.
    KeyVolumeChangeEvent {
        /// Duplicated KEY volume change time.
        time: ObjTime,
        /// Existing definition.
        older: &'a KeyVolumeObj,
        /// Incoming definition.
        newer: &'a KeyVolumeObj,
    },
    /// Seek message event is duplicated.
    SeekMessageEvent {
        /// Duplicated seek time.
        time: ObjTime,
        /// Existing definition.
        older: &'a SeekObj,
        /// Incoming definition.
        newer: &'a SeekObj,
    },
    /// Text event is duplicated.
    TextEvent {
        /// Duplicated text time.
        time: ObjTime,
        /// Existing definition.
        older: &'a TextObj,
        /// Incoming definition.
        newer: &'a TextObj,
    },
    /// Judge event is duplicated.
    JudgeEvent {
        /// Duplicated judge time.
        time: ObjTime,
        /// Existing definition.
        older: &'a JudgeObj,
        /// Incoming definition.
        newer: &'a JudgeObj,
    },
    /// BGA keybound event is duplicated.
    BgaKeyboundEvent {
        /// Duplicated BGA keybound time.
        time: ObjTime,
        /// Existing definition.
        older: &'a BgaKeyboundObj,
        /// Incoming definition.
        newer: &'a BgaKeyboundObj,
    },
    /// Option event is duplicated.
    OptionEvent {
        /// Duplicated option time.
        time: ObjTime,
        /// Existing definition.
        older: &'a OptionObj,
        /// Incoming definition.
        newer: &'a OptionObj,
    },
}

/// A choice to handle the duplicated definition.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DuplicationWorkaround {
    /// Choose to use the existing one.
    UseOlder,
    /// Choose to use the incoming one.
    UseNewer,
    /// Choose to warn and use older values.
    WarnAndUseOlder,
    /// Choose to warn and use newer values.
    WarnAndUseNewer,
}

impl DuplicationWorkaround {
    pub(crate) fn apply_def<T>(self, target: &mut T, newer: T, id: ObjId) -> Result<()> {
        match self {
            Self::UseOlder => Ok(()),
            Self::UseNewer => {
                *target = newer;
                Ok(())
            }
            Self::WarnAndUseOlder => Err(ParseWarning::DuplicatingDef(id)),
            Self::WarnAndUseNewer => {
                *target = newer;
                Err(ParseWarning::DuplicatingDef(id))
            }
        }
    }

    pub(crate) fn apply_track<T>(
        self,
        target: &mut T,
        newer: T,
        track: Track,
        channel: Channel,
    ) -> Result<()> {
        match self {
            Self::UseOlder => Ok(()),
            Self::UseNewer => {
                *target = newer;
                Ok(())
            }
            Self::WarnAndUseOlder => Err(ParseWarning::DuplicatingTrackObj(track, channel)),
            Self::WarnAndUseNewer => {
                *target = newer;
                Err(ParseWarning::DuplicatingTrackObj(track, channel))
            }
        }
    }

    pub(crate) fn apply_channel<T>(
        self,
        target: &mut T,
        newer: T,
        time: ObjTime,
        channel: Channel,
    ) -> Result<()> {
        match self {
            Self::UseOlder => Ok(()),
            Self::UseNewer => {
                *target = newer;
                Ok(())
            }
            Self::WarnAndUseOlder => Err(ParseWarning::DuplicatingChannelObj(time, channel)),
            Self::WarnAndUseNewer => {
                *target = newer;
                Err(ParseWarning::DuplicatingChannelObj(time, channel))
            }
        }
    }
}

/// The strategy that always using older ones.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AlwaysUseOlder;

impl Prompter for AlwaysUseOlder {
    fn handle_def_duplication(&self, _: DefDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::UseOlder
    }

    fn handle_track_duplication(&self, _: TrackDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::UseOlder
    }

    fn handle_channel_duplication(&self, _: ChannelDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::UseOlder
    }
}

/// The strategy that always using newer ones.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AlwaysUseNewer;

impl Prompter for AlwaysUseNewer {
    fn handle_def_duplication(&self, _: DefDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::UseNewer
    }

    fn handle_track_duplication(&self, _: TrackDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::UseNewer
    }

    fn handle_channel_duplication(&self, _: ChannelDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::UseNewer
    }
}

/// The strategy that always warns and uses older values.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AlwaysWarnAndUseOlder;

impl Prompter for AlwaysWarnAndUseOlder {
    fn handle_def_duplication(&self, _: DefDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::WarnAndUseOlder
    }

    fn handle_track_duplication(&self, _: TrackDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::WarnAndUseOlder
    }

    fn handle_channel_duplication(&self, _: ChannelDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::WarnAndUseOlder
    }
}

/// The strategy that always warns and uses newer values.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AlwaysWarnAndUseNewer;

impl Prompter for AlwaysWarnAndUseNewer {
    fn handle_def_duplication(&self, _: DefDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::WarnAndUseNewer
    }

    fn handle_track_duplication(&self, _: TrackDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::WarnAndUseNewer
    }

    fn handle_channel_duplication(&self, _: ChannelDuplication) -> DuplicationWorkaround {
        DuplicationWorkaround::WarnAndUseNewer
    }
}