Some code improvements
This commit is contained in:
parent
e2332f79ec
commit
cadc799495
@ -1,5 +1,6 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
@ -13,10 +14,16 @@ pub struct DatabaseJson {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DatabaseJson {
|
impl DatabaseJson {
|
||||||
pub fn new(file: File) -> Self {
|
pub fn reader(path: &Path) -> Result<Self, std::io::Error> {
|
||||||
Self {
|
Ok(Self {
|
||||||
database_file: file,
|
database_file: File::open(path)?,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn writer(path: &Path) -> Result<Self, std::io::Error> {
|
||||||
|
Ok(Self {
|
||||||
|
database_file: File::create(path)?,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +48,8 @@ impl DatabaseWrite for DatabaseJson {
|
|||||||
S: Serialize,
|
S: Serialize,
|
||||||
{
|
{
|
||||||
let serialized = serde_json::to_string(&collection)?;
|
let serialized = serde_json::to_string(&collection)?;
|
||||||
self.database_file.write_all(serialized.as_bytes())
|
self.database_file.write_all(serialized.as_bytes())?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,35 +142,36 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn write() {
|
fn write() {
|
||||||
let data = test_data();
|
let write_data = test_data();
|
||||||
let temp_file = NamedTempFile::new().unwrap();
|
let temp_file = NamedTempFile::new().unwrap();
|
||||||
DatabaseJson::new(File::create(temp_file.path()).unwrap())
|
DatabaseJson::writer(temp_file.path())
|
||||||
.write(&data)
|
.unwrap()
|
||||||
|
.write(&write_data)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Compare files.
|
let mut write_data_str = String::new();
|
||||||
let mut data_str = String::new();
|
|
||||||
File::open(temp_file.path())
|
File::open(temp_file.path())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.read_to_string(&mut data_str)
|
.read_to_string(&mut write_data_str)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut test_str = String::new();
|
let mut test_data_str = String::new();
|
||||||
File::open(TEST_FILENAME)
|
File::open(TEST_FILENAME)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.read_to_string(&mut test_str)
|
.read_to_string(&mut test_data_str)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(data_str, test_str);
|
assert_eq!(write_data_str, test_data_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn read() {
|
fn read() {
|
||||||
let mut data: Vec<ReleaseGroup> = vec![];
|
let mut read_data: Vec<ReleaseGroup> = vec![];
|
||||||
DatabaseJson::new(File::open(Path::new(TEST_FILENAME)).unwrap())
|
DatabaseJson::reader(Path::new(TEST_FILENAME))
|
||||||
.read(&mut data)
|
.unwrap()
|
||||||
|
.read(&mut read_data)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(data, test_data());
|
assert_eq!(read_data, test_data());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -172,10 +181,12 @@ mod tests {
|
|||||||
|
|
||||||
let temp_file = NamedTempFile::new().unwrap();
|
let temp_file = NamedTempFile::new().unwrap();
|
||||||
|
|
||||||
DatabaseJson::new(File::create(temp_file.path()).unwrap())
|
DatabaseJson::writer(temp_file.path())
|
||||||
|
.unwrap()
|
||||||
.write(&write_data)
|
.write(&write_data)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
DatabaseJson::new(File::open(temp_file.path()).unwrap())
|
DatabaseJson::reader(temp_file.path())
|
||||||
|
.unwrap()
|
||||||
.read(&mut read_data)
|
.read(&mut read_data)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
//! MusicHoard - a music collection manager.
|
//! MusicHoard - a music collection manager.
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
mod database;
|
mod database;
|
||||||
|
|
||||||
/// [MusicBrainz Identifier](https://musicbrainz.org/doc/MusicBrainz_Identifier) (MBID).
|
/// [MusicBrainz Identifier](https://musicbrainz.org/doc/MusicBrainz_Identifier) (MBID).
|
||||||
pub type Mbid = uuid::Uuid;
|
pub type Mbid = Uuid;
|
||||||
|
|
||||||
/// [Artist](https://musicbrainz.org/doc/Artist).
|
/// [Artist](https://musicbrainz.org/doc/Artist).
|
||||||
#[derive(Debug, Deserialize, Serialize, PartialEq)]
|
#[derive(Debug, Deserialize, Serialize, PartialEq)]
|
||||||
|
Loading…
Reference in New Issue
Block a user