Fix clippy
This commit is contained in:
parent
11bcb8bdb9
commit
f744024369
@ -6,19 +6,19 @@ use serde::Serialize;
|
||||
#[cfg(test)]
|
||||
use mockall::automock;
|
||||
|
||||
use super::{IDatabase, ReadError, WriteError};
|
||||
use super::{IDatabase, LoadError, SaveError};
|
||||
|
||||
pub mod backend;
|
||||
|
||||
impl From<serde_json::Error> for ReadError {
|
||||
fn from(err: serde_json::Error) -> ReadError {
|
||||
ReadError::SerDeError(err.to_string())
|
||||
impl From<serde_json::Error> for LoadError {
|
||||
fn from(err: serde_json::Error) -> LoadError {
|
||||
LoadError::SerDeError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for WriteError {
|
||||
fn from(err: serde_json::Error) -> WriteError {
|
||||
WriteError::SerDeError(err.to_string())
|
||||
impl From<serde_json::Error> for SaveError {
|
||||
fn from(err: serde_json::Error) -> SaveError {
|
||||
SaveError::SerDeError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,13 +46,13 @@ impl<JDB: IJsonDatabaseBackend> JsonDatabase<JDB> {
|
||||
}
|
||||
|
||||
impl<JDB: IJsonDatabaseBackend> IDatabase for JsonDatabase<JDB> {
|
||||
fn read<D: DeserializeOwned>(&self, collection: &mut D) -> Result<(), ReadError> {
|
||||
fn load<D: DeserializeOwned>(&self, collection: &mut D) -> Result<(), LoadError> {
|
||||
let serialized = self.backend.read()?;
|
||||
*collection = serde_json::from_str(&serialized)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write<S: Serialize>(&mut self, collection: &S) -> Result<(), WriteError> {
|
||||
fn save<S: Serialize>(&mut self, collection: &S) -> Result<(), SaveError> {
|
||||
let serialized = serde_json::to_string(&collection)?;
|
||||
self.backend.write(&serialized)?;
|
||||
Ok(())
|
||||
@ -133,7 +133,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write() {
|
||||
fn save() {
|
||||
let write_data = COLLECTION.to_owned();
|
||||
let input = artists_to_json(&write_data);
|
||||
|
||||
@ -144,11 +144,11 @@ mod tests {
|
||||
.times(1)
|
||||
.return_once(|_| Ok(()));
|
||||
|
||||
JsonDatabase::new(backend).write(&write_data).unwrap();
|
||||
JsonDatabase::new(backend).save(&write_data).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read() {
|
||||
fn load() {
|
||||
let expected = COLLECTION.to_owned();
|
||||
let result = Ok(artists_to_json(&expected));
|
||||
|
||||
@ -156,7 +156,7 @@ mod tests {
|
||||
backend.expect_read().times(1).return_once(|| result);
|
||||
|
||||
let mut read_data: Vec<Artist> = vec![];
|
||||
JsonDatabase::new(backend).read(&mut read_data).unwrap();
|
||||
JsonDatabase::new(backend).load(&mut read_data).unwrap();
|
||||
|
||||
assert_eq!(read_data, expected);
|
||||
}
|
||||
@ -179,8 +179,8 @@ mod tests {
|
||||
|
||||
let write_data = COLLECTION.to_owned();
|
||||
let mut read_data: Vec<Artist> = vec![];
|
||||
database.write(&write_data).unwrap();
|
||||
database.read(&mut read_data).unwrap();
|
||||
database.save(&write_data).unwrap();
|
||||
database.load(&mut read_data).unwrap();
|
||||
|
||||
assert_eq!(write_data, read_data);
|
||||
}
|
||||
@ -197,7 +197,7 @@ mod tests {
|
||||
let serde_err = serde_json::to_string(&object);
|
||||
assert!(serde_err.is_err());
|
||||
|
||||
let serde_err: WriteError = serde_err.unwrap_err().into();
|
||||
let serde_err: SaveError = serde_err.unwrap_err().into();
|
||||
assert!(!serde_err.to_string().is_empty());
|
||||
assert!(!format!("{:?}", serde_err).is_empty());
|
||||
}
|
||||
|
@ -13,23 +13,23 @@ pub mod json;
|
||||
/// Trait for interacting with the database.
|
||||
#[cfg_attr(test, automock)]
|
||||
pub trait IDatabase {
|
||||
/// Read collection from the database.
|
||||
fn read<D: DeserializeOwned + 'static>(&self, collection: &mut D) -> Result<(), ReadError>;
|
||||
/// Load collection from the database.
|
||||
fn load<D: DeserializeOwned + 'static>(&self, collection: &mut D) -> Result<(), LoadError>;
|
||||
|
||||
/// Write collection to the database.
|
||||
fn write<S: Serialize + 'static>(&mut self, collection: &S) -> Result<(), WriteError>;
|
||||
/// Save collection to the database.
|
||||
fn save<S: Serialize + 'static>(&mut self, collection: &S) -> Result<(), SaveError>;
|
||||
}
|
||||
|
||||
/// Error type for database calls.
|
||||
#[derive(Debug)]
|
||||
pub enum ReadError {
|
||||
pub enum LoadError {
|
||||
/// The database experienced an I/O read error.
|
||||
IoError(String),
|
||||
/// The database experienced a deserialisation error.
|
||||
SerDeError(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for ReadError {
|
||||
impl fmt::Display for LoadError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Self::IoError(ref s) => write!(f, "the database experienced an I/O read error: {s}"),
|
||||
@ -40,22 +40,22 @@ impl fmt::Display for ReadError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for ReadError {
|
||||
fn from(err: std::io::Error) -> ReadError {
|
||||
ReadError::IoError(err.to_string())
|
||||
impl From<std::io::Error> for LoadError {
|
||||
fn from(err: std::io::Error) -> LoadError {
|
||||
LoadError::IoError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Error type for database calls.
|
||||
#[derive(Debug)]
|
||||
pub enum WriteError {
|
||||
pub enum SaveError {
|
||||
/// The database experienced an I/O write error.
|
||||
IoError(String),
|
||||
/// The database experienced a serialisation error.
|
||||
SerDeError(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for WriteError {
|
||||
impl fmt::Display for SaveError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Self::IoError(ref s) => write!(f, "the database experienced an I/O write error: {s}"),
|
||||
@ -66,9 +66,9 @@ impl fmt::Display for WriteError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for WriteError {
|
||||
fn from(err: std::io::Error) -> WriteError {
|
||||
WriteError::IoError(err.to_string())
|
||||
impl From<std::io::Error> for SaveError {
|
||||
fn from(err: std::io::Error) -> SaveError {
|
||||
SaveError::IoError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,15 +76,15 @@ impl From<std::io::Error> for WriteError {
|
||||
mod tests {
|
||||
use std::io;
|
||||
|
||||
use super::{ReadError, WriteError};
|
||||
use super::{LoadError, SaveError};
|
||||
|
||||
#[test]
|
||||
fn errors() {
|
||||
let io_err: ReadError = io::Error::new(io::ErrorKind::Interrupted, "error").into();
|
||||
let io_err: LoadError = io::Error::new(io::ErrorKind::Interrupted, "error").into();
|
||||
assert!(!io_err.to_string().is_empty());
|
||||
assert!(!format!("{:?}", io_err).is_empty());
|
||||
|
||||
let io_err: WriteError = io::Error::new(io::ErrorKind::Interrupted, "error").into();
|
||||
let io_err: SaveError = io::Error::new(io::ErrorKind::Interrupted, "error").into();
|
||||
assert!(!io_err.to_string().is_empty());
|
||||
assert!(!format!("{:?}", io_err).is_empty());
|
||||
}
|
||||
|
24
src/lib.rs
24
src/lib.rs
@ -213,14 +213,14 @@ impl From<library::Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<database::ReadError> for Error {
|
||||
fn from(err: database::ReadError) -> Error {
|
||||
impl From<database::LoadError> for Error {
|
||||
fn from(err: database::LoadError) -> Error {
|
||||
Error::DatabaseError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<database::WriteError> for Error {
|
||||
fn from(err: database::WriteError) -> Error {
|
||||
impl From<database::SaveError> for Error {
|
||||
fn from(err: database::SaveError) -> Error {
|
||||
Error::DatabaseError(err.to_string())
|
||||
}
|
||||
}
|
||||
@ -248,7 +248,7 @@ impl<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
|
||||
let mut library = Self::items_to_artists(items);
|
||||
Self::sort(&mut library);
|
||||
|
||||
let collection = mem::replace(&mut self.collection, vec![]);
|
||||
let collection = mem::take(&mut self.collection);
|
||||
self.collection = Self::merge(library, collection);
|
||||
|
||||
Ok(())
|
||||
@ -256,17 +256,17 @@ impl<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
|
||||
|
||||
pub fn load_from_database(&mut self) -> Result<(), Error> {
|
||||
let mut database: Collection = vec![];
|
||||
self.database.read(&mut database)?;
|
||||
self.database.load(&mut database)?;
|
||||
Self::sort(&mut database);
|
||||
|
||||
let collection = mem::replace(&mut self.collection, vec![]);
|
||||
let collection = mem::take(&mut self.collection);
|
||||
self.collection = Self::merge(collection, database);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save_to_database(&mut self) -> Result<(), Error> {
|
||||
self.database.write(&self.collection)?;
|
||||
self.database.save(&self.collection)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -489,7 +489,7 @@ mod tests {
|
||||
.return_once(|_| library_result);
|
||||
|
||||
database
|
||||
.expect_write()
|
||||
.expect_save()
|
||||
.with(predicate::eq(database_input))
|
||||
.times(1)
|
||||
.return_once(|_: &Collection| database_result);
|
||||
@ -528,10 +528,10 @@ mod tests {
|
||||
let library = MockILibrary::new();
|
||||
let mut database = MockIDatabase::new();
|
||||
|
||||
let database_result = Err(database::WriteError::IoError(String::from("I/O error")));
|
||||
let database_result = Err(database::SaveError::IoError(String::from("I/O error")));
|
||||
|
||||
database
|
||||
.expect_write()
|
||||
.expect_save()
|
||||
.times(1)
|
||||
.return_once(|_: &Collection| database_result);
|
||||
|
||||
@ -539,7 +539,7 @@ mod tests {
|
||||
|
||||
let actual_err = music_hoard.save_to_database().unwrap_err();
|
||||
let expected_err = Error::DatabaseError(
|
||||
database::WriteError::IoError(String::from("I/O error")).to_string(),
|
||||
database::SaveError::IoError(String::from("I/O error")).to_string(),
|
||||
);
|
||||
|
||||
assert_eq!(actual_err, expected_err);
|
||||
|
@ -70,7 +70,7 @@ fn main() {
|
||||
Ok(f) => {
|
||||
drop(f);
|
||||
JsonDatabase::new(JsonDatabaseFileBackend::new(&opt.database_file_path))
|
||||
.write::<Collection>(&vec![])
|
||||
.save::<Collection>(&vec![])
|
||||
.expect("failed to create empty database");
|
||||
}
|
||||
Err(e) => match e.kind() {
|
||||
|
@ -16,14 +16,14 @@ static DATABASE_TEST_FILE: Lazy<PathBuf> =
|
||||
Lazy::new(|| fs::canonicalize("./tests/files/database/database.json").unwrap());
|
||||
|
||||
#[test]
|
||||
fn write() {
|
||||
fn save() {
|
||||
let file = NamedTempFile::new().unwrap();
|
||||
|
||||
let backend = JsonDatabaseFileBackend::new(file.path());
|
||||
let mut database = JsonDatabase::new(backend);
|
||||
|
||||
let write_data = COLLECTION.to_owned();
|
||||
database.write(&write_data).unwrap();
|
||||
database.save(&write_data).unwrap();
|
||||
|
||||
let expected = fs::read_to_string(&*DATABASE_TEST_FILE).unwrap();
|
||||
let actual = fs::read_to_string(file.path()).unwrap();
|
||||
@ -32,12 +32,12 @@ fn write() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read() {
|
||||
fn load() {
|
||||
let backend = JsonDatabaseFileBackend::new(&*DATABASE_TEST_FILE);
|
||||
let database = JsonDatabase::new(backend);
|
||||
|
||||
let mut read_data: Vec<Artist> = vec![];
|
||||
database.read(&mut read_data).unwrap();
|
||||
database.load(&mut read_data).unwrap();
|
||||
|
||||
let expected = COLLECTION.to_owned();
|
||||
assert_eq!(read_data, expected);
|
||||
@ -51,10 +51,10 @@ fn reverse() {
|
||||
let mut database = JsonDatabase::new(backend);
|
||||
|
||||
let write_data = COLLECTION.to_owned();
|
||||
database.write(&write_data).unwrap();
|
||||
database.save(&write_data).unwrap();
|
||||
|
||||
let mut read_data: Vec<Artist> = vec![];
|
||||
database.read(&mut read_data).unwrap();
|
||||
database.load(&mut read_data).unwrap();
|
||||
|
||||
assert_eq!(write_data, read_data);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user