Skip to content

Commit

Permalink
feat: Cli (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf authored Jan 19, 2024
1 parent cbe8d34 commit aa76c16
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 21 deletions.
179 changes: 179 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 @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.18", features = ["derive"] }
lazy_static = "1.4.0"
pest = "2.7.6"
pest_derive = "2.7.6"
Expand Down
60 changes: 45 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,54 @@ use tag::{
search::get_tags_from_files,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let tagged_files = get_tags_from_files("testfiles")?;
mod cli {
use clap::Parser;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
#[clap(value_name = "QUERY")]
/// Search query for the tags
pub query: String,

for file in tagged_files.iter() {
println!("File {} contains {:?}", file.path.display(), file.tags);
#[clap(value_name = "PATH")]
/// The path that will be searched
pub path: String,

#[arg(short, long)]
/// Only print the paths of matched files.
pub silent: bool,
}

let ast = construct_query_ast(
QueryParser::parse(Rule::tagsearch, "!#e")
.unwrap()
.next()
.unwrap()
.into_inner(),
vec!["#a", "#c", "#d"],
);

println!("{:#?}", ast);
println!("{}", evaluate_ast(ast));
impl Cli {
pub fn new_and_parse() -> Cli {
Cli::parse()
}
}
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = cli::Cli::new_and_parse();

let file_index = get_tags_from_files(args.path.as_str())?;
let query = QueryParser::parse(Rule::tagsearch, args.query.as_str())?;

for file in file_index.iter() {
let ast = construct_query_ast(
query.clone().next().unwrap().into_inner(),
file.tags.iter().map(|tag| tag.as_str()).collect(),
);

if !evaluate_ast(ast) {
continue;
}

println!("{}", file.path.display());

if !args.silent {
println!("\ttags:{:?} ", file.tags);
}
}

Ok(())
}
15 changes: 9 additions & 6 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use walkdir::WalkDir;
use crate::parsers::tagline::{self, TaglineParser};

/// TaggedFile is a file that contains tags.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct TaggedFile {
pub path: PathBuf,
pub tags: Vec<String>,
Expand Down Expand Up @@ -49,11 +49,14 @@ pub fn get_tags_from_files(directory: &str) -> Result<Vec<TaggedFile>, Box<dyn s
continue;
}

let tags = get_tags_from_file(entry.path())?;
tagged_files.push(TaggedFile {
path: entry.path().to_owned(),
tags,
})
let tags = get_tags_from_file(entry.path());

if let Ok(tags) = tags {
tagged_files.push(TaggedFile {
path: entry.path().to_owned(),
tags,
})
}
}

Ok(tagged_files.clone())
Expand Down

0 comments on commit aa76c16

Please sign in to comment.