1use serde::{Deserialize, Serialize};
2
3impl std::str::FromStr for Bmson {
4 type Err = serde_json::Error;
5 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
6 serde_json::from_str(s)
7 }
8}
9impl Bmson {
10 pub fn parse(source: &str) -> serde_json::Result<Bmson> {
12 serde_json::from_str(source)
13 }
14 pub fn to_string(&self) -> serde_json::Result<String> {
16 serde_json::to_string(self)
17 }
18 pub fn to_string_pretty(&self) -> serde_json::Result<String> {
22 serde_json::to_string_pretty(self)
23 }
24}
25
26#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
28pub struct Bmson {
29 pub version: String,
31 pub info: BmsonInfo,
33 pub lines: Option<Vec<BarLine>>,
35 pub bpm_events: Option<Vec<BpmEvent>>,
37 pub stop_events: Option<Vec<StopEvent>>,
39 pub sound_channels: Option<Vec<SoundChannel>>,
41 pub bga: Bga,
43 pub scroll_events: Option<Vec<ScrollEvent>>,
45 pub mine_channels: Option<Vec<MineChannel>>,
47 pub key_channels: Option<Vec<KeyChannel>>,
49}
50
51#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
53pub struct BmsonInfo {
54 pub title: String,
56 #[serde(default)]
58 pub subtitle: String,
59 pub artist: String,
61 pub subartists: Option<Vec<String>>,
63 pub genre: String,
65 #[serde(default = "default_mode_hint")]
69 pub mode_hint: String,
70 pub chart_name: String,
74 pub level: u32,
76 pub init_bpm: f64,
80 #[serde(default = "default_judge_rank")]
84 pub judge_rank: f64,
85 #[serde(default = "default_total")]
91 pub total: f64,
92 pub back_image: Option<String>,
94 pub eyecatch_image: Option<String>,
96 pub title_image: Option<String>,
98 pub banner_image: Option<String>,
100 pub preview_music: Option<String>,
102 #[serde(default = "default_resolution")]
108 pub resolution: u32,
109 pub ln_type: Option<LongNoteType>,
111}
112fn default_mode_hint() -> String {
113 "beat-7k".to_string()
114}
115fn default_judge_rank() -> f64 {
116 100.
117}
118fn default_total() -> f64 {
119 100.
120}
121fn default_resolution() -> u32 {
122 240
123}
124
125#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
127pub struct BarLine {
128 pub y: u32,
130}
131
132#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
134pub struct SoundChannel {
135 pub name: String,
137 pub notes: Vec<Note>,
139}
140
141#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
143pub struct Note {
144 pub x: Option<u32>,
148 pub y: u32,
150 pub l: u32,
154 pub c: bool,
158 pub t: Option<LongNoteType>,
160 pub up: Option<bool>,
164}
165
166#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
168pub struct BpmEvent {
169 pub y: u32,
171 pub bpm: f64,
173}
174
175#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
177pub struct StopEvent {
178 pub y: u32,
180 pub duration: u32,
182}
183
184#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
186pub struct Bga {
187 pub bga_header: Vec<BgaHeader>,
189 pub bga_events: Vec<BgaEvent>,
191 pub layer_events: Vec<BgaEvent>,
193 pub poor_events: Vec<BgaEvent>,
195}
196
197#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
199pub struct BgaHeader {
200 pub id: u32,
202 pub name: String,
204}
205
206#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
208pub struct BgaEvent {
209 pub y: u32,
211 pub id: u32,
213}
214
215#[derive(
217 serde_repr::Deserialize_repr,
218 serde_repr::Serialize_repr,
219 Clone,
220 Debug,
221 PartialEq,
222)]
223#[repr(u8)]
224pub enum LongNoteType {
225 None = 0,
227 LongNote = 1,
233 ChargeNote = 2,
239 HellChargeNote = 3,
247}
248
249#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
251pub struct ScrollEvent {
252 pub y: f64,
254 pub rate: f64,
256}
257
258#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
260pub struct MineChannel {
261 pub name: String,
263 pub notes: Vec<MineNote>,
265}
266
267#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
269pub struct MineNote {
270 pub x: Option<u32>,
274 pub y: u32,
276 pub damage: f64,
280}
281
282#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
284pub struct KeyChannel {
285 pub name: String,
287 pub notes: Vec<KeyNote>,
289}
290
291#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
293pub struct KeyNote {
294 pub x: Option<u32>,
298 pub y: u32,
300}