2026-02-19 10:09:18 +00:00
|
|
|
use std::fs;
|
2026-02-09 22:58:16 +00:00
|
|
|
use std::io;
|
2026-02-19 10:09:18 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2026-02-09 22:58:16 +00:00
|
|
|
|
|
|
|
|
/// Resource payload that can be either borrowed from mapped bytes or owned.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub enum ResourceData<'a> {
|
|
|
|
|
Borrowed(&'a [u8]),
|
|
|
|
|
Owned(Vec<u8>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> ResourceData<'a> {
|
|
|
|
|
pub fn as_slice(&self) -> &[u8] {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Borrowed(slice) => slice,
|
|
|
|
|
Self::Owned(buf) => buf.as_slice(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn into_owned(self) -> Vec<u8> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Borrowed(slice) => slice.to_vec(),
|
|
|
|
|
Self::Owned(buf) => buf,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsRef<[u8]> for ResourceData<'_> {
|
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
|
self.as_slice()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Output sink used by `read_into`/`load_into` APIs.
|
|
|
|
|
pub trait OutputBuffer {
|
2026-02-11 21:43:40 +00:00
|
|
|
/// Writes the full payload to the sink, replacing any previous content.
|
2026-02-09 22:58:16 +00:00
|
|
|
fn write_exact(&mut self, data: &[u8]) -> io::Result<()>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl OutputBuffer for Vec<u8> {
|
|
|
|
|
fn write_exact(&mut self, data: &[u8]) -> io::Result<()> {
|
|
|
|
|
self.clear();
|
|
|
|
|
self.extend_from_slice(data);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-19 10:09:18 +00:00
|
|
|
|
|
|
|
|
/// Recursively collects all files under `root`.
|
|
|
|
|
pub fn collect_files_recursive(root: &Path, out: &mut Vec<PathBuf>) {
|
|
|
|
|
let Ok(entries) = fs::read_dir(root) else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
for entry in entries.flatten() {
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
if path.is_dir() {
|
|
|
|
|
collect_files_recursive(&path, out);
|
|
|
|
|
} else if path.is_file() {
|
|
|
|
|
out.push(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|