Skip to content

docs(swiftide): documented file swiftide/src/loaders/file_loader.rs #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
31 changes: 28 additions & 3 deletions swiftide/src/loaders/file_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,48 @@ use crate::{ingestion::IngestionNode, ingestion::IngestionStream, Loader};
use futures_util::{stream, StreamExt};
use std::path::PathBuf;

/// `FileLoader` is responsible for loading files from the filesystem based on specified extensions.
/// It provides functionality to list and stream files for ingestion into the Swiftide pipeline.
pub struct FileLoader {
pub(crate) path: PathBuf,
pub(crate) extensions: Vec<String>,
}

impl FileLoader {
/// Creates a new `FileLoader` instance with the specified path.
///
/// # Arguments
/// * `path` - The root directory path from which files will be loaded.
///
/// # Returns
/// A new instance of `FileLoader`.
pub fn new(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
extensions: vec![],
}
}

/// Add extensions to the loader
/// Adds file extensions to the loader.
///
/// # Arguments
/// * `extensions` - A list of extensions to add without the leading dot
/// * `extensions` - A slice of extensions to add without the leading dot.
///
/// # Returns
/// The `FileLoader` instance with the added extensions.
pub fn with_extensions(mut self, extensions: &[&str]) -> Self {
self.extensions
.extend(extensions.iter().map(ToString::to_string));
self
}

/// Debug method
/// Lists the nodes (files) that match the specified extensions.
///
/// # Returns
/// A vector of `IngestionNode` representing the files that match the specified extensions.
///
/// # Panics
/// This method will panic if it fails to read the file contents.
pub fn list_nodes(&self) -> Vec<IngestionNode> {
ignore::Walk::new(&self.path)
.filter_map(|entry| entry.ok())
Expand Down Expand Up @@ -54,6 +72,13 @@ impl FileLoader {
}

impl Loader for FileLoader {
/// Converts the `FileLoader` into an `IngestionStream`.
///
/// # Returns
/// An `IngestionStream` that streams `IngestionNode` instances representing the files that match the specified extensions.
///
/// # Errors
/// This method will return an error if it fails to read the file contents.
fn into_stream(self) -> IngestionStream {
let file_paths = ignore::Walk::new(self.path)
.filter_map(|entry| entry.ok())
Expand Down