keyword_in_file/lib.rs
1//! # WEBSITE READING
2//!
3//! This code peice is used to put agruements into your terminal when running cargo, to 1: Search for the file name (txt) , 2: Search for a specific word in the file.txt
4//!
5//! HOW IT'S USED: Cargo run file_name Word_to_seach_for
6//! To turn off search caps sensitive you can run: $env:CAPS_SENS = "true/false"
7//!
8/// # Examples
9///
10/// ```rust
11///
12/// //WITHOUT ANY ARGUEMENTS
13/// pub fn reading_file() {
14/// use std::fs::File;
15/// let mut message = String::new();
16/// let file = File::open("Name.txt");
17///
18/// match file {
19/// Ok(mut file) => {
20/// use std::io::Read;
21/// let file_text = file.read_to_string(&mut message);
22///
23/// match file_text {
24/// Ok(_) => println!("FILE TEXT: {}", message),
25/// Err(error) => println!("HAD THE ERROR WHILE READING {}", error),
26/// }
27/// },
28/// Err(error) => println!("THE ERROR FOUND IS: {:?}", error),
29/// }
30/// }
31/// pub fn main() {
32/// reading_file();
33/// }
34/// ```
35
36pub fn reading_file() {
37
38 //getting the arguements for the file and the search
39
40 use std::env;
41
42 let caps_toggle= env::var("CAPS_SENS").unwrap_or_default() == "true";
43
44 let args: Vec<String> = env::args().collect();
45
46 if args.len() < 2 {
47
48 use std::process;
49
50 println!("DIDNT USE AGRUEMENTS, PLEASE RUN CARGO WITH ARGUEMENTS e.g cargo run file.txt search_word");
51
52 process::exit(1);
53
54 }
55
56 let file = &args[1];
57 let search = &args[2];
58
59
60
61 //code to read and search for the file
62
63 use std::fs::File;
64 use std::io::Read;
65
66
67 let file = File::open(file);
68
69 match file {
70
71 Ok(mut file) => {
72
73 let mut message = String::new();
74
75 let reading = file.read_to_string(&mut message);
76
77 match reading {
78 Ok(_) => {
79
80 if caps_toggle {
81
82 println!("CAPS_SENS");
83
84 for line in message.lines() {
85
86 if line.to_string().to_lowercase().contains(&search.to_lowercase()) {
87 println!("{}", line);
88 } else {
89 println!("couldnt find {}", search);
90 }
91 }
92
93 } else {
94
95 println!("NONE CAPS_SENS");
96
97 for line in message.lines() {
98
99 if line.to_string().contains(search) {
100 println!("{}", line);
101 }
102 }
103
104 }
105
106
107 },
108
109 Err(error) => println!("ERROR WHILE READING {:?}", error)
110 }
111
112 },
113
114 Err(error) => println!("{:?}", error)
115 }
116}
117
118pub fn main() {
119
120 reading_file();
121
122}