Generalize VFolder, implement Read & Write for VFile and improve docs

This commit is contained in:
Moritz Hölting 2024-09-27 16:05:04 +02:00
parent 60458e6b2d
commit a16f1ce105
3 changed files with 58 additions and 4 deletions

View File

@ -1,6 +1,10 @@
name: Cargo build & test
on:
push:
branches:
- main
- development
- 'releases/**'
pull_request:
env:

View File

@ -8,6 +8,7 @@ use crate::util::{
ExtendableQueue,
};
/// Execute command with all its variants.
#[allow(missing_docs)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -369,6 +370,7 @@ fn combine_conditions_commands(
.collect()
}
/// Condition for the execute command.
#[allow(missing_docs)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

View File

@ -131,13 +131,17 @@ impl VFolder {
/// # Errors
/// - If the folder cannot be written
#[cfg(feature = "fs_access")]
pub fn place(&self, path: &Path) -> std::io::Result<()> {
pub fn place<P>(&self, path: P) -> std::io::Result<()>
where
P: AsRef<Path>,
{
use std::fs;
let path = path.as_ref();
fs::create_dir_all(path)?;
// place each subfolder recursively
for (name, folder) in &self.folders {
folder.place(&path.join(name))?;
folder.place(path.join(name))?;
}
// create each file
for (name, file) in &self.files {
@ -158,7 +162,10 @@ impl VFolder {
/// # Errors
/// - If the zip archive cannot be written
#[cfg(all(feature = "fs_access", feature = "zip"))]
pub fn zip(&self, path: &Path) -> std::io::Result<()> {
pub fn zip<P>(&self, path: P) -> std::io::Result<()>
where
P: AsRef<Path>,
{
use std::{fs, io::Write};
// open target file
@ -191,8 +198,9 @@ impl VFolder {
/// # Errors
/// - If the zip archive cannot be written
#[cfg(all(feature = "fs_access", feature = "zip"))]
pub fn zip_with_comment<S>(&self, path: &Path, comment: S) -> std::io::Result<()>
pub fn zip_with_comment<P, S>(&self, path: P, comment: S) -> std::io::Result<()>
where
P: AsRef<Path>,
S: Into<String>,
{
use std::{fs, io::Write};
@ -315,6 +323,46 @@ pub enum VFile {
Binary(Vec<u8>),
}
impl std::io::Read for VFile {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.as_bytes().read(buf)
}
fn read_to_string(&mut self, buf: &mut String) -> std::io::Result<usize> {
match self {
Self::Text(text) => {
buf.push_str(text);
Ok(text.len())
}
Self::Binary(data) => {
let text =
std::str::from_utf8(data).map_err(|_| std::io::ErrorKind::InvalidData)?;
buf.push_str(text);
Ok(text.len())
}
}
}
}
impl std::io::Write for VFile {
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
match self {
Self::Text(text) => {
text.push_str(&String::from_utf8_lossy(buf));
Ok(buf.len())
}
Self::Binary(data) => {
data.extend_from_slice(buf);
Ok(buf.len())
}
}
}
}
impl From<String> for VFile {
fn from(value: String) -> Self {
Self::Text(value)