minigrep_cli/
lib.rs

1//! # Grep CLI Tool
2//!
3//! 一个简单的命令行工具,用于从文件中搜索指定的查询字符串。
4//!
5//! # 功能概述
6//!
7//! - 提供命令行接口以执行文本搜索。
8//! - 支持通过环境变量 `IGNORE_CASE` 切换大小写不敏感模式。
9//! - 在遇到错误时提供友好的提示信息。
10//!
11//! # 使用方法
12//!
13//! 假设程序的可执行文件名为 `grep`,可以通过以下方式运行:
14//!
15//! ```bash
16//! ./grep <query> <file_path>
17//! ```
18//!
19//! 例如:
20//! ```bash
21//! ./grep rust example.txt
22//! ```
23//! 如果设置了环境变量 `IGNORE_CASE`,查询会忽略大小写。
24//! - CMD
25//!   ``` bash
26//!   IGNORE_CASE=1 ./grep rust example.txt
27//!   ```
28//! - Powershell
29//!   ``` powershell
30//!   $env:IGNORE_CASE = 1  ./grep rust example.txt
31//!   ```
32use std::error::Error;
33use std::{env, fs};
34
35/// 配置结构体,存储查询字符串、文件路径以及是否忽略大小写的标志。
36pub struct Config {
37    /// 查询字符串
38    pub query: String, // 修改为 pub,便于外部访问
39    /// 文件路径
40    pub file_path: String, // 修改为 pub,便于外部访问
41    /// 是否忽略大小写
42    pub ignore_case: bool, // 修改为 pub,便于外部访问
43}
44impl Config {
45    /// 从命令行参数构建 `Config` 实例。
46    ///
47    /// # 参数
48    ///
49    /// - `args`: 命令行参数的迭代器。
50    ///
51    /// # 返回值
52    ///
53    /// 返回 `Config` 实例,或者在参数缺失时返回错误消息。
54    ///
55    /// # 错误
56    ///
57    /// 当缺少查询字符串或文件路径时,返回对应的错误消息。
58    ///
59    /// # 示例
60    ///
61    /// ```
62    /// let args = vec!["program".to_string(), "query".to_string(), "file.txt".to_string()];
63    /// let config = Config::build(args.into_iter());
64    /// assert!(config.is_ok());
65    /// ```
66    pub fn build(mut args: impl Iterator<Item = String>) -> Result<Config, &'static str> {
67        args.next();
68        let query = match args.next() {
69            Some(args) => args,
70            None => return Err("Please provide a query string"),
71        };
72        let file_path = match args.next() {
73            Some(args) => args,
74            None => return Err("Please provide a file path"),
75        };
76        let ignore_case = env::var("IGNORE_CASE").is_ok();
77        Ok(Config {
78            query,
79            file_path,
80            ignore_case,
81        })
82    }
83}
84
85/// 运行搜索逻辑。
86///
87/// # 参数
88///
89/// - `config`: 包含查询信息的 `Config` 实例。
90///
91/// # 返回值
92///
93/// 成功返回 `Ok(())`,失败返回包含错误信息的 `Box<dyn Error>`。
94///
95/// # 错误
96///
97/// 如果文件读取失败,返回错误信息。
98pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
99    let contents = fs::read_to_string(config.file_path)?;
100    let result = if config.ignore_case {
101        search_case_insensitive(&config.query, &contents)
102    } else {
103        search(&config.query, &contents)
104    };
105    for line in result {
106        println!("{line}")
107    }
108    Ok(())
109}
110
111/// 在内容中进行大小写敏感的查询。
112///
113/// # 参数
114///
115/// - `query`: 查询字符串。
116/// - `contents`: 文件内容。
117///
118/// # 返回值
119///
120/// 包含匹配行的 `Vec<&str>`。
121fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
122    contents.lines()
123        .filter(|line| line.contains(query))
124        .collect()
125}
126
127/// 在内容中进行大小写不敏感的查询。
128///
129/// # 参数
130///
131/// - `query`: 查询字符串。
132/// - `contents`: 文件内容。
133///
134/// # 返回值
135///
136/// 包含匹配行的 `Vec<&str>`。
137fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
138    let query = query.to_lowercase();
139    contents.lines()
140        .filter(|line| line.to_lowercase().contains(&query))
141        .collect()
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147
148    /// 测试大小写敏感的查询功能。
149    #[test]
150    fn case_sensitive() {
151        let query = "duct";
152        let contents = "\
153Rust:
154safe, fast, productive.
155Pick three.
156Duct tape.";
157
158        assert_eq!(vec!["safe, fast, productive."], search(query, contents));
159    }
160
161    /// 测试大小写不敏感的查询功能。
162    #[test]
163    fn case_insensitive() {
164        let query = "rUsT";
165        let contents = "\
166Rust:
167safe, fast, productive.
168Pick three.
169Trust me.";
170
171        assert_eq!(
172            vec!["Rust:", "Trust me."],
173            search_case_insensitive(query, contents)
174        );
175    }
176}