Updated library serialization to cbor with ciborium

This commit is contained in:
MrDulfin 2024-12-03 23:37:28 -05:00
parent 5aa675111e
commit 9080205a7b
2 changed files with 8 additions and 20 deletions

View file

@ -25,11 +25,9 @@ lofty = "0.18.2"
serde = { version = "1.0.195", features = ["derive"] }
walkdir = "2.4.0"
chrono = { version = "0.4.31", features = ["serde"] }
bincode = { version = "2.0.0-rc.3", features = ["serde"] }
rayon = "1.8.0"
log = "0.4"
base64 = "0.21.5"
snap = "1"
rcue = "0.1.3"
gstreamer = "0.21.3"
glib = "0.18.5"
@ -55,3 +53,4 @@ futures = "0.3.30"
text_io = "0.1.12"
tokio = { version = "1.40.0", features = ["macros", "rt"] }
async-channel = "2.3.1"
ciborium = "0.2.2"

View file

@ -1,6 +1,6 @@
use ciborium::{from_reader, into_writer};
use deunicode::deunicode_with_tofu;
use file_format::{FileFormat, Kind};
use snap;
use std::error::Error;
use std::fs::{self, File};
use std::io::{BufReader, BufWriter};
@ -24,7 +24,7 @@ pub(super) fn normalize(input_string: &str) -> String {
}
/// Write any data structure which implements [serde::Serialize]
/// out to a [bincode] encoded file compressed using [snap]
/// out to a [cbor] encoded file compressed using [ciborium]
pub(super) fn write_file<
T: serde::Serialize,
U: std::convert::AsRef<Path> + std::convert::AsRef<std::ffi::OsStr> + Clone,
@ -37,17 +37,11 @@ pub(super) fn write_file<
writer_name.set_extension("tmp");
// Create a new BufWriter on the file and a snap frame encoder
let mut writer = BufWriter::new(File::create(&writer_name)?);
let writer = BufWriter::new(File::create(&writer_name)?);
//let mut e = snap::write::FrameEncoder::new(writer);
// Write out the data
bincode::serde::encode_into_std_write(
library,
&mut writer,
bincode::config::standard()
.with_little_endian()
.with_variable_int_encoding(),
)?;
into_writer(&library, writer)?;
fs::rename(writer_name, &path)?;
Ok(())
@ -59,16 +53,11 @@ pub(super) fn read_file<T: for<'de> serde::Deserialize<'de>>(
path: PathBuf,
) -> Result<T, Box<dyn Error>> {
// Create a new snap reader over the file
let mut file_reader = BufReader::new(File::open(path)?);
let file_reader = BufReader::new(File::open(path)?);
//let mut d = snap::read::FrameDecoder::new(file_reader);
// Decode the library from the serialized data into the vec
let library: T = bincode::serde::decode_from_std_read(
&mut file_reader,
bincode::config::standard()
.with_little_endian()
.with_variable_int_encoding(),
)?;
let library: T = from_reader(file_reader)?;
Ok(library)
}