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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*!
    grep-rs is a simple tool for searching through text  
    * [Github](https://github.com/Pyrerune/grep-rs)
    ## Currently Working Features
    * Support for searching through files
    * Support for searching through standard input
    * Searching through text that includes specific patterns
    * Searching through text that excludes specific patterns
    * Printing all lines before the first instance of the pattern  
    * Printing all lines after the first instance of the pattern 
    * Case Insensitivity 
    * Simple Regular Expressions 
    ## Installation
    Add this to your Cargo.toml
    ```toml
    libgrep-rs = "0.1.3"
    ```
    ## Example
    ```no_run
    use libgrep_rs::searcher::Searcher;
    use libgrep_rs::options::Options;

    fn main() {
        let options = Options::default();
        let text = String::from("Hello World\n libgrep-rs test");
        let pattern = String::from("World");
        let searcher = Searcher::new(pattern, text, options, Some(String::from("\n")));
        //Some(String::from("\n")) is the deliminator, it could be anything
        //If set to None, it will use the default "\n"
        let output = searcher.search();
        println!("{}", output);
    }
    ```
    If it worked, the output will be
    ```txt
    Hello World
    ```
    You can see other examples in the examples/ directory
*/

pub mod searcher;
pub mod options;
#[cfg(test)]
mod tests {
    
    use crate::searcher::Searcher;
    use crate::options::Options;
    #[test]
    fn exclude() {
        let options = Options {
            exclude: true,
            ..Options::default()
        };
        let text = String::from("Steve Jobs Passed Away\nGates thrilled");
        let searcher = Searcher::new(String::from("Jobs"), text, options, None);
        let assert_text: String = String::from("Gates thrilled");
        let mut return_text: String = searcher.search();
        if return_text.contains("\n") {
            return_text.remove(return_text.find("\n").unwrap());
        }
        assert_eq!(assert_text, return_text);
    }
    #[test]
    fn include() {
        let options = Options::default();
        let text = String::from("Steve Jobs Passed Away\nGates thrilled");
        let searcher = Searcher::new(String::from("Jobs"), text, options, None);
        let assert_text: String = String::from("Steve Jobs Passed Away");
        let mut return_text: String = searcher.search();
        if return_text.contains("\n") {
            return_text.remove(return_text.find("\n").unwrap());
        }
        assert_eq!(assert_text, return_text);
    
    }
    #[test]
    fn include_before() {
        let options = Options {
            include_before: true,
            ..Options::default()
        };
        let text = String::from("Steve Jobs Passed Away\nGates thrilled\nApple Fans Devastated\nGates Thrilled and Devastated");
        let searcher = Searcher::new(String::from("Gates"), text, options, None);
        let assert_text: String = String::from("Steve Jobs Passed Away Gates thrilled Gates Thrilled and Devastated");
        let mut return_text: String = searcher.search();
        if return_text.contains("\n") {
            return_text = return_text.replace("\n", " ");
            if return_text.ends_with(" ") {
                return_text.remove(return_text.len() - 1);
            }
            if return_text.starts_with(" ") {
                return_text.remove(0);
            }
            println!("test: {}", return_text);
        }
        assert_eq!(assert_text, return_text);
    }
    #[test]
    fn include_after() {
        let options = Options {
            include_after: true,
            ..Options::default()
        };
        let text = String::from("Steve Jobs Passed Away\nGates thrilled\nApple Fans Devastated");
        let searcher = Searcher::new(String::from("Gates"), text, options, None);
        let assert_text: String = String::from("Gates thrilled Apple Fans Devastated");
        let mut return_text: String = searcher.search();
        if return_text.contains("\n") {
            return_text = return_text.replace("\n", " ");
            if return_text.ends_with(" ") {
                return_text.remove(return_text.len() - 1);
            }
            if return_text.starts_with(" ") {
                return_text.remove(0);
            }
            println!("test: {}", return_text);
        }
        assert_eq!(assert_text, return_text);
    }
    #[test]
    fn case_insensitive() {
        let options = Options {
            case_insensitive: true,
            ..Options::default()
        };
        let text = String::from("Steve Jobs Passed Away\nGates thrilled\n Steve jobs hasn't passed away");
        let searcher = Searcher::new(String::from("Jobs"), text, options, None);
        let assert_text: String = String::from("Steve Jobs Passed Away Steve jobs hasn't passed away").to_lowercase();
        let mut return_text: String = searcher.search();
        if return_text.contains("\n") {
            return_text.remove(return_text.find("\n").unwrap());
        }
        if return_text.ends_with("\n") {
            return_text.remove(return_text.len()-1);
        }

        assert_eq!(assert_text, return_text);
    }
    #[test]
    fn regex() {
        let options = Options {
            regex: true,
            ..Options::default()
        };
        let text = String::from("Steve Jobs Passed Away on 2020-05-18\nHe passed away at exactly midnight as he didn't like being late");
        let searcher = Searcher::new(String::from(r"\d"), text, options, None);
        let assert_text = String::from("Steve Jobs Passed Away on 2020-05-18");
        let mut return_text: String = searcher.search();
        if return_text.contains("\n") {
            return_text.remove(return_text.find("\n").unwrap());
        }
        if return_text.ends_with("\n") {
            return_text.remove(return_text.len()-1);
        }

        assert_eq!(assert_text, return_text);
    }
    #[test]
    fn custom_delim() {
        let options = Options::default();
        let text = String::from("Steve Jobs Passed Away\nGates thrilled");
        let searcher = Searcher::new(String::from("Jobs"), text, options, Some(String::from(" ")));
        let assert_text: String = String::from("Jobs");
        let mut return_text: String = searcher.search();
        if return_text.contains("\n") {
            return_text.remove(return_text.find("\n").unwrap());
        }
        assert_eq!(assert_text, return_text);
    }
    #[test]
    fn show_line() {
        let options = Options {
            show_line: true,
            ..Options::default()
        };
        let text = String::from("Steve Jobs Passed Away\nGates thrilled");
        let searcher = Searcher::new(String::from("Jobs"), text, options, None);
        let assert_text: String = String::from("found at line: 0\nSteve Jobs Passed Away\n");
        let mut return_text: String = searcher.search();

        assert_eq!(assert_text, return_text);
    }
}