Database trait and JSON implementation #6

Merged
wojtek merged 4 commits from 3---database-trait-and-json-implementation into main 2023-03-29 08:37:02 +02:00
3 changed files with 11 additions and 7 deletions
Showing only changes of commit 96f824863c - Show all commits

View File

@ -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)?,

View File

@ -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;

View File

@ -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;