First attempt at database
This commit is contained in:
parent
057808c1cc
commit
b6474e3850
24
Cargo.lock
generated
24
Cargo.lock
generated
@ -2,11 +2,18 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6"
|
||||
|
||||
[[package]]
|
||||
name = "musichoard"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
@ -28,6 +35,12 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.159"
|
||||
@ -48,6 +61,17 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.95"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.11"
|
||||
|
@ -7,4 +7,5 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
uuid = { version = "1.3", features = ["serde"] }
|
||||
|
39
src/database/json.rs
Normal file
39
src/database/json.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::database::DatabaseRead;
|
||||
|
||||
use super::DatabaseWrite;
|
||||
|
||||
struct DatabaseReaderJson {}
|
||||
|
||||
impl DatabaseRead for DatabaseReaderJson {
|
||||
fn read<D>(path: &Path, collection: &mut D) -> Result<(), std::io::Error>
|
||||
where
|
||||
D: DeserializeOwned,
|
||||
{
|
||||
// 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
|
||||
let mut serialized = String::new();
|
||||
File::open(path)?.read_to_string(&mut serialized)?;
|
||||
|
||||
*collection = serde_json::from_str(&serialized)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct DatabaseWriterJson {}
|
||||
|
||||
impl DatabaseWrite for DatabaseWriterJson {
|
||||
fn write<S>(path: &Path, collection: &S) -> Result<(), std::io::Error>
|
||||
where
|
||||
S: Serialize,
|
||||
{
|
||||
let serialized = serde_json::to_string(&collection)?;
|
||||
File::open(path)?.write_all(serialized.as_bytes())
|
||||
}
|
||||
}
|
18
src/database/mod.rs
Normal file
18
src/database/mod.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use std::path::Path;
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
mod json;
|
||||
|
||||
trait DatabaseRead {
|
||||
fn read<D>(path: &Path, collection: &mut D) -> Result<(), std::io::Error>
|
||||
where
|
||||
D: DeserializeOwned;
|
||||
}
|
||||
|
||||
trait DatabaseWrite {
|
||||
fn write<S>(path: &Path, collection: &S) -> Result<(), std::io::Error>
|
||||
where
|
||||
S: Serialize;
|
||||
}
|
@ -3,6 +3,8 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid;
|
||||
|
||||
mod database;
|
||||
|
||||
/// [MusicBrainz Identifier](https://musicbrainz.org/doc/MusicBrainz_Identifier) (MBID).
|
||||
pub type Mbid = uuid::Uuid;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user