use std::{
fs,
path::{Path, PathBuf},
};
use tectonic_errors::prelude::*;
use tectonic_io_base::{filesystem::FilesystemIo, InputHandle, IoProvider, OpenResult};
use tectonic_status_base::StatusBackend;
use super::Bundle;
pub struct DirBundle(FilesystemIo);
impl DirBundle {
pub fn new<P: AsRef<Path>>(dir: P) -> DirBundle {
DirBundle(FilesystemIo::new(
dir.as_ref(),
false, false, Default::default(), ))
}
}
impl IoProvider for DirBundle {
fn input_open_name(
&mut self,
name: &str,
status: &mut dyn StatusBackend,
) -> OpenResult<InputHandle> {
self.0.input_open_name(name, status)
}
fn input_open_name_with_abspath(
&mut self,
name: &str,
status: &mut dyn StatusBackend,
) -> OpenResult<(InputHandle, Option<PathBuf>)> {
self.0.input_open_name_with_abspath(name, status)
}
}
impl Bundle for DirBundle {
fn all_files(&mut self, _status: &mut dyn StatusBackend) -> Result<Vec<String>> {
let mut files = Vec::new();
for entry in fs::read_dir(&self.0.root())? {
let entry = entry?;
if !entry.file_type()?.is_dir() {
if let Some(s) = entry.file_name().to_str() {
files.push(s.to_owned());
}
}
}
Ok(files)
}
}