-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --regex, and --first flags. Plus bug fixes
Fixed an issue with rogue newlines appearing in command output.
- Loading branch information
Showing
7 changed files
with
102 additions
and
34 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
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
This file was deleted.
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
mod regex; | ||
mod simple; | ||
|
||
pub use regex::RegexMatcher; | ||
pub use simple::SimpleMatcher; | ||
pub use self::regex::RegexMatcher; | ||
use crate::document::Document; | ||
use crate::Opts; | ||
pub use simple::SimpleMatcher; | ||
|
||
pub trait Matcher { | ||
/// Find any sections within the document that match | ||
pub fn match(document: &Document, options: &Opts) -> Document; | ||
fn get_matches(document: &Document, opts: &Opts) -> Document; | ||
} |
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,24 @@ | ||
use super::Matcher; | ||
use crate::document::Document; | ||
use crate::Opts; | ||
use regex::RegexBuilder; | ||
|
||
pub struct RegexMatcher; | ||
|
||
impl Matcher for RegexMatcher { | ||
/// Compile the pattern as a regular expression | ||
fn get_matches(document: &Document, opts: &Opts) -> Document { | ||
// Compile regex for provided pattern | ||
let re = RegexBuilder::new(&opts.pattern) | ||
.case_insensitive(!opts.case_sensitive) | ||
.size_limit(1024 * 100) // 100 kb | ||
.build() | ||
.unwrap(); | ||
|
||
document | ||
.iter() | ||
.filter(|section| re.is_match(§ion.title)) | ||
.cloned() | ||
.collect() | ||
} | ||
} |
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,22 @@ | ||
use super::Matcher; | ||
use crate::document::Document; | ||
use crate::Opts; | ||
|
||
pub struct SimpleMatcher; | ||
|
||
impl Matcher for SimpleMatcher { | ||
/// Performs a simple pattern == title match | ||
fn get_matches(document: &Document, opts: &Opts) -> Document { | ||
document | ||
.iter() | ||
.filter(|section| { | ||
if opts.case_sensitive { | ||
section.title.trim() == opts.pattern.trim() | ||
} else { | ||
section.title.to_lowercase().trim() == opts.pattern.to_lowercase().trim() | ||
} | ||
}) | ||
.cloned() | ||
.collect() | ||
} | ||
} |