diff --git a/Cargo.toml b/Cargo.toml index 5d7204f..54c4002 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ members = [ "exercise/idiomatic", "exercise/docs", "exercise/closures_iterators", - "exercise/traits" + "exercise/traits", + "exercise/errors", ] diff --git a/exercise/errors/Cargo.toml b/exercise/errors/Cargo.toml new file mode 100644 index 0000000..bdb3b0b --- /dev/null +++ b/exercise/errors/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "aquarium" +version = "0.1.0" +authors = ["Nathan Stocks "] +edition = "2018" + +[dependencies] +# I thought you might want this for your library... +thiserror = "1.0" +# ...and this for your binary +anyhow = "1.0" diff --git a/exercise/errors/src/lib.rs b/exercise/errors/src/lib.rs new file mode 100644 index 0000000..fa2604f --- /dev/null +++ b/exercise/errors/src/lib.rs @@ -0,0 +1,60 @@ +// 1. Create error type(s) representing the following three conditions: +// - An orca is hungry (Hungry) +// - An orca is too young (TooYoung) +// - The orca's name is too long (LongName) +// +// As a reminder, here are the 5 Guidelines for creating an error type +// (1) Use an `enum` for your error type +// (2) Your error conditions should be enum variants grouped in as few enums as makes sense +// (3) Don't expose error types other than your own to users +// (4) Make your enum non-exhaustive +// (5) Implement the Debug, Display, and Error traits +// (5b) You can use thiserror's `Error` macro to derive Display and Error.h +// +// Once you have completed the error type correctly, you should be able to run `cargo build --lib` +// without any errors. + +// pub enum OrcaError... + +use thiserror::Error; + +#[derive(Debug, Error)] +#[non_exhaustive] +pub enum OrcaError { + #[error("I'm too hungry to do that.")] + Hungry, + #[error("I'm too young to do that.")] + TooYoung, + #[error("I would, but my name is just so long!")] + LongName, +} + +pub struct Orca { + pub name: String, + pub age: u8, + pub hungry: bool, +} + +impl Orca { + pub fn say_your_name(&self) -> Result { + if self.name.len() > 10 { + Err(OrcaError::LongName) + } else { + Ok(format!("Hi, my name is {} and I'm an Orca!", self.name)) + } + } + pub fn flip(&self) -> Result { + if self.age < 4 { + Err(OrcaError::TooYoung) + } else { + Ok(format!("Yippee, I'm doing a flip!")) + } + } + pub fn shake_hands(&self) -> Result { + if self.hungry { + Err(OrcaError::Hungry) + } else { + Ok(format!("Nice to meet you, let's shake hands!")) + } + } +} diff --git a/exercise/errors/src/main.rs b/exercise/errors/src/main.rs new file mode 100644 index 0000000..8e2f0c0 --- /dev/null +++ b/exercise/errors/src/main.rs @@ -0,0 +1,62 @@ +// START IN lib.rs! + +use aquarium::Orca; + +// (You already did #1 in lib.rs, right?) +// 2a. Uncomment and finish the play_time function below +// - Bring anyhow::Result into scope with a `use` statement +// - Have the function return a Result>. The vector of Strings will represent successful +// outcomes of various performances. + +// fn play_time(orca: &Orca) -> ... { +// let mut responses = vec![]; +// // 2b. There are three methods on the Orca struct: +// // - .say_your_name() +// // - .flip() +// // - .shake_hands() +// // +// // For each of the three methods above: +// // - Call the method on `orca`, using the `?` operator to unwrap the value / return the error +// // - Push the unwrapped string onto the `responses` vector using the .push() method + +// // let response = ... // this can be done with an intermediate variable... +// // responses.push( ... ) // ...or all on one line. Either way is fine! + +// Ok(responses) +// } + +fn main() { + let orcas = vec![ + Orca { + name: "Augustinius".into(), + age: 7, + hungry: false, + }, + Orca { + name: "Bitty".into(), + age: 2, + hungry: true, + }, + Orca { + name: "Carson".into(), + age: 5, + hungry: true, + }, + Orca { + name: "Devin".into(), + age: 6, + hungry: false, + }, + ]; + for orca in &orcas { + match play_time(orca) { + Ok(responses) => { + println!("{} did a FABULOUS PERFORMANCE!", orca.name); + for response in responses { + println!(" {}", response); + } + } + Err(e) => println!("{} can't perform today: {}", orca.name, e.to_string()), + } + } +}