mirror of
https://github.com/ArthurDanjou/minigrep.git
synced 2026-01-14 12:14:35 +01:00
Initial Commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
11
.idea/minigrep.iml
generated
Normal file
11
.idea/minigrep.iml
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="CPP_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/minigrep.iml" filepath="$PROJECT_DIR$/.idea/minigrep.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "minigrep"
|
||||
version = "0.1.0"
|
||||
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "minigrep"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors = ["Arthur DANJOU"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
9
poem.txt
Normal file
9
poem.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
I'm nobody! Who are you?
|
||||
Are you nobody, too?
|
||||
Then there's a pair of us - don't tell!
|
||||
They'd banish us, you know.
|
||||
|
||||
How dreary to be somebody!
|
||||
How public, like a frog
|
||||
To tell your name the livelong day
|
||||
To an admiring bog!
|
||||
108
src/lib.rs
Normal file
108
src/lib.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
use std::error::Error;
|
||||
use std::{env, fs};
|
||||
|
||||
pub struct Config {
|
||||
pub research: String,
|
||||
pub filename: String,
|
||||
pub verbose: bool
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new(args: &[String]) -> Result<Config, &'static str> {
|
||||
if args.len() < 3 {
|
||||
return Err("Not enough arguments !");
|
||||
}
|
||||
|
||||
let research = args[1].clone();
|
||||
let filename = args[2].clone();
|
||||
|
||||
let verbose = env::var("VERBOSE").is_err();
|
||||
|
||||
Ok(Config {
|
||||
research,
|
||||
filename,
|
||||
verbose
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn search<'a>(
|
||||
research: &str,
|
||||
content: &'a str,
|
||||
) -> Vec<&'a str> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
for line in content.lines() {
|
||||
if line.contains(&research) {
|
||||
result.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn search_insensitive_case<'a>(
|
||||
research: &str,
|
||||
content: &'a str,
|
||||
) -> Vec<&'a str> {
|
||||
let research = research.to_lowercase();
|
||||
let mut result = Vec::new();
|
||||
|
||||
for line in content.lines() {
|
||||
if line.to_lowercase().contains(&research) {
|
||||
result.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let content = fs::read_to_string(config.filename)?;
|
||||
|
||||
println!("Content : \n{}", content);
|
||||
|
||||
let result = if config.verbose {
|
||||
search(&config.research, &content)
|
||||
} else {
|
||||
search_insensitive_case(&config.research, &content)
|
||||
};
|
||||
|
||||
for line in result {
|
||||
println!("{}", line);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sensible_casse() {
|
||||
let research = "duct";
|
||||
let content = "\
|
||||
Rust:
|
||||
sécurité, rapidité, productivité.
|
||||
Obtenez les trois en même temps.
|
||||
Duck tape.";
|
||||
|
||||
assert_eq!(vec!["sécurité, rapidité, productivité."], search(research, content));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insensible_casse() {
|
||||
let research = "rUsT";
|
||||
let content = "\
|
||||
Rust:
|
||||
sécurité, rapidité, productivité.
|
||||
Obtenez les trois en même temps.
|
||||
C'est pas rustique.";
|
||||
|
||||
assert_eq!(
|
||||
vec!["Rust:", "C'est pas rustique."],
|
||||
search_insensitive_case(research, content)
|
||||
);
|
||||
}
|
||||
}
|
||||
19
src/main.rs
Normal file
19
src/main.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use std::env::args;
|
||||
use std::process;
|
||||
|
||||
use minigrep::Config;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = args().collect();
|
||||
|
||||
let config = Config::new(&args).unwrap_or_else(|err| {
|
||||
eprintln!("Problème rencontré lors de l'interprétation des arguments : {}", err);
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
if let Err(e) = minigrep::run(config) {
|
||||
eprintln!("Erreur applicative : {}", e);
|
||||
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user