Skip to content

Commit

Permalink
wrote "test" and fixed search problem
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf committed Jan 18, 2024
1 parent 2fb9600 commit 7eb038e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target

/testfiles
12 changes: 10 additions & 2 deletions src/main.rs
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(())
}
18 changes: 11 additions & 7 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::parsers::tagline::{self, TaglineParser};
/// TaggedFile is a file that contains tags.
#[derive(Clone)]
pub struct TaggedFile {
path: PathBuf,
tags: Vec<String>,
pub path: PathBuf,
pub tags: Vec<String>,
}

/// get_tags_from_file() returns a list of tags found in a file.
Expand All @@ -24,14 +24,13 @@ fn get_tags_from_file(file: &Path) -> Result<Vec<String>, Box<dyn std::error::Er
let mut tagline = String::new();
let _ = buffer.read_line(&mut tagline)?;

let mut parsed = TaglineParser::parse(tagline::Rule::tagline, &tagline)?;
let parsed = TaglineParser::parse(tagline::Rule::tagline, tagline.trim())?;

let mut tags = Vec::new();

for tag in parsed.next().unwrap().into_inner() {
match tag.as_rule() {
tagline::Rule::tag => tags.push(tag.as_str().to_string()),
_ => unreachable!(),
for tag in parsed {
if tag.as_rule() == tagline::Rule::tag {
tags.push(tag.as_str().to_string())
}
}

Expand All @@ -45,6 +44,11 @@ pub fn get_tags_from_files(directory: &str) -> Result<Vec<TaggedFile>, Box<dyn s

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(),
Expand Down

0 comments on commit 7eb038e

Please sign in to comment.