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::{parse_bms, BmsOutput};
16//! #[cfg(not(feature = "rand"))]
17//! use bms_rs::bms::{ast::rng::RngMock, parse_bms_with_rng, BmsOutput};
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/files/lilith_mx.bms").unwrap();
23//! #[cfg(feature = "rand")]
24//! let BmsOutput { bms, warnings }: BmsOutput<KeyLayoutBeat> = parse_bms(&source);
25//! #[cfg(not(feature = "rand"))]
26//! let BmsOutput { bms, warnings }: BmsOutput<KeyLayoutBeat> = parse_bms_with_rng(&source, RngMock([BigUint::from(1u64)]));
27//! assert_eq!(warnings, vec![]);
28//! println!("Title: {}", bms.header.title.as_deref().unwrap_or("Unknown"));
29//! println!("BPM: {}", bms.arrangers.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 num::BigUint;
43//!
44//! let source = std::fs::read_to_string("tests/files/lilith_mx.bms").unwrap();
45//! let LexOutput { tokens, lex_warnings } = TokenStream::parse_lex(&source);
46//! assert_eq!(lex_warnings, vec![]);
47//! // You can modify the tokens before parsing, for some commands that this library does not warpped.
48//! let AstBuildOutput { root, ast_build_warnings } = AstRoot::from_token_stream(&tokens);
49//! assert_eq!(ast_build_warnings, vec![]);
50//! #[cfg(feature = "rand")]
51//! let AstParseOutput { token_refs } = root.parse(RandRng(StdRng::seed_from_u64(42)));
52//! #[cfg(not(feature = "rand"))]
53//! let AstParseOutput { token_refs } = root.parse(RngMock([BigUint::from(1u64)]));
54//! let ParseOutput { bms, parse_warnings }:  ParseOutput<bms_rs::bms::command::channel::mapper::KeyLayoutBeat> = Bms::from_token_stream(
55//!     token_refs, AlwaysWarnAndUseNewer
56//! );
57//! // 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.
58//! assert_eq!(parse_warnings, vec![]);
59//! let PlayingCheckOutput { playing_warnings, playing_errors } = bms.check_playing();
60//! assert_eq!(playing_warnings, vec![]);
61//! assert_eq!(playing_errors, vec![]);
62//! println!("Title: {}", bms.header.title.as_deref().unwrap_or("Unknown"));
63//! println!("Artist: {}", bms.header.artist.as_deref().unwrap_or("Unknown"));
64//! println!("BPM: {}", bms.arrangers.bpm.unwrap_or(120.into()));
65//! ```
66//!
67//! - Note: You can also use [`bms::model::Bms::from_token_stream_with_ast`] to skip the AST building & parsing step.
68//!
69//! ## AST Extraction Usage
70//!
71//! You can also extract tokens back from an AST using the [`bms::ast::AstRoot::extract`] method, which serves as the inverse of [`bms::ast::AstRoot::from_token_stream`]:
72//!
73//! ```rust
74//! use bms_rs::bms::prelude::*;
75//!
76//! let source = std::fs::read_to_string("tests/files/lilith_mx.bms").unwrap();
77//! let LexOutput { tokens, lex_warnings } = TokenStream::parse_lex(&source);
78//! assert_eq!(lex_warnings, vec![]);
79//!
80//! // Build AST from tokens
81//! let AstBuildOutput { root, ast_build_warnings } = AstRoot::from_token_stream(&tokens);
82//! assert_eq!(ast_build_warnings, vec![]);
83//!
84//! // Extract tokens back from AST
85//! let extracted_tokens = root.extract();
86//! ```
87//!
88//! # Features
89//!
90//! - For supported commands, see [docs.rs#Token](https://docs.rs/bms-rs/latest/bms_rs/bms/lex/token/enum.Token.html).
91//!
92//! - For supported note channels, see [docs.rs#Channel](https://docs.rs/bms-rs/latest/bms_rs/bms/command/channel/enum.Channel.html).
93//!
94//! ## Default Features
95//!
96//! - `bmson` feature enables the BMSON format support.
97//! - `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.
98//! - `rand` feature enables the random number generator support. It supports [`bms::parse::random::rng::RandRng`].
99//!
100//! ## Optional Features
101//!
102//! - `minor-command` feature enables the commands that are almost never used in modern BMS Players.
103//!
104//! # About the format
105//!
106//! ## Command
107//!
108//! Each command starts with `#` character, and other lines will be ignored. Some commands require arguments separated by whitespace character such as spaces or tabs.
109//!
110//! ```text
111//! #PLAYER 1
112//! #GENRE FUGA
113//! #TITLE BAR(^^)
114//! #ARTIST MikuroXina
115//! #BPM 120
116//! #PLAYLEVEL 6
117//! #RANK 2
118//!
119//! #WAV01 hoge.WAV
120//! #WAV02 foo.WAV
121//! #WAV03 bar.WAV
122//!
123//! #00211:0303030303
124//! ```
125//!
126//! ### Header command
127//!
128//! Header commands are used to express the metadata of the music or the definition for note arrangement.
129//!
130//! ### Message command
131//!
132//! 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.
133//!
134//! The measure must start from 1, but some player may allow the 0 measure (i.e. Lunatic Rave 2).
135//!
136//! The channel commonly expresses what the lane be arranged the note to.
137//!
138//! 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:
139//!
140//! ```text
141//! #00211:0303000303
142//! ```
143//!
144//! This will be placed as:
145//!
146//! ```text
147//! 003|--|--------------|
148//!    |  |03            |
149//!    |  |03            |
150//!    |  |              |
151//!    |  |03            |
152//! 002|--|03------------|
153//!    |  |  []  []  []  |
154//!    |()|[]  []  []  []|
155//!    |-----------------|
156//! ```
157
158#![warn(missing_docs)]
159#![warn(clippy::must_use_candidate)]
160#![deny(rustdoc::broken_intra_doc_links)]
161#![cfg_attr(docsrs, feature(doc_cfg))]
162
163pub mod bms;
164pub mod bmson;
165
166pub use bms::{ast, command, lex, parse};