Skip to content

Commit f5e0a1b

Browse files
committed
Small optimisations
1 parent 471f8a6 commit f5e0a1b

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "searcher_txt"
3-
version = "1.2.2"
3+
version = "1.2.3"
44
authors = ["Raphdf201 <deschenesr545@gmail.com>"]
55
edition = "2021"
66
license-file = "unlicense.txt"

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,28 @@
22

33
i made this to train my rust skills
44

5+
## Usage
6+
7+
This is a cli program, wich means you need to execute it using .\searcher_txt.exe on windows and ./searcher_txt on linux<br/>
8+
The arguments needed are the query, filename and case sensitivity
9+
Example for searching "potato" in file.txt with no case sensitivity on windows :
10+
11+
```
12+
.\searcher_txt potato file.txt false
13+
```
14+
15+
If you want your search to be case sensitive :
16+
17+
```
18+
.\searcher_txt potato file.txt true
19+
```
20+
21+
If you're on linux or MacOS, replace the \ by /
22+
523
## Changelog
624

25+
1.2.3 : Small optimizations
26+
727
1.2.2 : Case sensitivity is now an argument
828

929
1.2.1 : Better error messages

src/lib.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fs;
44
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
55
let contents = fs::read_to_string(config.filename)?;
66

7-
let results = if config.case_sensitivity == "true" {
7+
let results = if config.case_sensitivity {
88
search(&config.query, &contents)
99
} else {
1010
search_case_insensitive(&config.query, &contents)
@@ -20,17 +20,20 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
2020
pub struct Config {
2121
pub query: String,
2222
pub filename: String,
23-
pub case_sensitivity: String,
23+
pub case_sensitivity: bool,
2424
}
2525

2626
impl Config {
2727
pub fn new(args: &[String]) -> Result<Config, &str> {
2828
if args.len() < 3 {
29-
return Err("Missing arguments");
29+
return Err("Missing 2 arguments");
30+
} else if args.len() < 4 {
31+
return Err("Missing 1 argument");
3032
}
33+
3134
let query = args[1].clone();
3235
let filename = args[2].clone();
33-
let case_sensitivity = args[3].clone();
36+
let case_sensitivity = string_to_bool(args[3].clone());
3437

3538
Ok(Config {
3639
query,
@@ -65,6 +68,20 @@ pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a st
6568
results
6669
}
6770

71+
pub fn string_to_bool(string: String) -> bool {
72+
let mut is_bool = false;
73+
if string == "true" {
74+
is_bool = true;
75+
} else if string == "false" {
76+
is_bool = false;
77+
} else {
78+
println!("An error ocurred");
79+
eprintln!("Internal error : bad usage of searcher_txt::string_to_bool");
80+
}
81+
82+
is_bool
83+
}
84+
6885
#[cfg(test)]
6986
mod tests {
7087
use super::{search, search_case_insensitive};

0 commit comments

Comments
 (0)