1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::error::Error;


pub fn run(config:Config){
    println!("lib run.");
    
    // let contents = fs::read_to_string(config.filename)?;
    
    // println!("With text:\n{}", contents);
}

pub struct Config<'a>{
    query:&'a str,
    filename:&'a str,
}

// struct Config(String,String);

// struct Config;

fn parse_config<'a>(args: &'a [String])->Config<'a>{
    let query = &args[1];//.expect("需要搜索字符串参数。")
    let filename = &args[2];//.expect("需要文件名参数。")

    println!("Searching for {}", query);
    println!("In File {}", filename);
    
    Config{query, filename}
}

impl<'a> Config<'a>{
    pub fn new (args:&[String])->Result<Config, &'static str>{
        if args.len() < 3{
            return Err("需要搜索字符串和文件名参数。")
        }
        Ok(parse_config(args))
    }
}

fn search<'a>(query:&str,contents:&'a str)
    ->Vec<&'a str>{
    vec![]
}

#[cfg(test)]
mod tests{
    use super::*;

    #[test]
    fn one(){
        let query="duct";
        let contents = "\
        rust:
        safe, fase, productive.
        pick three.";

        assert!(vec!["safe,fase,productive."] 
            == search(query,contents));

    }
}