templates

This commit is contained in:
Pascal Engélibert 2026-03-03 15:15:18 +01:00
commit 2c1793a128
12 changed files with 496 additions and 26 deletions

View file

@ -4,11 +4,12 @@ use crate::{
};
use std::{
io::{ErrorKind, Read},
io::{ErrorKind, Read, Write},
path::Path,
};
const REPO_METADATA_FILE_NAME: &str = "meta.bin";
const VERSION: [u8; 1] = [0];
#[derive(Debug)]
pub struct RepoMetadata {
@ -17,6 +18,22 @@ pub struct RepoMetadata {
content: Vec<u8>,
}
#[derive(Debug)]
pub enum WriteRepoMetadataError {
AlreadyExists,
CannotOpenFile(std::io::Error),
CannotWriteFile(std::io::Error),
}
impl From<std::io::Error> for WriteRepoMetadataError {
fn from(value: std::io::Error) -> Self {
match value.kind() {
ErrorKind::AlreadyExists => Self::AlreadyExists,
_ => Self::CannotWriteFile(value),
}
}
}
#[derive(Debug)]
pub enum ReadRepoMetadataError {
CannotOpenFile(std::io::Error),
@ -41,10 +58,35 @@ impl From<std::string::FromUtf8Error> for ReadRepoMetadataError {
}
impl RepoMetadata {
pub fn new(config: &Config, repo_dir: &Path) -> Result<Self, ReadRepoMetadataError> {
pub fn write_to_file(
&self,
config: &Config,
repo_dir: &Path,
) -> Result<(), WriteRepoMetadataError> {
let mut file = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(repo_dir.join("index"))
.map_err(WriteRepoMetadataError::CannotOpenFile)?;
file.write_all(&VERSION)?;
let now = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs();
file.write_all(&now.to_be_bytes())?;
file.write_all(&(self.commit_url.len() as u32).to_be_bytes())?;
file.write_all(self.commit_url.as_bytes())?;
file.write_all(&self.content)?;
Ok(())
}
pub fn read_from_file(config: &Config, repo_dir: &Path) -> Result<Self, ReadRepoMetadataError> {
let mut file = std::fs::OpenOptions::new()
.read(true)
.open(repo_dir.join(""))
.open(repo_dir.join("index"))
.map_err(ReadRepoMetadataError::CannotOpenFile)?;
let mut version = [0u8; 1];
@ -92,8 +134,8 @@ pub struct RepoMetadataIter<'a> {
}
pub struct RepoMetadataEntry<'a> {
file_path: &'a str,
hash: &'a str,
pub file_path: &'a str,
pub hash: &'a str,
}
impl<'a> Iterator for RepoMetadataIter<'a> {