Skip to content

Commit

Permalink
wrote function to get all files + tags in directory
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf committed Jan 18, 2024
1 parent 689b6c2 commit 2fb9600
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
51 changes: 51 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
pest = "2.7.6"
pest_derive = "2.7.6"
walkdir = "2.4.0"
27 changes: 19 additions & 8 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use std::{
fs,
io::{BufRead, BufReader},
path::Path,
path::{Path, PathBuf},
};

use pest::Parser;
use walkdir::WalkDir;

use crate::parsers::tagline::{self, TaglineParser};

/// TaggedFile is a file that contains tags.
pub struct TaggedFile<'a> {
path: &'a Path,
tags: Vec<&'a str>,
#[derive(Clone)]
pub struct TaggedFile {
path: PathBuf,
tags: Vec<String>,
}

/// get_tags_from_file() returns a list of tags found in a file.
Expand All @@ -38,8 +40,17 @@ fn get_tags_from_file(file: &Path) -> Result<Vec<String>, Box<dyn std::error::Er

/// get_tags_from_files() recursively retrieves the tags of all files
/// in a given directory.
pub fn get_tags_from_files(
directory: &Path,
) -> Result<Vec<TaggedFile>, Box<dyn std::error::Error>> {
todo!()
pub fn get_tags_from_files(directory: &str) -> Result<Vec<TaggedFile>, Box<dyn std::error::Error>> {
let mut tagged_files = Vec::new();

for entry in WalkDir::new(directory).follow_links(true) {
let entry = entry?;
let tags = get_tags_from_file(entry.path())?;
tagged_files.push(TaggedFile {
path: entry.path().to_owned(),
tags,
})
}

Ok(tagged_files.clone())
}

0 comments on commit 2fb9600

Please sign in to comment.