better error handling

This commit is contained in:
Adrian Malacoda 2017-02-19 00:40:21 -06:00
parent 94c5952a0c
commit bec5b48601
3 changed files with 32 additions and 11 deletions

View File

@ -8,7 +8,8 @@ pub mod ninjas;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
Http(hyper::error::Error), Http(hyper::error::Error),
Io(std::io::Error) Io(std::io::Error),
Parse(String)
} }
impl From<hyper::error::Error> for Error { impl From<hyper::error::Error> for Error {
@ -23,6 +24,12 @@ impl From<std::io::Error> for Error {
} }
} }
impl From<std::num::ParseIntError> for Error {
fn from (err: std::num::ParseIntError) -> Error {
Error::Parse(format!("{}", err))
}
}
pub trait Fighter { pub trait Fighter {
fn power (&self) -> u8; fn power (&self) -> u8;
} }

View File

@ -53,8 +53,11 @@ pub struct Ninja {
impl Ninja { impl Ninja {
pub fn from_html (contents: &str) -> Result<Ninja, Error> { pub fn from_html (contents: &str) -> Result<Ninja, Error> {
let document = Document::from(contents); let document = Document::from(contents);
let full_name = document.find(Class("ninja-name")).first()
.ok_or_else(|| Error::Parse(String::from("could not find ninja-name element")))?.text();
let mut ninja = Ninja { let mut ninja = Ninja {
name: String::from(document.find(Class("ninja-name")).first().unwrap().text()), name: String::from(full_name),
sneakiness: 0, sneakiness: 0,
pajamas: 0, pajamas: 0,
pointy_things: 0, pointy_things: 0,
@ -63,11 +66,15 @@ impl Ninja {
for node in document.find(Class("ninja-skill")).iter() { for node in document.find(Class("ninja-skill")).iter() {
let skill = node.text(); let skill = node.text();
let score = node.parent().unwrap().find(Class("ninja-score")).first().unwrap().text(); let score = node.parent().ok_or_else(|| Error::Parse(String::from("could not get ninja-skill parent element")))?
.find(Class("ninja-score")).first()
.ok_or_else(|| Error::Parse(String::from("could not get ninja-score element")))?
.text();
match &skill[..] { match &skill[..] {
"Sneakiness:" => { ninja.sneakiness = score.parse::<u8>().unwrap() } "Sneakiness:" => { ninja.sneakiness = score.parse::<u8>()? }
"Pajamas:" => { ninja.pajamas = score.parse::<u8>().unwrap() }, "Pajamas:" => { ninja.pajamas = score.parse::<u8>()? },
"Pointy Things:" => { ninja.pointy_things = score.parse::<u8>().unwrap() }, "Pointy Things:" => { ninja.pointy_things = score.parse::<u8>()? },
"Weapons:" => { ninja.weapons = parse_weapons(&score[..]) } "Weapons:" => { ninja.weapons = parse_weapons(&score[..]) }
_ => { } _ => { }
} }

View File

@ -53,8 +53,11 @@ pub struct Pirate {
impl Pirate { impl Pirate {
pub fn from_html (contents: &str) -> Result<Pirate, Error> { pub fn from_html (contents: &str) -> Result<Pirate, Error> {
let document = Document::from(contents); let document = Document::from(contents);
let full_name = document.find(Class("pirate-name")).first()
.ok_or_else(|| Error::Parse(String::from("could not find pirate-name element")))?.text();
let mut pirate = Pirate { let mut pirate = Pirate {
name: String::from(document.find(Class("pirate-name")).first().unwrap().text()), name: String::from(full_name),
swashbuckling: 0, swashbuckling: 0,
drunkenness: 0, drunkenness: 0,
booty: 0, booty: 0,
@ -63,11 +66,15 @@ impl Pirate {
for node in document.find(Class("pirate-skill")).iter() { for node in document.find(Class("pirate-skill")).iter() {
let skill = node.text(); let skill = node.text();
let score = node.parent().unwrap().find(Class("pirate-score")).first().unwrap().text(); let score = node.parent().ok_or_else(|| Error::Parse(String::from("could not get pirate-skill parent element")))?
.find(Class("pirate-score")).first()
.ok_or_else(|| Error::Parse(String::from("could not get pirate-score element")))?
.text();
match &skill[..] { match &skill[..] {
"Swashbuckling:" => { pirate.swashbuckling = score.parse::<u8>().unwrap() } "Swashbuckling:" => { pirate.swashbuckling = score.parse::<u8>()? }
"Drunkenness:" => { pirate.drunkenness = score.parse::<u8>().unwrap() }, "Drunkenness:" => { pirate.drunkenness = score.parse::<u8>()? },
"Booty:" => { pirate.booty = score.parse::<u8>().unwrap() }, "Booty:" => { pirate.booty = score.parse::<u8>()? },
"Weapons:" => { pirate.weapons = parse_weapons(&score[..]) } "Weapons:" => { pirate.weapons = parse_weapons(&score[..]) }
_ => { } _ => { }
} }