minigrep_xyp/structs/
config.rs1use std::env;
2
3#[derive(Debug, PartialOrd, PartialEq)]
4pub struct Config {
5 pub query: String,
6 pub filename: String,
7 pub case_sensitive: bool,
8}
9
10impl Config {
11 pub fn new(query: String, filename: String, case_sensitive: bool) -> Config {
12 Config {
13 query,
14 filename,
15 case_sensitive,
16 }
17 }
18
19 pub fn parse_config(mut args: std::env::Args) -> Result<Config, &'static str> {
20 args.next();
21
22 let mut case_sensitive = env::var("MINIGREP_CASE_INSENSITIVE").is_err();
23
24 let query = match args.next() {
25 Some(arg) => arg,
26 None => return Err("not enough args!"),
27 };
28 let filename = match args.next() {
29 Some(arg) => arg,
30 None => return Err("not enough args!"),
31 };
32
33 let i = String::from("-i");
34 let c = String::from("-c");
35
36 let f = args.next().unwrap_or(String::from(""));
37
38 if f == i { case_sensitive = false}
39 else if f == c{ case_sensitive = true};
40
41 Ok(Config::new(query, filename, case_sensitive))
42 }
43}
44
45#[derive(Debug)]
46pub struct Counter {
47 pub count: u32,
48}
49
50impl Counter {
51 pub fn new(count: u32) -> Counter {
52 Counter{count}
53 }
54}
55
56impl Iterator for Counter {
57 type Item = u32;
58
59 fn next(&mut self) -> Option<Self::Item> {
60 if self.count > 0 {
61 self.count -= 1;
62 Some(self.count)
63 } else {
64 None
65 }
66 }
67}
68