Compare commits

..

No commits in common. "7ea306ec32beb7d5a5e688dbef455220c32e666c" and "044925fb1589de8ab125bfcd704733051f850568" have entirely different histories.

12 changed files with 43 additions and 171 deletions

View File

@ -1,9 +0,0 @@
[package]
name = "iterators"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,51 +0,0 @@
#[derive(PartialEq, Debug)]
struct Shoe {
size: u32,
style: String,
}
fn shoes_in_my_size(shoes: Vec<Shoe>, shoe_size: u32) -> Vec<Shoe> {
shoes.into_iter().filter(|s| s.size == shoe_size).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn filters_by_size() {
let shoes = vec![
Shoe {
size: 10,
style: String::from("sneaker"),
},
Shoe {
size: 13,
style: String::from("sandal"),
},
Shoe {
size: 10,
style: String::from("boot"),
},
];
let in_my_size = shoes_in_my_size(shoes, 10);
assert_eq!(
in_my_size,
vec![
Shoe {
size: 10,
style: String::from("sneaker")
},
Shoe {
size: 10,
style: String::from("boot")
},
]
);
}
}
fn main() {
}

View File

@ -9,18 +9,12 @@ pub struct Config {
} }
impl Config { impl Config {
pub fn new(mut args: env::Args) -> Result<Config, &'static str> { pub fn new(args: &[String]) -> Result<Config, &'static str> {
args.next(); if args.len() < 3 {
return Err("not enough arguments");
let query = match args.next() { }
Some(arg) => arg, let query = args[1].clone();
None => return Err("Didn't get a query string"), let filename = args[2].clone();
};
let filename = match args.next() {
Some(arg) => arg,
None => return Err("Didn't get a file name"),
};
let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); let case_sensitive = env::var("CASE_INSENSITIVE").is_err();
@ -29,19 +23,28 @@ impl Config {
} }
fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
contents let mut results = Vec::new();
.lines()
.filter(|line| line.contains(query)) for line in contents.lines() {
.collect() if line.contains(query) {
results.push(line);
}
}
results
} }
fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();
let query = query.to_lowercase(); let query = query.to_lowercase();
contents for line in contents.lines() {
.lines() if line.to_lowercase().contains(&query) {
.filter(|line| line.to_lowercase().contains(&query)) results.push(line);
.collect() }
}
results
} }
pub fn run(config: Config) -> Result<(), Box<dyn Error>> { pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
@ -96,7 +99,6 @@ Trust me.";
assert!(run(Config { assert!(run(Config {
query: String::from("test"), query: String::from("test"),
filename: String::from("poem.txt"), filename: String::from("poem.txt"),
case_sensitive: true,
}).is_ok()); }).is_ok());
} }
@ -105,7 +107,23 @@ Trust me.";
assert!(run(Config { assert!(run(Config {
query: String::from("test"), query: String::from("test"),
filename: String::from("134857320592.txt"), filename: String::from("134857320592.txt"),
case_sensitive: true,
}).is_err()); }).is_err());
} }
#[test]
fn config_new_valid() {
assert!(Config::new(
&vec![
String::from("binaryname"),
String::from("query"),
String::from("filename")]).is_ok());
}
#[test]
fn config_new_invalid() {
assert!(Config::new(
&vec![
String::from("only two"),
String::from("arguments")]).is_err());
}
} }

View File

@ -4,7 +4,9 @@ use std::process;
use minigrep::Config; use minigrep::Config;
fn main() { fn main() {
let config = Config::new(env::args()).unwrap_or_else(|err| { let args: Vec<String> = env::args().collect();
let config = Config::new(&args).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {}", err); eprintln!("Problem parsing arguments: {}", err);
process::exit(1); process::exit(1);
}); });

View File

@ -1,7 +0,0 @@
[workspace]
members = [
"adder",
"add-one",
"add-two",
]

View File

@ -1,10 +0,0 @@
[package]
name = "add-one"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.5.5"

View File

@ -1,13 +0,0 @@
pub fn add_one(n: i32) -> i32 {
n + 1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!(3, add_one(2));
}
}

View File

@ -1,9 +0,0 @@
[package]
name = "add-two"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,13 +0,0 @@
pub fn add_two(n: i32) -> i32 {
n + 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!(4, add_two(2));
}
}

View File

@ -1,12 +0,0 @@
[package]
name = "adder"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
add-one = { path = "../add-one" }
add-two = { path = "../add-two" }
rand = "0.5.5"

View File

@ -1,21 +0,0 @@
use rand::Rng;
use add_one;
use add_two;
fn main() {
let num = 10;
println!(
"Hello, world! {} plus one is {}!",
num,
add_one::add_one(num),
);
println!(
"Hello, world! {} plus two is {}!",
num,
add_two::add_two(num),
);
println!(
"And here is a random number between 1 and 100: {}",
rand::thread_rng().gen_range(1, 101),
)
}

View File

@ -1,3 +0,0 @@
fn main() {
println!("Hello, world!");
}