Feat: move find_matches to a library.
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
pub fn find_matches(content: &str, pattern: &str, mut writer: impl std::io::Write) -> Result<()> {
|
||||||
|
for line in content.lines() {
|
||||||
|
if line.contains(pattern) {
|
||||||
|
writeln!(writer, "{}", line)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn find_a_match() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let mut result = Vec::new();
|
||||||
|
find_matches("lorem ipsum\ndolar sit amet", "lorem", &mut result)?;
|
||||||
|
assert_eq!(result, b"lorem ipsum\n");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
+1
-20
@@ -14,12 +14,10 @@ fn main() -> Result<()> {
|
|||||||
info!("starting up");
|
info!("starting up");
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
|
|
||||||
// let content = std::fs::read_to_string(&args.path).expect("Could not read file.");
|
|
||||||
|
|
||||||
let content = std::fs::read_to_string(&args.path)
|
let content = std::fs::read_to_string(&args.path)
|
||||||
.with_context(|| format!("could not read file `{}`.", args.path.display()))?;
|
.with_context(|| format!("could not read file `{}`.", args.path.display()))?;
|
||||||
|
|
||||||
find_matches(
|
grrs::find_matches(
|
||||||
content.as_str(),
|
content.as_str(),
|
||||||
args.pattern.as_str(),
|
args.pattern.as_str(),
|
||||||
&mut std::io::stdout(),
|
&mut std::io::stdout(),
|
||||||
@@ -28,23 +26,6 @@ fn main() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_matches(content: &str, pattern: &str, mut writer: impl std::io::Write) -> Result<()> {
|
|
||||||
for line in content.lines() {
|
|
||||||
if line.contains(pattern) {
|
|
||||||
writeln!(writer, "{}", line)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn find_a_match() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let mut result = Vec::new();
|
|
||||||
find_matches("lorem ipsum\ndolar sit amet", "lorem", &mut result)?;
|
|
||||||
assert_eq!(result, b"lorem ipsum\n");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn answer() -> i32 {
|
fn answer() -> i32 {
|
||||||
return 42;
|
return 42;
|
||||||
|
|||||||
Reference in New Issue
Block a user