docs
This commit is contained in:
parent
0055b3553d
commit
bd5d14dbf0
|
|
@ -1,6 +1,7 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
|
/// Configuration to contain basic information of file and match operation
|
||||||
pub struct Configs {
|
pub struct Configs {
|
||||||
pattern: String,
|
pattern: String,
|
||||||
filename: String,
|
filename: String,
|
||||||
|
|
@ -8,6 +9,7 @@ pub struct Configs {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Configs {
|
impl Configs {
|
||||||
|
/// create new configuration
|
||||||
pub fn new(args: &mut std::env::Args) -> Result<Configs, &'static str> {
|
pub fn new(args: &mut std::env::Args) -> Result<Configs, &'static str> {
|
||||||
if args.len() < 3 {
|
if args.len() < 3 {
|
||||||
return Err("\
|
return Err("\
|
||||||
|
|
@ -40,6 +42,7 @@ adding \"case\" is optional to enable case sensitive grep");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// processing point of program
|
||||||
pub fn run(conf: Configs) -> Result<(), Box<dyn Error>> {
|
pub fn run(conf: Configs) -> Result<(), Box<dyn Error>> {
|
||||||
let contents: String = fs::read_to_string(conf.filename)?;
|
let contents: String = fs::read_to_string(conf.filename)?;
|
||||||
|
|
||||||
|
|
@ -52,7 +55,7 @@ pub fn run(conf: Configs) -> Result<(), Box<dyn Error>> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_case_sensitive<'a>(pattern: &str, contents: &'a str) -> Vec<&'a str> {
|
fn search_case_sensitive<'a>(pattern: &str, contents: &'a str) -> Vec<&'a str> {
|
||||||
contents.lines()
|
contents.lines()
|
||||||
.filter(
|
.filter(
|
||||||
|lin| lin
|
|lin| lin
|
||||||
|
|
@ -60,7 +63,7 @@ pub fn search_case_sensitive<'a>(pattern: &str, contents: &'a str) -> Vec<&'a st
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_case_insensitive<'a>(pattern: &str, contents: &'a str) -> Vec<&'a str> {
|
fn search_case_insensitive<'a>(pattern: &str, contents: &'a str) -> Vec<&'a str> {
|
||||||
contents.lines()
|
contents.lines()
|
||||||
.filter(
|
.filter(
|
||||||
|lin| lin.to_lowercase()
|
|lin| lin.to_lowercase()
|
||||||
|
|
@ -68,7 +71,7 @@ pub fn search_case_insensitive<'a>(pattern: &str, contents: &'a str) -> Vec<&'a
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_output(out: Vec<&str>) {
|
fn print_output(out: Vec<&str>) {
|
||||||
println!("matched: {}", out.len());
|
println!("matched: {}", out.len());
|
||||||
for x in out {
|
for x in out {
|
||||||
println!("{}", x);
|
println!("{}", x);
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
//! minigrep
|
||||||
|
//! Minimal grep program to sort out lines contaning text
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue