]> git.proxmox.com Git - rustc.git/blame - src/doc/book/listings/ch12-an-io-project/listing-12-17/src/lib.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / doc / book / listings / ch12-an-io-project / listing-12-17 / src / lib.rs
CommitLineData
74b04a01
XL
1use std::error::Error;
2use std::fs;
3
4pub struct Config {
5 pub query: String,
923072b8 6 pub file_path: String,
74b04a01
XL
7}
8
9impl 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
22pub 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
29pub 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)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn one_result() {
42 let query = "duct";
43 let contents = "\
44Rust:
45safe, fast, productive.
46Pick three.";
47
48 assert_eq!(vec!["safe, fast, productive."], search(query, contents));
49 }
50}