mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-07-10 17:29:06 +02:00
Add version field to file metadata
This commit is contained in:
parent
4d6644f427
commit
6077138292
6 changed files with 26 additions and 8 deletions
13
src/io/fs.rs
13
src/io/fs.rs
|
@ -8,8 +8,12 @@ use std::{
|
|||
use anyhow::{Context, Ok, Result};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize)]
|
||||
pub struct FileMetaVersion(pub u32);
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct FileMeta {
|
||||
version: FileMetaVersion,
|
||||
timestamp: SystemTime,
|
||||
}
|
||||
|
||||
|
@ -111,7 +115,12 @@ pub fn modified_timestamp(path: &Path) -> Result<SystemTime> {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn create_with_timestamp<T, F>(path: &Path, timestamp: SystemTime, f: F) -> Result<T>
|
||||
pub fn create_with_timestamp<T, F>(
|
||||
path: &Path,
|
||||
version: FileMetaVersion,
|
||||
timestamp: SystemTime,
|
||||
f: F,
|
||||
) -> Result<T>
|
||||
where
|
||||
F: FnOnce(&mut BufWriter<File>) -> Result<T>,
|
||||
{
|
||||
|
@ -119,7 +128,7 @@ where
|
|||
|
||||
let meta_path = metafile_name(path);
|
||||
create(&meta_path, |file| {
|
||||
serde_json::to_writer(file, &FileMeta { timestamp })?;
|
||||
serde_json::to_writer(file, &FileMeta { version, timestamp })?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
|
|
|
@ -10,8 +10,13 @@ use serde::{de::DeserializeOwned, Serialize};
|
|||
|
||||
use super::fs;
|
||||
|
||||
pub fn write<T: Serialize>(path: &Path, value: &T, timestamp: SystemTime) -> Result<()> {
|
||||
fs::create_with_timestamp(path, timestamp, |file| {
|
||||
pub fn write<T: Serialize>(
|
||||
path: &Path,
|
||||
value: &T,
|
||||
version: fs::FileMetaVersion,
|
||||
timestamp: SystemTime,
|
||||
) -> Result<()> {
|
||||
fs::create_with_timestamp(path, version, timestamp, |file| {
|
||||
let data = bincode::serialize(value)?;
|
||||
let len = u32::try_from(data.len())?;
|
||||
let compressed = zstd::bulk::compress(&data, 1)?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue