Skip to content

Commit

Permalink
Improve error handling and add Info::from_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinegb committed Aug 4, 2024
1 parent fcd5e32 commit af4de6f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ categories = ["data-structures", "games"]
[dependencies]
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.121"
thiserror = "1.0.63"
14 changes: 13 additions & 1 deletion src/info.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
//! Module related to `Info.dat` map file.
use std::path::PathBuf;
use std::{
fs,
path::{Path, PathBuf},
};

use serde::{Deserialize, Serialize};

use crate::Error;

/// Describes basic metadata about the song and points to map's other files.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html) for language-agnostic documentation.
Expand Down Expand Up @@ -52,6 +57,13 @@ impl Default for Info {
}
}

impl Info {
/// Instatiates an [`Info`] from an info file, typically named `Info.dat`.
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
Ok(serde_json::from_str(&fs::read_to_string(path)?)?)
}
}

/// Describes basic metadata about the song.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#song-metadata) for language-agnostic documentation.
Expand Down
30 changes: 15 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@
mod hex;
pub mod info;

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

use serde::de::Error;
use thiserror::Error;

pub use self::info::Info;

/// Any error that may occur from a function originating in this library.
#[derive(Error, Debug)]
pub enum Error {
/// Error from [`serde_json`].
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
/// Error from [`std::io`].
#[error(transparent)]
Io(#[from] io::Error),
}

/// Structural representation of a Beat Saber map folder.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format.html) for language-agnostic documentation.
Expand All @@ -34,20 +45,9 @@ pub struct BeatSaberMap {

impl BeatSaberMap {
/// Deserializes the files in a map folder.
pub fn from_dir(dir: impl Into<PathBuf>) -> serde_json::Result<Self> {
pub fn from_dir(dir: impl AsRef<Path>) -> Result<Self, Error> {
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,
})
},
)?)?,
info: Info::from_file(dir.as_ref().join("Info.dat"))?,
})
}
}

0 comments on commit af4de6f

Please sign in to comment.