Set lint warnings and add docs

This commit is contained in:
Moritz Hölting 2024-03-21 16:48:54 +01:00
parent 343f014e6c
commit 179b55ad4d
2 changed files with 55 additions and 26 deletions

View File

@ -1 +1,13 @@
//! Shulkerbox is a library for creating Minecraft data packs.
#![warn(
missing_docs,
missing_debug_implementations,
nonstandard_style,
clippy::complexity,
clippy::style,
clippy::suspicious
)]
#![deny(unsafe_code)]
pub mod virtual_fs; pub mod virtual_fs;

View File

@ -1,11 +1,15 @@
//! Virtual file system for creating and manipulating files and folders in memory.
use std::{collections::HashMap, fs, io, path::Path}; use std::{collections::HashMap, fs, io, path::Path};
/// Folder representation in virtual file system
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct VFolder { pub struct VFolder {
folders: HashMap<String, VFolder>, folders: HashMap<String, VFolder>,
files: HashMap<String, VFile>, files: HashMap<String, VFile>,
} }
impl VFolder { impl VFolder {
/// Create a new, empty virtual folder.
pub fn new() -> VFolder { pub fn new() -> VFolder {
VFolder { VFolder {
folders: HashMap::new(), folders: HashMap::new(),
@ -13,21 +17,25 @@ impl VFolder {
} }
} }
/// Get all direct subfolders in the folder.
pub fn get_folders(&self) -> &HashMap<String, VFolder> { pub fn get_folders(&self) -> &HashMap<String, VFolder> {
&self.folders &self.folders
} }
/// Get all direct files in the folder.
pub fn get_files(&self) -> &HashMap<String, VFile> { pub fn get_files(&self) -> &HashMap<String, VFile> {
&self.files &self.files
} }
pub fn add_folder(&mut self, name: &str) { /// Recursively add a new folder to the folder.
self.add_existing_folder(name, VFolder::new()); pub fn add_folder(&mut self, path: &str) {
self.add_existing_folder(path, VFolder::new());
} }
pub fn add_existing_folder(&mut self, name: &str, folder: VFolder) { /// Recursively add an existing folder to the folder.
let (head, tail) = name pub fn add_existing_folder(&mut self, path: &str, folder: VFolder) {
let (head, tail) = path
.split_once('/') .split_once('/')
.map(|(h, t)| (h, (!t.is_empty()).then_some(t))) .map(|(h, t)| (h, (!t.is_empty()).then_some(t)))
.unwrap_or((name, None)); .unwrap_or((path, None));
if let Some(tail) = tail { if let Some(tail) = tail {
if let Some(subfolder) = self.get_folder_mut(head) { if let Some(subfolder) = self.get_folder_mut(head) {
subfolder.add_folder(tail); subfolder.add_folder(tail);
@ -37,14 +45,15 @@ impl VFolder {
self.add_existing_folder(head, new_folder); self.add_existing_folder(head, new_folder);
} }
} else { } else {
self.folders.insert(name.to_string(), folder); self.folders.insert(path.to_string(), folder);
} }
} }
pub fn add_file(&mut self, name: &str, file: VFile) { /// Recursively add a new file to the folder.
let (head, tail) = name pub fn add_file(&mut self, path: &str, file: VFile) {
let (head, tail) = path
.split_once('/') .split_once('/')
.map(|(h, t)| (h, (!t.is_empty()).then_some(t))) .map(|(h, t)| (h, (!t.is_empty()).then_some(t)))
.unwrap_or((name, None)); .unwrap_or((path, None));
if let Some(tail) = tail { if let Some(tail) = tail {
if let Some(subfolder) = self.get_folder_mut(head) { if let Some(subfolder) = self.get_folder_mut(head) {
subfolder.add_file(tail, file); subfolder.add_file(tail, file);
@ -54,55 +63,60 @@ impl VFolder {
self.add_existing_folder(head, new_folder); self.add_existing_folder(head, new_folder);
} }
} else { } else {
self.files.insert(name.to_string(), file); self.files.insert(path.to_string(), file);
} }
} }
pub fn get_folder(&self, name: &str) -> Option<&VFolder> { /// Recursively get a subfolder by path.
let (head, tail) = name pub fn get_folder(&self, path: &str) -> Option<&VFolder> {
let (head, tail) = path
.split_once('/') .split_once('/')
.map(|(h, t)| (h, (!t.is_empty()).then_some(t))) .map(|(h, t)| (h, (!t.is_empty()).then_some(t)))
.unwrap_or((name, None)); .unwrap_or((path, None));
if let Some(tail) = tail { if let Some(tail) = tail {
self.folders.get(head)?.get_folder(tail) self.folders.get(head)?.get_folder(tail)
} else { } else {
self.folders.get(name) self.folders.get(path)
} }
} }
pub fn get_folder_mut(&mut self, name: &str) -> Option<&mut VFolder> { /// Recursively get a mutable subfolder by path.
let (head, tail) = name pub fn get_folder_mut(&mut self, path: &str) -> Option<&mut VFolder> {
let (head, tail) = path
.split_once('/') .split_once('/')
.map(|(h, t)| (h, (!t.is_empty()).then_some(t))) .map(|(h, t)| (h, (!t.is_empty()).then_some(t)))
.unwrap_or((name, None)); .unwrap_or((path, None));
if let Some(tail) = tail { if let Some(tail) = tail {
self.folders.get_mut(head)?.get_folder_mut(tail) self.folders.get_mut(head)?.get_folder_mut(tail)
} else { } else {
self.folders.get_mut(name) self.folders.get_mut(path)
} }
} }
pub fn get_file(&self, name: &str) -> Option<&VFile> { /// Recursively get a file by path.
let (head, tail) = name pub fn get_file(&self, path: &str) -> Option<&VFile> {
let (head, tail) = path
.split_once('/') .split_once('/')
.map(|(h, t)| (h, (!t.is_empty()).then_some(t))) .map(|(h, t)| (h, (!t.is_empty()).then_some(t)))
.unwrap_or((name, None)); .unwrap_or((path, None));
if let Some(tail) = tail { if let Some(tail) = tail {
self.folders.get(head)?.get_file(tail) self.folders.get(head)?.get_file(tail)
} else { } else {
self.files.get(name) self.files.get(path)
} }
} }
pub fn get_file_mut(&mut self, name: &str) -> Option<&mut VFile> { /// Recursively get a mutable file by path.
let (head, tail) = name pub fn get_file_mut(&mut self, path: &str) -> Option<&mut VFile> {
let (head, tail) = path
.split_once('/') .split_once('/')
.map(|(h, t)| (h, (!t.is_empty()).then_some(t))) .map(|(h, t)| (h, (!t.is_empty()).then_some(t)))
.unwrap_or((name, None)); .unwrap_or((path, None));
if let Some(tail) = tail { if let Some(tail) = tail {
self.folders.get_mut(head)?.get_file_mut(tail) self.folders.get_mut(head)?.get_file_mut(tail)
} else { } else {
self.files.get_mut(name) self.files.get_mut(path)
} }
} }
/// Place the folder and its contents on the file system.
pub fn place(&self, path: &Path) -> io::Result<()> { pub fn place(&self, path: &Path) -> io::Result<()> {
fs::create_dir_all(path)?; fs::create_dir_all(path)?;
for (name, folder) in &self.folders { for (name, folder) in &self.folders {
@ -122,9 +136,12 @@ impl VFolder {
} }
} }
/// File representation in virtual file system
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum VFile { pub enum VFile {
/// Text file
Text(String), Text(String),
/// Binary file
Binary(Vec<u8>), Binary(Vec<u8>),
} }