use super::{ModuleManifestIterator, ModuleNameIterator, StateDirectory};
use alloc::format;
use camino::Utf8PathBuf;
use derive_more::Display;
use std::{
io::{Error, ErrorKind, Result},
path::Path,
};
#[derive(Debug, Display)]
#[display("ModuleDirectory({:?})", path)]
pub struct ModuleDirectory {
pub(crate) path: Utf8PathBuf,
}
impl ModuleDirectory {
pub fn home() -> Result<Self> {
StateDirectory::home().map(|base_dir| base_dir.modules())?
}
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref();
if !path.exists() {
std::fs::create_dir_all(path)?;
}
let path = Utf8PathBuf::from_path_buf(path.to_path_buf()).map_err(|e| {
Error::new(
ErrorKind::InvalidFilename,
format!("failed to open non-UTF-8 path: {}", e.display()),
)
})?;
Ok(ModuleDirectory { path })
}
pub async fn iter_enabled(&self) -> Result<ModuleNameIterator> {
ModuleNameIterator::new(self.path.join("enabled")).await
}
pub async fn iter_installed(&self) -> Result<ModuleNameIterator> {
ModuleNameIterator::new(self.path.join("installed")).await
}
pub async fn iter_manifests(&self) -> Result<ModuleManifestIterator> {
ModuleManifestIterator::new(self.path.join("installed")).await
}
pub fn as_str(&self) -> &str {
self.path.as_str()
}
}
impl AsRef<str> for ModuleDirectory {
fn as_ref(&self) -> &str {
self.path.as_str()
}
}
impl AsRef<Path> for ModuleDirectory {
fn as_ref(&self) -> &Path {
self.path.as_std_path()
}
}
#[cfg(feature = "camino")]
impl AsRef<Utf8Path> for ModuleDirectory {
fn as_ref(&self) -> &Utf8Path {
self.path.as_path()
}
}
impl crate::ModuleDirectory for ModuleDirectory {
fn is_installed(&self, _module_name: impl AsRef<str>) -> bool {
false }
fn is_enabled(&self, _module_name: impl AsRef<str>) -> bool {
false }
}