bms_rs/lib.rs
1//! The BMS format parser.
2//!
3//! Be-Music Source, called BMS for short, is a file format devised by Urao Yane in 1998 for a simulator of the game Beatmania by KONAMI. This describes what and when notes are arranged and its music metadata. It is a plain text file with some "command" lines starting with `#` character.
4//!
5//! # Usage
6//!
7//! - **NOTE**: BMS files now is almost with Shift_JIS encoding. It's recommended to use [`encoding_rs`](https://crates.io/crates/encoding_rs) crate to parse raw file to `Cow<str>`, which is a compatible type of `&str`, using `AsRef::as_ref`.
8//!
9//! ## Simple Usage
10//!
11//! For most use cases, you can use the [`bms::parse_bms`] function to parse a BMS file in one step:
12//!
13//! ```rust
14//! #[cfg(feature = "rand")]
15//! use bms_rs::bms::{BmsOutput, default_config, parse_bms};
16//! #[cfg(not(feature = "rand"))]
17//! use bms_rs::bms::{BmsOutput, ast::rng::RngMock, default_config_with_rng, parse_bms};
18//! #[cfg(not(feature = "rand"))]
19//! use num::BigUint;
20//! use bms_rs::bms::{command::channel::mapper::KeyLayoutBeat, BmsWarning};
21//!
22//! let source = std::fs::read_to_string("tests/bms/files/lilith_mx.bms").unwrap();
23//! #[cfg(feature = "rand")]
24//! let BmsOutput { bms, warnings } = parse_bms(&source, default_config()).expect("must be parsed");
25//! #[cfg(not(feature = "rand"))]
26//! let BmsOutput { bms, warnings } = parse_bms_with_rng(&source, default_config_with_rng(RngMock([BigUint::from(1u64)]))).expect("must be parsed");
27//! assert_eq!(warnings, vec![]);
28//! println!("Title: {}", bms.music_info.title.as_deref().unwrap_or("Unknown"));
29//! println!("BPM: {}", bms.bpm.bpm.unwrap_or(120.into()));
30//! ```
31//!
32//! ## Advanced Usage
33//!
34//! For more control over the parsing process, you can use the individual steps:
35//!
36//! At first, you can get the tokens stream with [`bms::lex::TokenStream::parse_lex`]. Then pass it and the random generator to [`bms::model::Bms::from_token_stream`] to get the notes data. Because BMS format has some randomized syntax.
37//!
38//! ```rust
39//! #[cfg(feature = "rand")]
40//! use rand::{rngs::StdRng, SeedableRng};
41//! use bms_rs::bms::prelude::*;
42//! use bms_rs::bms::command::channel::mapper::KeyLayoutBeat;
43//! use num::BigUint;
44//!
45//! let source = std::fs::read_to_string("tests/bms/files/lilith_mx.bms").unwrap();
46//! let LexOutput { tokens, lex_warnings } = TokenStream::parse_lex(&source);
47//! assert_eq!(lex_warnings, vec![]);
48//! // You can modify the tokens before parsing.
49//!
50//! #[cfg(feature = "rand")]
51//! let bms = Bms::from_token_stream(
52//! &tokens, default_config_with_rng(RandRng(StdRng::seed_from_u64(42))),
53//! ).expect("must be parsed");
54//! #[cfg(not(feature = "rand"))]
55//! let bms = Bms::from_token_stream(
56//! &tokens, default_config_with_rng(RngMock([BigUint::from(1u64)])),
57//! ).expect("must be parsed");
58//! // According to [BMS command memo#BEHAVIOR IN GENERAL IMPLEMENTATION](https://hitkey.bms.ms/cmds.htm#BEHAVIOR-IN-GENERAL-IMPLEMENTATION), the newer values are used for the duplicated objects.
59//! let PlayingCheckOutput { playing_warnings, playing_errors } = bms.check_playing::<KeyLayoutBeat>();
60//! assert_eq!(playing_warnings, vec![]);
61//! assert_eq!(playing_errors, vec![]);
62//! println!("Title: {}", bms.music_info.title.as_deref().unwrap_or("Unknown"));
63//! println!("Artist: {}", bms.music_info.artist.as_deref().unwrap_or("Unknown"));
64//! println!("BPM: {}", bms.bpm.bpm.unwrap_or(120.into()));
65//! ```
66//!
67//! # Features
68//!
69//! - For supported commands, see [docs.rs#Token](https://docs.rs/bms-rs/latest/bms_rs/bms/lex/token/enum.Token.html).
70//!
71//! - For supported note channels, see [docs.rs#Channel](https://docs.rs/bms-rs/latest/bms_rs/bms/command/channel/enum.Channel.html).
72//!
73//! ## Default Features
74//!
75//! - `bmson` feature enables the BMSON format support.
76//! - `serde` feature enables the `serde` support. It supports [`serde::Serialize`] for all the definications in this crate, and [`serde::Deserialize`] for all the result types.
77//! - `rand` feature enables the random number generator support. It supports [`bms::parse::random::rng::RandRng`].
78//!
79//! ## Optional Features
80//!
81//! - `minor-command` feature enables the commands that are almost never used in modern BMS Players.
82//!
83//! # About the format
84//!
85//! ## Command
86//!
87//! Each command starts with `#` character, and other lines will be ignored. Some commands require arguments separated by whitespace character such as spaces or tabs.
88//!
89//! ```text
90//! #PLAYER 1
91//! #GENRE FUGA
92//! #TITLE BAR(^^)
93//! #ARTIST MikuroXina
94//! #BPM 120
95//! #PLAYLEVEL 6
96//! #RANK 2
97//!
98//! #WAV01 hoge.WAV
99//! #WAV02 foo.WAV
100//! #WAV03 bar.WAV
101//!
102//! #00211:0303030303
103//! ```
104//!
105//! ### Header command
106//!
107//! Header commands are used to express the metadata of the music or the definition for note arrangement.
108//!
109//! ### Message command
110//!
111//! Message command starts with `#XXXYY:ZZ...`. `XXX` is the number of the measure, `YY` is the channel of the message, and `ZZ...` is the object id sequence.
112//!
113//! The measure must start from 1, but some player may allow the 0 measure (i.e. Lunatic Rave 2).
114//!
115//! The channel commonly expresses what the lane be arranged the note to.
116//!
117//! The object id is formed by 2-digit of 36-radix (`[0-9a-zA-Z]`) integer. So the sequence length must be an even number. The `00` object id is the special id, expresses the rest (no object lies). The object lies on the position divided equally by how many the object is in the measure. For example:
118//!
119//! ```text
120//! #00211:0303000303
121//! ```
122//!
123//! This will be placed as:
124//!
125//! ```text
126//! 003|--|--------------|
127//! | |03 |
128//! | |03 |
129//! | | |
130//! | |03 |
131//! 002|--|03------------|
132//! | | [] [] [] |
133//! |()|[] [] [] []|
134//! |-----------------|
135//! ```
136
137#![warn(missing_docs)]
138#![warn(clippy::must_use_candidate)]
139#![deny(rustdoc::broken_intra_doc_links)]
140#![cfg_attr(docsrs, feature(doc_cfg))]
141
142pub mod bms;
143pub mod bmson;
144pub mod chart_process;
145pub mod diagnostics;
146pub(crate) mod util;
147
148pub use bms::{command, lex, parse};