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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! # SimpleGrep 🕵️♂️📄
//!
//! `simple_grep` is a simple command-line tool written in Rust to search for lines
//! that contain a specific query string in a given text file, similar to Unix `grep`.
//!
//! It supports both case-sensitive and case-insensitive search based on the
//! `IGNORE_CASE` environment variable.
//!
//! ## Example
//!
//! ```bash
//! cargo run -- <query> <file_path>
//! ```
//!
//! Case-insensitive search:
//!
//! ```bash
//! IGNORE_CASE=1 cargo run -- <query> <file_path>
//! ```
//!
//! ## Crate Structure
//!
//! - [`Config`] struct handles argument parsing and configuration.
//! - [`run`] function executes the main logic.
//! - [`search`] and [`search_case_insensitive`] perform line matching.
use env;
use Error;
use fs;
/// Holds the configuration parameters for the SimpleGrep application.
///
/// This struct is created using the [`Config::build`] method,
/// which parses command-line arguments and reads the `IGNORE_CASE`
/// environment variable to determine if the search should be case-insensitive.
/// Executes the main logic of SimpleGrep: reads the file, searches for the query,
/// and prints matching lines to stdout.
///
/// # Arguments
///
/// * `config` - A `Config` object containing the search parameters.
///
/// # Errors
///
/// Returns a boxed `dyn Error` if reading the file fails.
///
/// # Examples
///
/// ```no_run
/// use simple_grep::{Config, run};
///
/// let config = Config {
/// query: String::from("Rust"),
/// file_path: String::from("input.txt"),
/// ignore_case: false,
/// };
///
/// if let Err(e) = run(config) {
/// eprintln!("Application error: {e}");
/// }
/// ```
/// Performs a **case-sensitive** search of `query` in `contents`.
///
/// # Arguments
///
/// * `query` - The search string.
/// * `contents` - The contents of the file as a string slice.
///
/// # Returns
///
/// A vector of lines that contain the query.
///
/// # Examples
///
/// ```
/// use simple_grep::search;
///
/// let query = "duct";
/// let contents = "\
/// Rust:
/// safe, fast, productive.
/// Pick three.
/// Duct tape.";
///
/// assert_eq!(vec!["safe, fast, productive."], search(query, contents));
/// ```
/// Performs a **case-insensitive** search of `query` in `contents`.
///
/// # Arguments
///
/// * `query` - The search string (case-insensitive).
/// * `contents` - The contents of the file as a string slice.
///
/// # Returns
///
/// A vector of lines that contain the query, ignoring case.
///
/// # Examples
///
/// ```
/// use simple_grep::search_case_insensitive;
///
/// let query = "rUsT";
/// let contents = "\
/// Rust:
/// safe, fast, productive.
/// Pick three.
/// Trust me.";
///
/// assert_eq!(vec!["Rust:", "Trust me."], search_case_insensitive(query, contents));
/// ```