]>
Commit | Line | Data |
---|---|---|
74b04a01 XL |
1 | use std::error::Error; |
2 | use std::fs; | |
3 | ||
4 | pub struct Config { | |
5 | pub query: String, | |
923072b8 | 6 | pub file_path: String, |
74b04a01 XL |
7 | } |
8 | ||
9 | impl Config { | |
923072b8 | 10 | pub fn build(args: &[String]) -> Result<Config, &'static str> { |
74b04a01 XL |
11 | if args.len() < 3 { |
12 | return Err("not enough arguments"); | |
13 | } | |
14 | ||
15 | let query = args[1].clone(); | |
923072b8 | 16 | let file_path = args[2].clone(); |
74b04a01 | 17 | |
923072b8 | 18 | Ok(Config { query, file_path }) |
74b04a01 XL |
19 | } |
20 | } | |
21 | ||
22 | pub fn run(config: Config) -> Result<(), Box<dyn Error>> { | |
923072b8 | 23 | let contents = fs::read_to_string(config.file_path)?; |
74b04a01 XL |
24 | |
25 | Ok(()) | |
26 | } | |
27 | ||
28 | // ANCHOR: here | |
29 | pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { | |
30 | for line in contents.lines() { | |
31 | // do something with line | |
32 | } | |
33 | } | |
34 | // ANCHOR_END: here | |
35 | ||
36 | #[cfg(test)] | |
37 | mod tests { | |
38 | use super::*; | |
39 | ||
40 | #[test] | |
41 | fn one_result() { | |
42 | let query = "duct"; | |
43 | let contents = "\ | |
44 | Rust: | |
45 | safe, fast, productive. | |
46 | Pick three."; | |
47 | ||
48 | assert_eq!(vec!["safe, fast, productive."], search(query, contents)); | |
49 | } | |
50 | } |