From f35d18177fe6a4f769ac8bb647fce3ae8c66c3b1 Mon Sep 17 00:00:00 2001 From: SpontanCombust <61706594+SpontanCombust@users.noreply.github.com> Date: Tue, 20 Feb 2024 02:07:05 +0100 Subject: [PATCH] content scanning moved to `content.rs` --- crates/project/src/content.rs | 64 ++++++++++++++++++++++++ crates/project/src/content_repository.rs | 52 ++----------------- 2 files changed, 69 insertions(+), 47 deletions(-) diff --git a/crates/project/src/content.rs b/crates/project/src/content.rs index e3f9298b..03b59ceb 100644 --- a/crates/project/src/content.rs +++ b/crates/project/src/content.rs @@ -1,6 +1,8 @@ use std::path::{Path, PathBuf}; +use std::sync::Arc; use crate::manifest::{Manifest, ManifestError}; use crate::source_tree::SourceTree; +use crate::FileError; #[derive(Debug, Clone)] @@ -17,6 +19,46 @@ impl ContentDirectory { } } + pub fn find_in

(path: P, manifest_required: bool, scan_recursively: bool) -> (Vec, Vec) + where P: AsRef { + let mut contents = Vec::new(); + let mut errors = Vec::new(); + + match std::fs::read_dir(path.as_ref()) { + Ok(iter) => { + for entry in iter { + match entry { + Ok(entry) => { + let content_path_candidate = entry.path(); + if is_content_dir(&content_path_candidate, manifest_required) { + contents.push(ContentDirectory::new(content_path_candidate)); + } else if content_path_candidate.is_dir() && scan_recursively { + let (inner_contents, inner_errors) = Self::find_in(content_path_candidate, manifest_required, scan_recursively); + contents.extend(inner_contents); + errors.extend(inner_errors); + } + }, + Err(err) => { + errors.push(FileError { + path: path.as_ref().to_owned(), + error: Arc::new(err) + }); + } + } + } + }, + Err(err) => { + errors.push(FileError { + path: path.as_ref().to_owned(), + error: Arc::new(err) + }); + } + } + + (contents, errors) + } + + pub fn path(&self) -> &Path { &self.path } @@ -48,3 +90,25 @@ impl ContentDirectory { SourceTree::new(script_root) } } + + +fn is_content_dir(path: &Path, manifest_required: bool) -> bool { + if !path.is_dir() { + return false; + } + + let manifest_path = path.join(Manifest::FILE_NAME); + if manifest_path.exists() { + return true; + } + if manifest_required { + return false; + } + + let scripts_path = path.join("content").join("scripts"); + if scripts_path.is_dir() { + return true; + } + + false +} \ No newline at end of file diff --git a/crates/project/src/content_repository.rs b/crates/project/src/content_repository.rs index 019c6307..b7cd3a48 100644 --- a/crates/project/src/content_repository.rs +++ b/crates/project/src/content_repository.rs @@ -1,5 +1,5 @@ -use std::{path::{Path, PathBuf}, sync::Arc}; -use crate::{ContentDirectory, FileError, Manifest}; +use std::path::{Path, PathBuf}; +use crate::{ContentDirectory, FileError}; /// Looks for content in provided "repository" directories. @@ -40,51 +40,9 @@ impl ContentRepositories { self.errors.clear(); for repo in &self.repository_paths { - match std::fs::read_dir(repo) { - Ok(iter) => { - for entry in iter { - match entry { - Ok(entry) => { - let path = entry.path(); - if path.is_dir() && is_content_dir(&path) { - self.found_content.push(ContentDirectory::new(path)); - } - }, - Err(err) => { - self.errors.push(FileError { - path: repo.to_owned(), - error: Arc::new(err) - }); - } - } - } - }, - Err(err) => { - self.errors.push(FileError { - path: repo.to_owned(), - error: Arc::new(err) - }); - } - } + let (contents, errors) = ContentDirectory::find_in(repo, false, false); + self.found_content.extend(contents); + self.errors.extend(errors); } } -} - - -fn is_content_dir(path: &Path) -> bool { - if !path.is_dir() { - return false; - } - - let manifest_path = path.join(Manifest::FILE_NAME); - if manifest_path.exists() { - return true; - } - - let scripts_path = path.join("content").join("scripts"); - if scripts_path.is_dir() { - return true; - } - - false } \ No newline at end of file