-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
128 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/target | ||
|
||
/testfiles |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ edition = "2021" | |
[dependencies] | ||
pest = "2.7.6" | ||
pest_derive = "2.7.6" | ||
walkdir = "2.4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
/// parsers contains the relevant grammar parsers | ||
pub mod parsers; | ||
|
||
/// search contains functions for searching files and tags. | ||
pub mod search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
use tag::search::get_tags_from_files; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let tagged_files = get_tags_from_files("testfiles")?; | ||
|
||
for file in tagged_files.iter() { | ||
println!("File {} contains {:?}", file.path.display(), file.tags); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::{ | ||
fs, | ||
io::{BufRead, BufReader}, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use pest::Parser; | ||
use walkdir::WalkDir; | ||
|
||
use crate::parsers::tagline::{self, TaglineParser}; | ||
|
||
/// TaggedFile is a file that contains tags. | ||
#[derive(Clone)] | ||
pub struct TaggedFile { | ||
pub path: PathBuf, | ||
pub tags: Vec<String>, | ||
} | ||
|
||
/// get_tags_from_file() returns a list of tags found in a file. | ||
/// It will return an error if a file has no parsable tags. | ||
fn get_tags_from_file(file: &Path) -> Result<Vec<String>, Box<dyn std::error::Error>> { | ||
let file = fs::File::open(file)?; | ||
let mut buffer = BufReader::new(file); | ||
let mut tagline = String::new(); | ||
let _ = buffer.read_line(&mut tagline)?; | ||
|
||
let parsed = TaglineParser::parse(tagline::Rule::tagline, tagline.trim())?; | ||
|
||
let mut tags = Vec::new(); | ||
|
||
for tag in parsed { | ||
if tag.as_rule() == tagline::Rule::tag { | ||
tags.push(tag.as_str().to_string()) | ||
} | ||
} | ||
|
||
Ok(tags) | ||
} | ||
|
||
/// get_tags_from_files() recursively retrieves the tags of all files | ||
/// in a given directory. | ||
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?; | ||
|
||
if entry.file_type().is_dir() { | ||
continue; | ||
} | ||
|
||
let tags = get_tags_from_file(entry.path())?; | ||
tagged_files.push(TaggedFile { | ||
path: entry.path().to_owned(), | ||
tags, | ||
}) | ||
} | ||
|
||
Ok(tagged_files.clone()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters