2025-02-08 18:59:45 -05:00
|
|
|
use std::path::Path;
|
2025-02-08 17:27:20 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
2025-02-08 18:01:59 -05:00
|
|
|
use crate::error::CustomError;
|
|
|
|
|
|
2025-02-08 19:23:19 -05:00
|
|
|
use super::dependency::Dependency;
|
|
|
|
|
|
2025-02-08 17:27:20 -05:00
|
|
|
pub(crate) type RefDependencyManager = std::sync::Arc<std::sync::Mutex<DependencyManager>>;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(crate) struct DependencyManager {
|
|
|
|
|
/// A stack of paths for the files being visited.
|
|
|
|
|
///
|
|
|
|
|
/// The last entry is the current file being processed. This can be used for handling relative-path links.
|
|
|
|
|
file_stack: Vec<PathBuf>,
|
2025-02-08 19:23:19 -05:00
|
|
|
|
|
|
|
|
dependencies: Vec<Dependency>,
|
2025-02-08 17:27:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DependencyManager {
|
|
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
|
DependencyManager {
|
|
|
|
|
file_stack: Vec::new(),
|
2025-02-08 19:23:19 -05:00
|
|
|
dependencies: Vec::new(),
|
2025-02-08 17:27:20 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-08 18:01:59 -05:00
|
|
|
|
|
|
|
|
pub(crate) fn push_file<P>(&mut self, path: P) -> Result<(), CustomError>
|
|
|
|
|
where
|
|
|
|
|
P: Into<PathBuf>,
|
|
|
|
|
{
|
|
|
|
|
self.file_stack.push(path.into());
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn pop_file(&mut self) -> Result<(), CustomError> {
|
|
|
|
|
self.file_stack
|
|
|
|
|
.pop()
|
|
|
|
|
.expect("Popped more files off the dependency manager file stack than exist.");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-02-08 18:59:45 -05:00
|
|
|
|
|
|
|
|
pub(crate) fn get_current_folder(&self) -> Result<&Path, CustomError> {
|
|
|
|
|
Ok(self
|
|
|
|
|
.file_stack
|
|
|
|
|
.last()
|
|
|
|
|
.ok_or("No current file")?
|
|
|
|
|
.parent()
|
|
|
|
|
.ok_or("Current file was not in a directory")?)
|
|
|
|
|
}
|
2025-02-08 19:23:19 -05:00
|
|
|
|
|
|
|
|
pub(crate) fn mark_file_for_copying<P>(&mut self, path: P) -> Result<(), CustomError>
|
|
|
|
|
where
|
|
|
|
|
P: Into<PathBuf>,
|
|
|
|
|
{
|
|
|
|
|
self.dependencies.push(Dependency::StaticFile {
|
|
|
|
|
absolute_path: path.into(),
|
|
|
|
|
});
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-02-08 17:27:20 -05:00
|
|
|
}
|