Skip to content

Commit

Permalink
Add BeatSaberMap::from_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinegb committed Aug 2, 2024
1 parent b23e058 commit e3e4caa
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
mod hex;
pub mod info;

use std::{fs, path::PathBuf};

use serde::de::Error;

pub use self::info::Info;

/// Structural representation of a Beat Saber map folder.
Expand All @@ -19,3 +23,33 @@ pub struct BeatSaberMap {
/// See [`Info`].
pub info: Info,
}

impl BeatSaberMap {
/// Deserializes the files in a map folder.
pub fn from_dir(dir: impl Into<PathBuf>) -> serde_json::Result<Self> {
Ok(BeatSaberMap {
info: serde_json::from_str(&fs::read_to_string(dir.into().join("Info.dat")).map_err(
|err| {
let err_string = err.to_string();

serde_json::Error::custom(match err_string.chars().nth(0) {
Some(first_char) => {
first_char.to_lowercase().to_string() + &err_string[1..]
}
None => err_string,
})
},
)?)?,
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn from_dir_doesnt_fail() {
BeatSaberMap::from_dir("sample").unwrap();
}
}

0 comments on commit e3e4caa

Please sign in to comment.