Serialize Album collection (#2)
Release Tagging / release (push) Successful in 44s
Rust Build / Check (push) Successful in 1m0s
Rust Build / Rustfmt (push) Successful in 26s
Rust Build / Clippy (push) Successful in 1m2s
Rust Build / build (push) Successful in 1m7s
Rust Build / Test Suite (push) Successful in 2m29s
Release Tagging / release (push) Successful in 44s
Rust Build / Check (push) Successful in 1m0s
Rust Build / Rustfmt (push) Successful in 26s
Rust Build / Clippy (push) Successful in 1m2s
Rust Build / build (push) Successful in 1m7s
Rust Build / Test Suite (push) Successful in 2m29s
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
+36
-1
@@ -37,7 +37,42 @@ pub mod collection {
|
||||
pub artist: String,
|
||||
pub disc: i32,
|
||||
pub track: i32,
|
||||
// In seconds
|
||||
/// In seconds
|
||||
pub duration: f64,
|
||||
}
|
||||
|
||||
pub fn deserialize_json(json_data: &str, album: &mut Album) -> Result<(), serde_json::Error> {
|
||||
match serde_json::from_str::<Album>(json_data) {
|
||||
Ok(parsed_album) => {
|
||||
*album = parsed_album;
|
||||
Ok(())
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn test_deserilize_album_collection_json() {
|
||||
let album_file_path = "tests/album.json";
|
||||
let album_file_content = match std::fs::read_to_string(album_file_path) {
|
||||
Ok(content) => content,
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
String::new()
|
||||
}
|
||||
};
|
||||
|
||||
let mut album = super::collection::Album::default();
|
||||
match super::collection::deserialize_json(&album_file_content, &mut album) {
|
||||
Ok(_) => {
|
||||
assert_eq!(3, album.tracks.len(), "Track count do not match");
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user