summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--demo/poem.txt9
-rw-r--r--libs/adder/Cargo.toml6
-rw-r--r--libs/adder/src/lib.rs64
-rw-r--r--libs/adder/tests/common/mod.rs2
-rw-r--r--libs/adder/tests/integration_test.rs11
-rw-r--r--src/minigrep/Cargo.toml6
-rw-r--r--src/minigrep/src/lib.rs59
-rw-r--r--src/minigrep/src/main.rs63
8 files changed, 220 insertions, 0 deletions
diff --git a/demo/poem.txt b/demo/poem.txt
new file mode 100644
index 0000000..8707527
--- /dev/null
+++ b/demo/poem.txt
@@ -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!
diff --git a/libs/adder/Cargo.toml b/libs/adder/Cargo.toml
new file mode 100644
index 0000000..9a5826a
--- /dev/null
+++ b/libs/adder/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "adder"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/libs/adder/src/lib.rs b/libs/adder/src/lib.rs
new file mode 100644
index 0000000..62f625c
--- /dev/null
+++ b/libs/adder/src/lib.rs
@@ -0,0 +1,64 @@
+#[derive(Debug)]
+struct Rectangle {
+ width: u32,
+ height: u32,
+}
+
+impl Rectangle {
+ fn can_hold(&self, other: &Rectangle) -> bool {
+ self.width > other.width && self.height > other.height
+ }
+}
+
+pub fn add(left: u64, right: u64) -> u64 {
+ left + right
+}
+
+pub fn add_two(a: u64) -> u64 {
+ a + 2
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn exploration() {
+ let result = add(2, 2);
+ assert_eq!(result, 4);
+ }
+
+ #[test]
+ fn larger_can_hold_smaller() {
+ let larger = Rectangle {
+ width: 8,
+ height: 7,
+ };
+ let smaller = Rectangle {
+ width: 5,
+ height: 1,
+ };
+
+ assert!(larger.can_hold(&smaller));
+ }
+
+ #[test]
+ fn smaller_cannot_hold_larger() {
+ let larger = Rectangle {
+ width: 8,
+ height: 7,
+ };
+ let smaller = Rectangle {
+ width: 5,
+ height: 1,
+ };
+
+ assert!(!smaller.can_hold(&larger));
+ }
+
+ #[test]
+ fn it_adds_two() {
+ let result = add_two(2);
+ assert_eq!(result, 4)
+ }
+}
diff --git a/libs/adder/tests/common/mod.rs b/libs/adder/tests/common/mod.rs
new file mode 100644
index 0000000..71a3f5a
--- /dev/null
+++ b/libs/adder/tests/common/mod.rs
@@ -0,0 +1,2 @@
+pub fn setup() {
+}
diff --git a/libs/adder/tests/integration_test.rs b/libs/adder/tests/integration_test.rs
new file mode 100644
index 0000000..d18b5b6
--- /dev/null
+++ b/libs/adder/tests/integration_test.rs
@@ -0,0 +1,11 @@
+use adder::add_two;
+
+mod common;
+
+#[test]
+fn it_adds_two() {
+ common::setup();
+
+ let result = add_two(2);
+ assert_eq!(result, 4);
+}
diff --git a/src/minigrep/Cargo.toml b/src/minigrep/Cargo.toml
new file mode 100644
index 0000000..b8302ae
--- /dev/null
+++ b/src/minigrep/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "minigrep"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/minigrep/src/lib.rs b/src/minigrep/src/lib.rs
new file mode 100644
index 0000000..064cf7a
--- /dev/null
+++ b/src/minigrep/src/lib.rs
@@ -0,0 +1,59 @@
+pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
+ let mut results = Vec::new();
+
+ for line in contents.lines() {
+ if line.contains(query) {
+ results.push(line);
+ }
+ }
+
+ results
+}
+
+pub fn search_case_insensitive<'a>(
+ query: &str,
+ contents: &'a str,
+) -> Vec<&'a str> {
+ let query = query.to_lowercase();
+ let mut results = Vec::new();
+
+ for line in contents.lines() {
+ if line.to_lowercase().contains(&query) {
+ results.push(line);
+ }
+ }
+
+ results
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn case_sensitive() {
+ let query = "duct";
+ let contents = "\
+Rust:
+safe, fast, productive.
+Pick three.
+Duct tape.";
+
+ assert_eq!(vec!["safe, fast, productive."], search(query, contents));
+ }
+
+ #[test]
+ fn case_insensitive() {
+ let query = "rUsT";
+ let contents = "\
+Rust:
+safe, fast, productive.
+Pick three.
+Trust me.";
+
+ assert_eq!(
+ vec!["Rust:", "Trust me."],
+ search_case_insensitive(query, contents)
+ );
+ }
+}
diff --git a/src/minigrep/src/main.rs b/src/minigrep/src/main.rs
new file mode 100644
index 0000000..0e36bc6
--- /dev/null
+++ b/src/minigrep/src/main.rs
@@ -0,0 +1,63 @@
+use std::{env, fs, process};
+use std::error::Error;
+
+use minigrep::{search, search_case_insensitive};
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+
+ let config = Config::build(&args).unwrap_or_else(|err| {
+ eprintln!("Problem parsing arguments: {err}");
+ process::exit(1);
+ });
+
+ println!("Searching for {}", config.query);
+ println!("In file {}", config.file_path);
+
+ if let Err(e) = run(config) {
+ eprintln!("Application error: {e}");
+ process::exit(1);
+ }
+}
+
+
+fn run(config: Config) -> Result<(), Box<dyn Error>> {
+ let contents = fs::read_to_string(config.file_path)?;
+
+ let results = if config.ignore_case {
+ search_case_insensitive(&config.query, &contents)
+ } else {
+ search(&config.query, &contents)
+ };
+
+ for line in results {
+ println!("{line}");
+ }
+
+ Ok(())
+}
+
+struct Config{
+ pub query: String,
+ pub file_path: String,
+ pub ignore_case: bool,
+}
+
+impl Config {
+ fn build(args: &[String]) -> Result<Config, &'static str> {
+ if args.len() < 3 {
+ return Err("not enough arguments");
+ }
+
+ let query = args[1].clone();
+ let file_path = args[2].clone();
+
+ let ignore_case = env::var("IGNORE_CASE").is_ok();
+
+ Ok(Config {
+ query,
+ file_path,
+ ignore_case,
+ })
+ }
+}