Switch from Orca to Dolphin. Finish up removing code to be implemented.
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
// 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)
|
||||
// 1. Create a DolphinError type representing the following three conditions:
|
||||
// - Hungry - An dolphin is hungry
|
||||
// - TooYoung - An dolphin is too young
|
||||
// - LongName - The dolphin's name is too long and annoying to say
|
||||
//
|
||||
// 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
|
||||
// (3) Don't expose error types other than your own (you don't have to do anything for this one)
|
||||
// (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
|
||||
@@ -14,45 +14,32 @@
|
||||
// Once you have completed the error type correctly, you should be able to run `cargo build --lib`
|
||||
// without any errors.
|
||||
|
||||
// pub enum OrcaError...
|
||||
// pub enum DolphinError...
|
||||
|
||||
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 struct Dolphin {
|
||||
pub name: String,
|
||||
pub age: u8,
|
||||
pub hungry: bool,
|
||||
}
|
||||
|
||||
impl Orca {
|
||||
pub fn say_your_name(&self) -> Result<String, OrcaError> {
|
||||
impl Dolphin {
|
||||
pub fn say_your_name(&self) -> Result<String, DolphinError> {
|
||||
if self.name.len() > 10 {
|
||||
Err(OrcaError::LongName)
|
||||
Err(DolphinError::LongName)
|
||||
} else {
|
||||
Ok(format!("Hi, my name is {} and I'm an Orca!", self.name))
|
||||
Ok(format!("Hi, my name is {} and I'm a Dolphin!", self.name))
|
||||
}
|
||||
}
|
||||
pub fn flip(&self) -> Result<String, OrcaError> {
|
||||
pub fn flip(&self) -> Result<String, DolphinError> {
|
||||
if self.age < 4 {
|
||||
Err(OrcaError::TooYoung)
|
||||
Err(DolphinError::TooYoung)
|
||||
} else {
|
||||
Ok(format!("Yippee, I'm doing a flip!"))
|
||||
}
|
||||
}
|
||||
pub fn shake_hands(&self) -> Result<String, OrcaError> {
|
||||
pub fn shake_hands(&self) -> Result<String, DolphinError> {
|
||||
if self.hungry {
|
||||
Err(OrcaError::Hungry)
|
||||
Err(DolphinError::Hungry)
|
||||
} else {
|
||||
Ok(format!("Nice to meet you, let's shake hands!"))
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
// START IN lib.rs!
|
||||
|
||||
use aquarium::Orca;
|
||||
use aquarium::Dolphin;
|
||||
|
||||
// (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<Vec<String>>. The vector of Strings will represent successful
|
||||
// outcomes of various performances.
|
||||
// - Have the play_time function return a `Result<Vec<String>>`. The vector of Strings will
|
||||
// represent successful outcomes of various dolphin tricks.
|
||||
|
||||
// fn play_time(orca: &Orca) -> ... {
|
||||
// fn play_time(dolphin: &Dolphin) -> ... {
|
||||
// let mut responses = vec![];
|
||||
// // 2b. There are three methods on the Orca struct:
|
||||
// // 2b. There are three methods on the Dolphin 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
|
||||
// // - Call the method on `dolphin`, 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...
|
||||
@@ -26,37 +26,37 @@ use aquarium::Orca;
|
||||
// }
|
||||
|
||||
fn main() {
|
||||
let orcas = vec![
|
||||
Orca {
|
||||
let dolphins = vec![
|
||||
Dolphin {
|
||||
name: "Augustinius".into(),
|
||||
age: 7,
|
||||
hungry: false,
|
||||
},
|
||||
Orca {
|
||||
Dolphin {
|
||||
name: "Bitty".into(),
|
||||
age: 2,
|
||||
hungry: true,
|
||||
},
|
||||
Orca {
|
||||
Dolphin {
|
||||
name: "Carson".into(),
|
||||
age: 5,
|
||||
hungry: true,
|
||||
},
|
||||
Orca {
|
||||
Dolphin {
|
||||
name: "Devin".into(),
|
||||
age: 6,
|
||||
hungry: false,
|
||||
},
|
||||
];
|
||||
for orca in &orcas {
|
||||
match play_time(orca) {
|
||||
for dolphin in &dolphins {
|
||||
match play_time(dolphin) {
|
||||
Ok(responses) => {
|
||||
println!("{} did a FABULOUS PERFORMANCE!", orca.name);
|
||||
println!("{} did a FABULOUS PERFORMANCE!", dolphin.name);
|
||||
for response in responses {
|
||||
println!(" {}", response);
|
||||
}
|
||||
}
|
||||
Err(e) => println!("{} can't perform today: {}", orca.name, e.to_string()),
|
||||
Err(e) => println!("{} can't perform today: {}", dolphin.name, e.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user