2018-02-22 06:09:53 +00:00
|
|
|
// errorsn.rs
|
2016-02-17 23:46:10 +00:00
|
|
|
// This is a bigger error exercise than the previous ones!
|
2018-03-06 01:10:19 +00:00
|
|
|
// You can do it! :)
|
2016-02-17 23:46:10 +00:00
|
|
|
//
|
|
|
|
// Edit the `read_and_validate` function so that it compiles and
|
|
|
|
// passes the tests... so many things could go wrong!
|
|
|
|
//
|
|
|
|
// - Reading from stdin could produce an io::Error
|
|
|
|
// - Parsing the input could produce a num::ParseIntError
|
|
|
|
// - Validating the input could produce a CreationError (defined below)
|
|
|
|
//
|
|
|
|
// How can we lump these errors into one general error? That is, what
|
|
|
|
// type goes where the question marks are, and how do we return
|
|
|
|
// that type from the body of read_and_validate?
|
|
|
|
//
|
|
|
|
// Scroll down for hints :)
|
|
|
|
|
2019-11-11 12:38:24 +00:00
|
|
|
// I AM NOT DONE
|
|
|
|
|
2016-02-17 23:46:10 +00:00
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
// PositiveNonzeroInteger is a struct defined below the tests.
|
2019-08-20 21:52:24 +00:00
|
|
|
fn read_and_validate(b: &mut dyn io::BufRead) -> Result<PositiveNonzeroInteger, ???> {
|
2016-02-17 23:46:10 +00:00
|
|
|
let mut line = String::new();
|
|
|
|
b.read_line(&mut line);
|
|
|
|
let num: i64 = line.trim().parse();
|
2016-06-21 15:10:21 +00:00
|
|
|
let answer = PositiveNonzeroInteger::new(num);
|
|
|
|
answer
|
2016-02-17 23:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a test helper function that turns a &str into a BufReader.
|
2019-08-20 21:52:24 +00:00
|
|
|
fn test_with_str(s: &str) -> Result<PositiveNonzeroInteger, Box<dyn error::Error>> {
|
2016-02-17 23:46:10 +00:00
|
|
|
let mut b = io::BufReader::new(s.as_bytes());
|
|
|
|
read_and_validate(&mut b)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_success() {
|
|
|
|
let x = test_with_str("42\n");
|
|
|
|
assert_eq!(PositiveNonzeroInteger(42), x.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_num() {
|
|
|
|
let x = test_with_str("eleven billion\n");
|
|
|
|
assert!(x.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_non_positive() {
|
|
|
|
let x = test_with_str("-40\n");
|
|
|
|
assert!(x.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ioerror() {
|
|
|
|
struct Broken;
|
|
|
|
impl io::Read for Broken {
|
|
|
|
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
Err(io::Error::new(io::ErrorKind::BrokenPipe, "uh-oh!"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut b = io::BufReader::new(Broken);
|
|
|
|
assert!(read_and_validate(&mut b).is_err());
|
2016-06-21 15:10:21 +00:00
|
|
|
assert_eq!("uh-oh!", read_and_validate(&mut b).unwrap_err().to_string());
|
2016-02-17 23:46:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 11:48:32 +00:00
|
|
|
#[derive(PartialEq, Debug)]
|
2016-02-17 23:46:10 +00:00
|
|
|
struct PositiveNonzeroInteger(u64);
|
|
|
|
|
|
|
|
impl PositiveNonzeroInteger {
|
|
|
|
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
|
|
|
if value == 0 {
|
|
|
|
Err(CreationError::Zero)
|
|
|
|
} else if value < 0 {
|
|
|
|
Err(CreationError::Negative)
|
|
|
|
} else {
|
|
|
|
Ok(PositiveNonzeroInteger(value as u64))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_positive_nonzero_integer_creation() {
|
|
|
|
assert!(PositiveNonzeroInteger::new(10).is_ok());
|
2019-05-22 11:48:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
Err(CreationError::Negative),
|
|
|
|
PositiveNonzeroInteger::new(-10)
|
|
|
|
);
|
2016-02-17 23:46:10 +00:00
|
|
|
assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0));
|
|
|
|
}
|
|
|
|
|
2019-05-22 11:48:32 +00:00
|
|
|
#[derive(PartialEq, Debug)]
|
2016-02-17 23:46:10 +00:00
|
|
|
enum CreationError {
|
|
|
|
Negative,
|
|
|
|
Zero,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CreationError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-08-20 21:52:24 +00:00
|
|
|
f.write_str((self as &dyn error::Error).description())
|
2016-02-17 23:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for CreationError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
CreationError::Negative => "Negative",
|
|
|
|
CreationError::Zero => "Zero",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 11:47:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-17 23:46:10 +00:00
|
|
|
// First hint: To figure out what type should go where the ??? is, take a look
|
|
|
|
// at the test helper function `test_with_str`, since it returns whatever
|
|
|
|
// `read_and_validate` returns and`test_with_str` has its signature fully
|
|
|
|
// specified.
|
|
|
|
|
2019-05-23 09:21:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-21 15:10:21 +00:00
|
|
|
// Next hint: There are three places in `read_and_validate` that we call a
|
|
|
|
// function that returns a `Result` (that is, the functions might fail).
|
2018-11-09 19:31:14 +00:00
|
|
|
// Apply the `?` operator on those calls so that we return immediately from
|
2016-06-21 15:10:21 +00:00
|
|
|
// `read_and_validate` if those function calls fail.
|
2016-02-17 23:46:10 +00:00
|
|
|
|
2019-05-23 09:21:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-09 19:31:14 +00:00
|
|
|
// Another hint: under the hood, the `?` operator calls `From::from`
|
2019-08-20 21:52:24 +00:00
|
|
|
// on the error value to convert it to a boxed trait object, a Box<dyn error::Error>,
|
2016-06-21 15:10:21 +00:00
|
|
|
// which is polymorphic-- that means that lots of different kinds of errors
|
|
|
|
// can be returned from the same function because all errors act the same
|
|
|
|
// since they all implement the `error::Error` trait.
|
|
|
|
// Check out this section of the book:
|
2019-06-07 03:17:22 +00:00
|
|
|
// https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
|
2016-06-21 15:10:21 +00:00
|
|
|
|
2019-05-23 09:21:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-09 19:31:14 +00:00
|
|
|
// Another another hint: Note that because the `?` operator returns
|
2016-06-21 15:10:21 +00:00
|
|
|
// the *unwrapped* value in the `Ok` case, if we want to return a `Result` from
|
|
|
|
// `read_and_validate` for *its* success case, we'll have to rewrap a value
|
2018-11-09 19:31:14 +00:00
|
|
|
// that we got from the return value of a `?`ed call in an `Ok`-- this will
|
2016-06-21 15:10:21 +00:00
|
|
|
// look like `Ok(something)`.
|
|
|
|
|
2019-05-23 09:21:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-21 15:10:21 +00:00
|
|
|
// Another another another hint: `Result`s must be "used", that is, you'll
|
|
|
|
// get a warning if you don't handle a `Result` that you get in your
|
|
|
|
// function. Read more about that in the `std::result` module docs:
|
|
|
|
// https://doc.rust-lang.org/std/result/#results-must-be-used
|