From a16f1ce105959b37bd5776476352b92905ff12d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Fri, 27 Sep 2024 16:05:04 +0200 Subject: [PATCH] Generalize VFolder, implement Read & Write for VFile and improve docs --- .github/workflows/test.yml | 4 +++ src/datapack/command/execute.rs | 2 ++ src/virtual_fs.rs | 56 ++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 74d11be..845e627 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,10 @@ name: Cargo build & test on: push: + branches: + - main + - development + - 'releases/**' pull_request: env: diff --git a/src/datapack/command/execute.rs b/src/datapack/command/execute.rs index ec56462..573b01e 100644 --- a/src/datapack/command/execute.rs +++ b/src/datapack/command/execute.rs @@ -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)] diff --git a/src/virtual_fs.rs b/src/virtual_fs.rs index aee8aec..7d5328e 100644 --- a/src/virtual_fs.rs +++ b/src/virtual_fs.rs @@ -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

(&self, path: P) -> std::io::Result<()> + where + P: AsRef, + { 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

(&self, path: P) -> std::io::Result<()> + where + P: AsRef, + { 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(&self, path: &Path, comment: S) -> std::io::Result<()> + pub fn zip_with_comment(&self, path: P, comment: S) -> std::io::Result<()> where + P: AsRef, S: Into, { use std::{fs, io::Write}; @@ -315,6 +323,46 @@ pub enum VFile { Binary(Vec), } +impl std::io::Read for VFile { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + self.as_bytes().read(buf) + } + + fn read_to_string(&mut self, buf: &mut String) -> std::io::Result { + 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 { + 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 for VFile { fn from(value: String) -> Self { Self::Text(value)