Database trait and JSON implementation #6
@ -5,21 +5,23 @@ use std::path::Path;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::database::DatabaseRead;
|
||||
|
||||
use super::DatabaseWrite;
|
||||
use crate::database::{DatabaseRead, DatabaseWrite};
|
||||
|
||||
/// A JSON file database.
|
||||
pub struct DatabaseJson {
|
||||
database_file: File,
|
||||
}
|
||||
|
||||
impl DatabaseJson {
|
||||
/// Create a read-only database instance. If the JSON file does not exist, an error is returned.
|
||||
pub fn reader(path: &Path) -> Result<Self, std::io::Error> {
|
||||
Ok(Self {
|
||||
database_file: File::open(path)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a write-only database instance. If the file does not exist, it is created, if it does
|
||||
/// exist, it is truncated.
|
||||
pub fn writer(path: &Path) -> Result<Self, std::io::Error> {
|
||||
Ok(Self {
|
||||
database_file: File::create(path)?,
|
||||
|
@ -1,15 +1,17 @@
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
mod json;
|
||||
pub mod json;
|
||||
|
||||
trait DatabaseRead {
|
||||
/// Trait for database reads.
|
||||
pub trait DatabaseRead {
|
||||
fn read<D>(&mut self, collection: &mut D) -> Result<(), std::io::Error>
|
||||
where
|
||||
D: DeserializeOwned;
|
||||
}
|
||||
|
||||
trait DatabaseWrite {
|
||||
/// Trait for database writes.
|
||||
pub trait DatabaseWrite {
|
||||
fn write<S>(&mut self, collection: &S) -> Result<(), std::io::Error>
|
||||
where
|
||||
S: Serialize;
|
||||
|
@ -3,7 +3,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
mod database;
|
||||
pub mod database;
|
||||
|
||||
/// [MusicBrainz Identifier](https://musicbrainz.org/doc/MusicBrainz_Identifier) (MBID).
|
||||
pub type Mbid = Uuid;
|
||||
|
Loading…
Reference in New Issue
Block a user