Serialize Album collection #2
+36
-1
@@ -37,7 +37,42 @@ pub mod collection {
|
|||||||
pub artist: String,
|
pub artist: String,
|
||||||
pub disc: i32,
|
pub disc: i32,
|
||||||
pub track: i32,
|
pub track: i32,
|
||||||
// In seconds
|
/// In seconds
|
||||||
pub duration: f64,
|
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