From c87e710d699bdbc686aa13fde8e3c78e5a848871 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Fri, 14 Apr 2023 16:15:25 +0200 Subject: [PATCH] Split out backends from database --- src/database/json/backend.rs | 30 +++++++++++++++++++++++++++ src/database/{json.rs => json/mod.rs} | 29 ++------------------------ src/main.rs | 2 +- tests/database/json.rs | 2 +- 4 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 src/database/json/backend.rs rename src/database/{json.rs => json/mod.rs} (87%) diff --git a/src/database/json/backend.rs b/src/database/json/backend.rs new file mode 100644 index 0000000..32026d1 --- /dev/null +++ b/src/database/json/backend.rs @@ -0,0 +1,30 @@ +//! Module for storing MusicHoard data in a JSON file database. + +use std::fs; +use std::path::PathBuf; + +use super::JsonDatabaseBackend; + +/// JSON database backend that uses a local file for persistent storage. +pub struct JsonDatabaseFileBackend { + path: PathBuf, +} + +impl JsonDatabaseFileBackend { + /// Create a [`JsonDatabaseFileBackend`] that will read/write to the provided path. + pub fn new>(path: P) -> Self { + JsonDatabaseFileBackend { path: path.into() } + } +} + +impl JsonDatabaseBackend for JsonDatabaseFileBackend { + fn read(&self) -> Result { + // Read entire file to memory as for now this is faster than a buffered read from disk: + // https://github.com/serde-rs/json/issues/160 + fs::read_to_string(&self.path) + } + + fn write(&mut self, json: &str) -> Result<(), std::io::Error> { + fs::write(&self.path, json) + } +} diff --git a/src/database/json.rs b/src/database/json/mod.rs similarity index 87% rename from src/database/json.rs rename to src/database/json/mod.rs index 08b8913..5caeefa 100644 --- a/src/database/json.rs +++ b/src/database/json/mod.rs @@ -1,8 +1,5 @@ //! Module for storing MusicHoard data in a JSON file database. -use std::fs; -use std::path::PathBuf; - use serde::de::DeserializeOwned; use serde::Serialize; @@ -11,6 +8,8 @@ use mockall::automock; use super::{Database, Error}; +pub mod backend; + impl From for Error { fn from(err: serde_json::Error) -> Error { Error::SerDeError(err.to_string()) @@ -53,30 +52,6 @@ impl Database for JsonDatabase { } } -/// JSON database backend that uses a local file for persistent storage. -pub struct JsonDatabaseFileBackend { - path: PathBuf, -} - -impl JsonDatabaseFileBackend { - /// Create a [`JsonDatabaseFileBackend`] that will read/write to the provided path. - pub fn new>(path: P) -> Self { - JsonDatabaseFileBackend { path: path.into() } - } -} - -impl JsonDatabaseBackend for JsonDatabaseFileBackend { - fn read(&self) -> Result { - // Read entire file to memory as for now this is faster than a buffered read from disk: - // https://github.com/serde-rs/json/issues/160 - fs::read_to_string(&self.path) - } - - fn write(&mut self, json: &str) -> Result<(), std::io::Error> { - fs::write(&self.path, json) - } -} - #[cfg(test)] mod tests { use std::collections::HashMap; diff --git a/src/main.rs b/src/main.rs index 28c466e..cc74a41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use structopt::StructOpt; use musichoard::{ collection::MhCollectionManager, database::{ - json::{JsonDatabase, JsonDatabaseFileBackend}, + json::{backend::JsonDatabaseFileBackend, JsonDatabase}, Database, }, library::{ diff --git a/tests/database/json.rs b/tests/database/json.rs index d39cb7e..55ea7a6 100644 --- a/tests/database/json.rs +++ b/tests/database/json.rs @@ -2,7 +2,7 @@ use std::{fs, path::PathBuf}; use musichoard::{ database::{ - json::{JsonDatabase, JsonDatabaseFileBackend}, + json::{backend::JsonDatabaseFileBackend, JsonDatabase}, Database, }, Artist,