libgrep_rs/
lib.rs

1/*!
2    grep-rs is a simple tool for searching through text  
3    * [Github](https://github.com/Pyrerune/grep-rs)
4    ## Currently Working Features
5    * Support for searching through files
6    * Support for searching through standard input
7    * Searching through text that includes specific patterns
8    * Searching through text that excludes specific patterns
9    * Printing all lines before the first instance of the pattern  
10    * Printing all lines after the first instance of the pattern 
11    * Case Insensitivity 
12    * Simple Regular Expressions 
13    ## Installation
14    Add this to your Cargo.toml
15    ```toml
16    libgrep-rs = "0.1.3"
17    ```
18    ## Example
19    ```no_run
20    use libgrep_rs::searcher::Searcher;
21    use libgrep_rs::options::Options;
22
23    fn main() {
24        let options = Options::default();
25        let text = String::from("Hello World\n libgrep-rs test");
26        let pattern = String::from("World");
27        let searcher = Searcher::new(pattern, text, options, Some(String::from("\n")));
28        //Some(String::from("\n")) is the deliminator, it could be anything
29        //If set to None, it will use the default "\n"
30        let output = searcher.search();
31        println!("{}", output);
32    }
33    ```
34    If it worked, the output will be
35    ```txt
36    Hello World
37    ```
38    You can see other examples in the examples/ directory
39*/
40
41pub mod searcher;
42pub mod options;
43#[cfg(test)]
44mod tests {
45    
46    use crate::searcher::Searcher;
47    use crate::options::Options;
48    #[test]
49    fn exclude() {
50        let options = Options {
51            exclude: true,
52            ..Options::default()
53        };
54        let text = String::from("Steve Jobs Passed Away\nGates thrilled");
55        let searcher = Searcher::new(String::from("Jobs"), text, options, None);
56        let assert_text: String = String::from("Gates thrilled");
57        let mut return_text: String = searcher.search();
58        if return_text.contains("\n") {
59            return_text.remove(return_text.find("\n").unwrap());
60        }
61        assert_eq!(assert_text, return_text);
62    }
63    #[test]
64    fn include() {
65        let options = Options::default();
66        let text = String::from("Steve Jobs Passed Away\nGates thrilled");
67        let searcher = Searcher::new(String::from("Jobs"), text, options, None);
68        let assert_text: String = String::from("Steve Jobs Passed Away");
69        let mut return_text: String = searcher.search();
70        if return_text.contains("\n") {
71            return_text.remove(return_text.find("\n").unwrap());
72        }
73        assert_eq!(assert_text, return_text);
74    
75    }
76    #[test]
77    fn include_before() {
78        let options = Options {
79            include_before: true,
80            ..Options::default()
81        };
82        let text = String::from("Steve Jobs Passed Away\nGates thrilled\nApple Fans Devastated\nGates Thrilled and Devastated");
83        let searcher = Searcher::new(String::from("Gates"), text, options, None);
84        let assert_text: String = String::from("Steve Jobs Passed Away Gates thrilled Gates Thrilled and Devastated");
85        let mut return_text: String = searcher.search();
86        if return_text.contains("\n") {
87            return_text = return_text.replace("\n", " ");
88            if return_text.ends_with(" ") {
89                return_text.remove(return_text.len() - 1);
90            }
91            if return_text.starts_with(" ") {
92                return_text.remove(0);
93            }
94            println!("test: {}", return_text);
95        }
96        assert_eq!(assert_text, return_text);
97    }
98    #[test]
99    fn include_after() {
100        let options = Options {
101            include_after: true,
102            ..Options::default()
103        };
104        let text = String::from("Steve Jobs Passed Away\nGates thrilled\nApple Fans Devastated");
105        let searcher = Searcher::new(String::from("Gates"), text, options, None);
106        let assert_text: String = String::from("Gates thrilled Apple Fans Devastated");
107        let mut return_text: String = searcher.search();
108        if return_text.contains("\n") {
109            return_text = return_text.replace("\n", " ");
110            if return_text.ends_with(" ") {
111                return_text.remove(return_text.len() - 1);
112            }
113            if return_text.starts_with(" ") {
114                return_text.remove(0);
115            }
116            println!("test: {}", return_text);
117        }
118        assert_eq!(assert_text, return_text);
119    }
120    #[test]
121    fn case_insensitive() {
122        let options = Options {
123            case_insensitive: true,
124            ..Options::default()
125        };
126        let text = String::from("Steve Jobs Passed Away\nGates thrilled\n Steve jobs hasn't passed away");
127        let searcher = Searcher::new(String::from("Jobs"), text, options, None);
128        let assert_text: String = String::from("Steve Jobs Passed Away Steve jobs hasn't passed away").to_lowercase();
129        let mut return_text: String = searcher.search();
130        if return_text.contains("\n") {
131            return_text.remove(return_text.find("\n").unwrap());
132        }
133        if return_text.ends_with("\n") {
134            return_text.remove(return_text.len()-1);
135        }
136
137        assert_eq!(assert_text, return_text);
138    }
139    #[test]
140    fn regex() {
141        let options = Options {
142            regex: true,
143            ..Options::default()
144        };
145        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");
146        let searcher = Searcher::new(String::from(r"\d"), text, options, None);
147        let assert_text = String::from("Steve Jobs Passed Away on 2020-05-18");
148        let mut return_text: String = searcher.search();
149        if return_text.contains("\n") {
150            return_text.remove(return_text.find("\n").unwrap());
151        }
152        if return_text.ends_with("\n") {
153            return_text.remove(return_text.len()-1);
154        }
155
156        assert_eq!(assert_text, return_text);
157    }
158    #[test]
159    fn custom_delim() {
160        let options = Options::default();
161        let text = String::from("Steve Jobs Passed Away\nGates thrilled");
162        let searcher = Searcher::new(String::from("Jobs"), text, options, Some(String::from(" ")));
163        let assert_text: String = String::from("Jobs");
164        let mut return_text: String = searcher.search();
165        if return_text.contains("\n") {
166            return_text.remove(return_text.find("\n").unwrap());
167        }
168        assert_eq!(assert_text, return_text);
169    }
170    #[test]
171    fn show_line() {
172        let options = Options {
173            show_line: true,
174            ..Options::default()
175        };
176        let text = String::from("Steve Jobs Passed Away\nGates thrilled");
177        let searcher = Searcher::new(String::from("Jobs"), text, options, None);
178        let assert_text: String = String::from("found at line: 0\nSteve Jobs Passed Away\n");
179        let mut return_text: String = searcher.search();
180
181        assert_eq!(assert_text, return_text);
182    }
183}