asimov-directory 25.2.0

ASIMOV Software Development Kit (SDK) for Rust
// This is free and unencumbered software released into the public domain.

use super::{ModuleManifestIterator, ModuleNameIterator, StateDirectory};
use alloc::format;
use camino::Utf8PathBuf;
use derive_more::Display;
use std::{
    io::{Error, ErrorKind, Result},
    path::Path,
};

/// A module directory stored on a file system (e.g., `$HOME/.asimov/modules/`).
#[derive(Debug, Display)]
#[display("ModuleDirectory({:?})", path)]
pub struct ModuleDirectory {
    pub(crate) path: Utf8PathBuf,
}

impl ModuleDirectory {
    /// Opens the default module directory in the user's home directory.
    ///
    /// On Unix platforms, including macOS and Linux, this is `$HOME/.asimov/modules/`.
    pub fn home() -> Result<Self> {
        StateDirectory::home().map(|base_dir| base_dir.modules())?
    }

    /// Opens a module directory from a file system path.
    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 // TODO
    }

    fn is_enabled(&self, _module_name: impl AsRef<str>) -> bool {
        false // TODO
    }
}