Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/asimov-directory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ tokio = ["dep:tokio"]
[dependencies]
asimov-config.workspace = true
asimov-core.workspace = true
asimov-module = { workspace = true, features = ["serde"] }
derive_more = { workspace = true, features = ["display"] }
serde_json = { workspace = true }
serde_yaml_ng = { workspace = true }

# Integrations:
camino = { workspace = true, optional = true }
Expand Down
6 changes: 5 additions & 1 deletion lib/asimov-directory/src/fs/module_directory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This is free and unencumbered software released into the public domain.

use super::{ModuleNameIterator, StateDirectory};
use super::{ModuleManifestIterator, ModuleNameIterator, StateDirectory};
use alloc::format;
use camino::Utf8PathBuf;
use derive_more::Display;
Expand Down Expand Up @@ -47,6 +47,10 @@ impl ModuleDirectory {
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()
}
Expand Down
41 changes: 41 additions & 0 deletions lib/asimov-directory/src/fs/module_iterators.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This is free and unencumbered software released into the public domain.

use asimov_core::ModuleName;
use asimov_module::InstalledModuleManifest;
use camino::Utf8PathBuf;
use std::io::Result;
use tokio::fs::ReadDir;
Expand Down Expand Up @@ -38,3 +39,43 @@ impl crate::ModuleNameIterator for ModuleNameIterator {
None
}
}

/// An iterator over module manifests in a module directory.
#[derive(Debug)]
pub struct ModuleManifestIterator {
dir: ReadDir,
}

impl ModuleManifestIterator {
pub async fn new(path: Utf8PathBuf) -> Result<Self> {
Ok(ModuleManifestIterator {
dir: tokio::fs::read_dir(path).await?,
})
}
}

impl crate::ModuleManifestIterator for ModuleManifestIterator {
async fn next(&mut self) -> Option<InstalledModuleManifest> {
while let Some(entry) = self.dir.next_entry().await.transpose() {
if let Ok(entry) = entry
&& let Some(entry_name) = entry.file_name().to_str()
&& !entry_name.starts_with(".")
&& let Ok(entry_type) = entry.file_type().await
&& (entry_type.is_file() || entry_type.is_symlink())
&& let Some(ext) = std::path::Path::new(entry_name)
.extension()
.and_then(|ext| ext.to_str())
&& (ext == "json" || ext == "yaml" || ext == "yml")
&& let Ok(content) = tokio::fs::read(entry.path()).await
&& let Some(manifest) = match ext {
"json" => serde_json::from_slice(&content).ok(),
"yaml" | "yml" => serde_yaml_ng::from_slice(&content).ok(),
_ => None,
}
{
return Some(manifest);
}
}
None
}
}
6 changes: 6 additions & 0 deletions lib/asimov-directory/src/module_iterators.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// This is free and unencumbered software released into the public domain.

use asimov_core::ModuleName;
use asimov_module::InstalledModuleManifest;

/// An iterator over module names in a module directory.
pub trait ModuleNameIterator {
fn next(&mut self) -> impl Future<Output = Option<ModuleName>> + Send;
}

/// An iterator over module manifests in a module directory.
pub trait ModuleManifestIterator {
fn next(&mut self) -> impl Future<Output = Option<InstalledModuleManifest>> + Send;
}

/// An iterator over module names in a module directory.
pub trait BlockingModuleNameIterator {
fn next(&mut self) -> Option<ModuleName>;
Expand Down