From 8ebbcb393c79d20290ddac10353a35d576875fdb Mon Sep 17 00:00:00 2001 From: Stephen Ngozi Date: Mon, 9 Feb 2026 06:35:23 +0100 Subject: [PATCH 1/6] Add Rust project files and .gitignore --- .../Stephen-Ngozi-Merkle-Tree/.gitignore | 1 + .../Stephen-Ngozi-Merkle-Tree/Cargo.lock | 7 ++ .../Stephen-Ngozi-Merkle-Tree/Cargo.toml | 6 ++ .../Stephen-Ngozi-Merkle-Tree/src/main.rs | 80 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/.gitignore create mode 100644 submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/Cargo.lock create mode 100644 submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/Cargo.toml create mode 100644 submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/main.rs diff --git a/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/.gitignore b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/.gitignore new file mode 100644 index 0000000..c41cc9e --- /dev/null +++ b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/.gitignore @@ -0,0 +1 @@ +/target \ No newline at end of file diff --git a/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/Cargo.lock b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/Cargo.lock new file mode 100644 index 0000000..ae2415d --- /dev/null +++ b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "Stephen-Ngozi-Merkle-Tree" +version = "0.1.0" diff --git a/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/Cargo.toml b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/Cargo.toml new file mode 100644 index 0000000..0587fb5 --- /dev/null +++ b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "Stephen-Ngozi-Merkle-Tree" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/main.rs b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/main.rs new file mode 100644 index 0000000..a544e80 --- /dev/null +++ b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/main.rs @@ -0,0 +1,80 @@ +use std::{collections::HashMap, io}; + +// Expense tracker +// Add the expenses +// Remove +// Update +// View + +// Hashmaps +// structs +// enums +// Hashmaps + +#[derive(Debug, Clone)] +enum TransactionType { + Credit, + Debit, +} +#[derive(Debug, Clone)] +struct Expense { + id: u8, + name: String, + amount: f64, + tx_type: TransactionType, +} + +struct ExpenseTracker { + values: HashMap, + next_id: u8, +} + +impl ExpenseTracker { + fn new() -> Self { + Self { + values: HashMap::new(), + next_id: 1, + } + } + + fn add(&mut self, name: String, amount: f64, tx_type: TransactionType) -> Expense { + let current_id = self.next_id; + let new_expense = Expense { + id: current_id, + name, + amount, + tx_type, + }; + self.values.insert(current_id, new_expense.clone()); + self.next_id += 1; + new_expense + } + + fn view_all(&self) -> Vec<&Expense> { + self.values.values().collect() + } + + fn update(&mut self, id: u8, amount: f64, tx_type: TransactionType) -> bool { + match self.values.get_mut(&id) { + Some(exp) => { + exp.amount = amount; + exp.tx_type = tx_type; + true + } + None => false, + } + // let updated_expense = Expense { + // id, + // amount, + // tx_type, + // }; + // self.values.put(id) + } + + fn delete(&mut self, id: u8) -> bool { + self.values.remove(&id).is_some() + } +} +fn main() { + println!("Hello, world!"); +} From 3eefd82f80a639c179ce0014bf968a926ba16bb3 Mon Sep 17 00:00:00 2001 From: Stephen Ngozi Date: Mon, 9 Feb 2026 10:37:03 +0100 Subject: [PATCH 2/6] Initialize Week 2 Day 5 Assignment - Stephen Ngozi --- .../Stephen-Ngozi-Merkle-Tree/src/expense.rs | 17 ++ .../Stephen-Ngozi-Merkle-Tree/src/main.rs | 209 +++++++++++------- .../Stephen-Ngozi-Merkle-Tree/src/tracker.rs | 84 +++++++ .../stephen_expences.txt | 2 + 4 files changed, 238 insertions(+), 74 deletions(-) create mode 100644 submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/expense.rs create mode 100644 submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/tracker.rs create mode 100644 submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/stephen_expences.txt diff --git a/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/expense.rs b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/expense.rs new file mode 100644 index 0000000..986fe5d --- /dev/null +++ b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/expense.rs @@ -0,0 +1,17 @@ + + +#[derive(Debug, Clone)] +pub enum TransactionType { + Credit, + Debit, +} + + + +#[derive(Debug, Clone)] +pub struct Expense { + pub id: u8, + pub name: String, + pub amount: f64, + pub tx_type: TransactionType, +} \ No newline at end of file diff --git a/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/main.rs b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/main.rs index a544e80..eec5b9f 100644 --- a/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/main.rs +++ b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/main.rs @@ -1,80 +1,141 @@ -use std::{collections::HashMap, io}; - -// Expense tracker -// Add the expenses -// Remove -// Update -// View - -// Hashmaps -// structs -// enums -// Hashmaps - -#[derive(Debug, Clone)] -enum TransactionType { - Credit, - Debit, -} -#[derive(Debug, Clone)] -struct Expense { - id: u8, - name: String, - amount: f64, - tx_type: TransactionType, -} +mod expense; +mod tracker; + +use std::io; +use expense::TransactionType; +use tracker::ExpenseTracker; + -struct ExpenseTracker { - values: HashMap, - next_id: u8, + + +fn print_menu() { + println!("\n=== Expense Tracker ==="); + println!("1 - Add Expense"); + println!("2 - View Expenses"); + println!("3 - Update Expense"); + println!("4 - Delete Expense"); + println!("q - Quit"); } +fn main() { + // println!("Hello, world!"); -impl ExpenseTracker { - fn new() -> Self { - Self { - values: HashMap::new(), - next_id: 1, - } - } - - fn add(&mut self, name: String, amount: f64, tx_type: TransactionType) -> Expense { - let current_id = self.next_id; - let new_expense = Expense { - id: current_id, - name, - amount, - tx_type, - }; - self.values.insert(current_id, new_expense.clone()); - self.next_id += 1; - new_expense - } - - fn view_all(&self) -> Vec<&Expense> { - self.values.values().collect() - } - - fn update(&mut self, id: u8, amount: f64, tx_type: TransactionType) -> bool { - match self.values.get_mut(&id) { - Some(exp) => { - exp.amount = amount; - exp.tx_type = tx_type; - true + let mut tracker = ExpenseTracker::new(); + + loop { + print_menu(); + + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Failed"); + let input = input.trim(); + + + if input == "q" { + println!("Are you sure you want to quit? (y/n)"); + let mut confirm = String::new(); + io::stdin().read_line(&mut confirm).expect("Failed "); + let confirm = confirm.trim(); + + if confirm == "y" { + tracker.save_to_file("stephen_expences.txt"); + println!("Data saved to stephen_expences.txt"); + println!("Bye bye go home!"); + break; + } + }else if input == "1" { + println!("Enter expense name:"); + let mut name = String::new(); + io::stdin().read_line(&mut name).expect("Failed to read input"); + let name = name.trim().to_string(); + + println!("Enter amount:"); + let mut amount_input = String::new(); + io::stdin().read_line(&mut amount_input).expect("Failed to read input"); + let amount: f64 = amount_input.trim().parse().expect("Please enter a valid number"); + + println!("Is this Credit or Debit? (c/d):"); + let mut tx_input = String::new(); + io::stdin().read_line(&mut tx_input).expect("Failed to read input"); + let tx_input = tx_input.trim(); + + let tx_type = if tx_input == "c" { + TransactionType::Credit + } else { + TransactionType::Debit + }; + + let new_expense = tracker.add(name, amount, tx_type); + println!(" Expense added successfully!"); + println!("ID: {}, Name: {}, Amount: ${:.2}, Type: {:?}", + new_expense.id, new_expense.name, new_expense.amount, new_expense.tx_type); + }else if input == "2" { + let expenses = tracker.view_all(); + + if expenses.is_empty() { + println!("No Expenses in the Tracker"); + } else { + println!("All Expenses"); + for expense in expenses { + println!("ID: {} | Name: {} | Amount: Naria{:.2} | Type: {:?}", + expense.id, expense.name, expense.amount, expense.tx_type); + } + } + } else if input == "3" { + println!("Enter the ID of the Expence"); + + let mut new_id_input = String::new(); + + io::stdin().read_line(&mut new_id_input).expect("Failed to read input"); + let id: u8 = new_id_input.trim().parse().expect("Please enter a valid ID"); + + + + println!("Enter new amount:"); + let mut amount_input = String::new(); + io::stdin().read_line(&mut amount_input).expect("Failed to read input"); + let amount: f64 = amount_input.trim().parse().expect("Please enter a valid number"); + + println!("Is this Credit or Debit? (c/d):"); + let mut tx_input = String::new(); + io::stdin().read_line(&mut tx_input).expect("Failed to read input"); + let tx_input = tx_input.trim(); + + let tx_type = if tx_input == "c" { + TransactionType::Credit + } else { + TransactionType::Debit + }; + + if tracker.update(id, amount, tx_type){ + println!("Expense Updated Successfu"); + + if let Some(expense) = tracker.values.get(&id) { + println!("ID: {}, Name: {}, Amount: Naria{:.2}, Type: {:?}", + expense.id, expense.name, expense.amount, expense.tx_type); + }else { + println!("Expense with ID {} not found!", id); + + } + } + }else if input == "4" { + println!("Enter the ID of the expense you to delete:"); + + let mut id_input = String::new(); + + io::stdin().read_line(&mut id_input).expect("Failed to read input"); + let id: u8 = id_input.trim().parse().expect("Please enter a valid ID"); + + if tracker.delete(id){ + println!(" Your Expence is Deleted "); + }else { + println!("Expense with ID {} not found!", id); + } + }else { + println!("Invalid choice. Please try again."); } - None => false, + + + + } - // let updated_expense = Expense { - // id, - // amount, - // tx_type, - // }; - // self.values.put(id) - } - - fn delete(&mut self, id: u8) -> bool { - self.values.remove(&id).is_some() - } -} -fn main() { - println!("Hello, world!"); + } diff --git a/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/tracker.rs b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/tracker.rs new file mode 100644 index 0000000..d454c42 --- /dev/null +++ b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/src/tracker.rs @@ -0,0 +1,84 @@ +use std::{collections::HashMap,fs::File,io::Write}; +use crate::expense::{Expense, TransactionType}; + + +// Expense tracker +// Add the expenses +// Remove +// Update +// View + +// Hashmaps +// structs +// enums +// Hashmaps + + + + +pub struct ExpenseTracker { + pub values: HashMap, + pub next_id: u8, +} + +impl ExpenseTracker { + pub fn new() -> Self { + Self { + values: HashMap::new(), + next_id: 1, + } + } + + pub fn add(&mut self, name: String, amount: f64, tx_type: TransactionType) -> Expense { + let current_id = self.next_id; + let new_expense = Expense { + id: current_id, + name, + amount, + tx_type, + }; + self.values.insert(current_id, new_expense.clone()); + self.next_id += 1; + new_expense + } + + pub fn view_all(&self) -> Vec<&Expense> { + let mut expenses: Vec<&Expense> = self.values.values().collect(); + + expenses.sort_by(|a, b|{a.id.cmp(&b.id)}); + expenses + + } + + pub fn update(&mut self, id: u8, amount: f64, tx_type: TransactionType) -> bool { + match self.values.get_mut(&id) { + Some(exp) => { + exp.amount = amount; + exp.tx_type = tx_type; + true + } + None => false, + } + // let updated_expense = Expense { + // id, + // amount, + // tx_type, + // }; + // self.values.put(id) + } + + pub fn delete(&mut self, id: u8) -> bool { + self.values.remove(&id).is_some() + } + + + pub fn save_to_file(&self, filename: &str) { + let mut file = File::create(filename).expect("Could not create file"); + + for expense in self.values.values() { + let line = format!("ID: {}, Name: {}, Amount: {:.2}, Type: {:?}\n", + expense.id, expense.name, expense.amount, expense.tx_type); + file.write_all(line.as_bytes()).expect("Could not write to file"); + } + } +} \ No newline at end of file diff --git a/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/stephen_expences.txt b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/stephen_expences.txt new file mode 100644 index 0000000..15371be --- /dev/null +++ b/submissions/week-2/day-5/Stephen-Ngozi-Merkle-Tree/stephen_expences.txt @@ -0,0 +1,2 @@ +ID: 2, Name: Beans, Amount: 5000.00, Type: Credit +ID: 1, Name: Rice, Amount: 20000.00, Type: Credit From 57f9c7491c4ed30f49f27119675aeb6a49663ad5 Mon Sep 17 00:00:00 2001 From: Stephen Ngozi Date: Mon, 9 Feb 2026 11:20:41 +0100 Subject: [PATCH 3/6] Assignment on user-Input --- .../week-2/day-3/Stephen-Ngozi-User-Input/.gitignore | 1 + .../week-2/day-3/Stephen-Ngozi-User-Input/Cargo.lock | 7 +++++++ .../week-2/day-3/Stephen-Ngozi-User-Input/Cargo.toml | 6 ++++++ .../week-2/day-3/Stephen-Ngozi-User-Input/src/main.rs | 3 +++ 4 files changed, 17 insertions(+) create mode 100644 submissions/week-2/day-3/Stephen-Ngozi-User-Input/.gitignore create mode 100644 submissions/week-2/day-3/Stephen-Ngozi-User-Input/Cargo.lock create mode 100644 submissions/week-2/day-3/Stephen-Ngozi-User-Input/Cargo.toml create mode 100644 submissions/week-2/day-3/Stephen-Ngozi-User-Input/src/main.rs diff --git a/submissions/week-2/day-3/Stephen-Ngozi-User-Input/.gitignore b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/.gitignore @@ -0,0 +1 @@ +/target diff --git a/submissions/week-2/day-3/Stephen-Ngozi-User-Input/Cargo.lock b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/Cargo.lock new file mode 100644 index 0000000..483ae47 --- /dev/null +++ b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "Stephen-Ngozi-User-Input" +version = "0.1.0" diff --git a/submissions/week-2/day-3/Stephen-Ngozi-User-Input/Cargo.toml b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/Cargo.toml new file mode 100644 index 0000000..e17216a --- /dev/null +++ b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "Stephen-Ngozi-User-Input" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/submissions/week-2/day-3/Stephen-Ngozi-User-Input/src/main.rs b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 14de3baa30c5b31628a0af4d825497fc1bbe841d Mon Sep 17 00:00:00 2001 From: Stephen Ngozi Date: Mon, 9 Feb 2026 11:27:37 +0100 Subject: [PATCH 4/6] Add Week 2 Day 3 task for User Input --- .../Stephen-Ngozi-User-Input/src/main.rs | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/submissions/week-2/day-3/Stephen-Ngozi-User-Input/src/main.rs b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/src/main.rs index e7a11a9..8870704 100644 --- a/submissions/week-2/day-3/Stephen-Ngozi-User-Input/src/main.rs +++ b/submissions/week-2/day-3/Stephen-Ngozi-User-Input/src/main.rs @@ -1,3 +1,50 @@ + +use std::io; +enum PowerStatus { + Off, + Sleep, + Reboot, + Shutdown, + Hibernate, +} + +fn format_power_status(input: &str) -> Option { + match input.to_lowercase().as_str() { + "off" => Some(PowerStatus::Off), + "sleep" => Some(PowerStatus::Sleep), + "reboot" => Some(PowerStatus::Reboot), + "shutdown" => Some(PowerStatus::Shutdown), + "hibernate" => Some(PowerStatus::Hibernate), + _ => None, + } +} + + +fn handle_power_status(status: PowerStatus) { + match status { + PowerStatus::Off => println!("Turning off the computer"), + PowerStatus::Sleep => println!("Putting the computer to sleep"), + PowerStatus::Reboot => println!("Rebooting the computer"), + PowerStatus::Shutdown => println!("Shutting down the computer"), + PowerStatus::Hibernate => println!("Hibernating the computer"), + } +} + + + fn main() { - println!("Hello, world!"); + println!("Enter power option (off, sleep, reboot, shutdown, hibernate):"); + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read input"); + + let input = input.trim(); + + match format_power_status(input) { + Some(status) => handle_power_status(status), + None => println!("Error: Invalid power option"), + } } + \ No newline at end of file From fc1e7777dc2606397d1d6a3766e797b60c141571 Mon Sep 17 00:00:00 2001 From: Stephen Ngozi Date: Thu, 12 Feb 2026 05:31:08 +0100 Subject: [PATCH 5/6] Add Week 3 Day-3 - CLI- Project Stephen Ngozi --- .../week-3/day-3/stephenngozi-build-cli-project/.gitignore | 1 + .../week-3/day-3/stephenngozi-build-cli-project/Cargo.lock | 7 +++++++ .../week-3/day-3/stephenngozi-build-cli-project/Cargo.toml | 6 ++++++ .../day-3/stephenngozi-build-cli-project/src/main.rs | 3 +++ 4 files changed, 17 insertions(+) create mode 100644 submissions/week-3/day-3/stephenngozi-build-cli-project/.gitignore create mode 100644 submissions/week-3/day-3/stephenngozi-build-cli-project/Cargo.lock create mode 100644 submissions/week-3/day-3/stephenngozi-build-cli-project/Cargo.toml create mode 100644 submissions/week-3/day-3/stephenngozi-build-cli-project/src/main.rs diff --git a/submissions/week-3/day-3/stephenngozi-build-cli-project/.gitignore b/submissions/week-3/day-3/stephenngozi-build-cli-project/.gitignore new file mode 100644 index 0000000..c41cc9e --- /dev/null +++ b/submissions/week-3/day-3/stephenngozi-build-cli-project/.gitignore @@ -0,0 +1 @@ +/target \ No newline at end of file diff --git a/submissions/week-3/day-3/stephenngozi-build-cli-project/Cargo.lock b/submissions/week-3/day-3/stephenngozi-build-cli-project/Cargo.lock new file mode 100644 index 0000000..0677c71 --- /dev/null +++ b/submissions/week-3/day-3/stephenngozi-build-cli-project/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "stephenngozi-build-cli-project" +version = "0.1.0" diff --git a/submissions/week-3/day-3/stephenngozi-build-cli-project/Cargo.toml b/submissions/week-3/day-3/stephenngozi-build-cli-project/Cargo.toml new file mode 100644 index 0000000..e5c586f --- /dev/null +++ b/submissions/week-3/day-3/stephenngozi-build-cli-project/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "stephenngozi-build-cli-project" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/submissions/week-3/day-3/stephenngozi-build-cli-project/src/main.rs b/submissions/week-3/day-3/stephenngozi-build-cli-project/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/submissions/week-3/day-3/stephenngozi-build-cli-project/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 039b0d93dd8b127010204499c77a6ee8d7460c0b Mon Sep 17 00:00:00 2001 From: Stephen Ngozi Date: Thu, 12 Feb 2026 07:34:17 +0100 Subject: [PATCH 6/6] Add Week 3 Day 3 task for StephenNgozi Build CLI Project --- .../stephenngozi-build-cli-project/output.txt | 4 + .../stephenngozi-build-cli-project/poem.txt | 9 ++ .../stephenngozi-build-cli-project/src/lib.rs | 106 ++++++++++++++++++ .../src/main.rs | 27 ++++- 4 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 submissions/week-3/day-3/stephenngozi-build-cli-project/output.txt create mode 100644 submissions/week-3/day-3/stephenngozi-build-cli-project/poem.txt create mode 100644 submissions/week-3/day-3/stephenngozi-build-cli-project/src/lib.rs diff --git a/submissions/week-3/day-3/stephenngozi-build-cli-project/output.txt b/submissions/week-3/day-3/stephenngozi-build-cli-project/output.txt new file mode 100644 index 0000000..465ad89 --- /dev/null +++ b/submissions/week-3/day-3/stephenngozi-build-cli-project/output.txt @@ -0,0 +1,4 @@ + searching for to +In File poem.txt +Are you nobody, too? +How dreary to be somebody! diff --git a/submissions/week-3/day-3/stephenngozi-build-cli-project/poem.txt b/submissions/week-3/day-3/stephenngozi-build-cli-project/poem.txt new file mode 100644 index 0000000..3a260bf --- /dev/null +++ b/submissions/week-3/day-3/stephenngozi-build-cli-project/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! \ No newline at end of file diff --git a/submissions/week-3/day-3/stephenngozi-build-cli-project/src/lib.rs b/submissions/week-3/day-3/stephenngozi-build-cli-project/src/lib.rs new file mode 100644 index 0000000..841aadc --- /dev/null +++ b/submissions/week-3/day-3/stephenngozi-build-cli-project/src/lib.rs @@ -0,0 +1,106 @@ + +use std::fs; +use std::error::Error; +use std::env; + + + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.filename)?; + + let results = if config.case_sensitive { + search(&config.query, &contents) + }else { + search_case_insensitive(&config.query, &contents) + }; + + for line in results{ + println!("{}", line); + + } + + Ok(()) +} + +pub struct Config { + pub query : String, + pub filename : String, + pub case_sensitive: bool, +} + +impl Config { + + pub fn new (args: &[String]) -> Result { + + if args.len() < 3 { + return Err("not enough arguments"); + } + let query = args[1].clone(); + + let filename = args[2].clone(); + + let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); + + Ok(Config{query, filename,case_sensitive }) + } +} + +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 test { + 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) + ); + } + +} \ No newline at end of file diff --git a/submissions/week-3/day-3/stephenngozi-build-cli-project/src/main.rs b/submissions/week-3/day-3/stephenngozi-build-cli-project/src/main.rs index e7a11a9..15fdc62 100644 --- a/submissions/week-3/day-3/stephenngozi-build-cli-project/src/main.rs +++ b/submissions/week-3/day-3/stephenngozi-build-cli-project/src/main.rs @@ -1,3 +1,28 @@ +use std::env; +use std::process; +use stephenngozi_build_cli_project::Config; + + fn main() { - println!("Hello, world!"); + + let args: Vec = env::args().collect(); + + let config = Config::new(&args).unwrap_or_else(|err |{ + eprintln!("Problem parsing argumemts: {}", err); + process::exit(1); + }); + + + println!(" searching for {}", config.query); + println!("In File {}", config.filename); + + if let Err(e) = stephenngozi_build_cli_project::run(config){ + eprintln!("Application Error: {}", e); + process::exit(1); + } + } + + + +