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