summaryrefslogtreecommitdiff
path: root/src/object.rs
blob: e3039005fffc45c93ee57d8f81ca2f921c8faa51 (plain)
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::{FileInfo, Id, Patch};

fn object_path(pj_dir: impl AsRef<Path>, object: &impl Id, file_type: &str) -> PathBuf {
	let id = object.id();
	Path::join(pj_dir.as_ref(), format!("objects/{id}.{file_type}"))
}

pub fn file_info_object_path(pj_dir: impl AsRef<Path>, file: &FileInfo) -> PathBuf {
	object_path(pj_dir, file, "file")
}

pub fn patch_object_path(pj_dir: impl AsRef<Path>, patch: &Patch) -> PathBuf {
	object_path(pj_dir, patch, "patch")
}

pub fn write_object(writer: impl Write, object: &impl Serialize) -> std::io::Result<()> {
	serde_json::to_writer_pretty(writer, &object)?;
	Ok(())
}

pub fn read_object<T: DeserializeOwned>(reader: impl Read) -> serde_json::Result<T> {
	serde_json::from_reader(reader)
}