reorganize

This commit is contained in:
Adrian Malacoda 2017-02-19 02:01:58 -06:00
parent 50e04d5f69
commit f80694e1b6
4 changed files with 70 additions and 66 deletions

View File

@ -4,11 +4,7 @@ extern crate select;
pub mod pirates; pub mod pirates;
pub mod ninjas; pub mod ninjas;
mod parser;
use select::document::Document;
use select::predicate::Class;
use std::collections::BTreeMap;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
@ -38,62 +34,3 @@ impl From<std::num::ParseIntError> for Error {
pub trait Fighter { pub trait Fighter {
fn power (&self) -> u8; fn power (&self) -> u8;
} }
struct Parser {
fighter_type: &'static str,
document: Document
}
impl Parser {
pub fn parse_name (&self) -> Result<String, Error> {
let name_class = format!("{}-name", self.fighter_type);
self.document.find(Class(&name_class[..])).first()
.ok_or_else(|| Error::Parse(format!("could not find {} element", name_class))).map(|result| result.text())
}
pub fn parse_skills (&self) -> Result<Skills, Error> {
let skill_class = format!("{}-skill", self.fighter_type);
let score_class = format!("{}-score", self.fighter_type);
let map: Result<BTreeMap<String, Skill>, Error> = self.document.find(Class(&skill_class[..])).iter().map(|node| {
let skill = node.text();
let score = node.parent().ok_or_else(|| Error::Parse(format!("could not get {} parent element", skill_class)))?
.find(Class(&score_class[..])).first()
.ok_or_else(|| Error::Parse(format!("could not get {} element", score_class)))?
.text();
Result::Ok((skill, Skill { score: score }))
}).collect();
Result::Ok(Skills {
map: map?
})
}
}
struct Skills {
map: BTreeMap<String, Skill>
}
impl Skills {
pub fn get (&self, key: &str) -> Result<&Skill, Error> {
self.map.get(key).ok_or_else(|| Error::Parse(format!("could not get {} value", key)))
}
}
struct Skill {
score: String
}
impl Skill {
pub fn as_number (&self) -> Result<u8, std::num::ParseIntError> {
self.score.parse::<u8>()
}
pub fn as_weapons (&self) -> Vec<String> {
if self.score == "None" {
return vec![];
}
self.score.split(", ").into_iter().map(String::from).collect()
}
}

View File

@ -4,7 +4,8 @@ use select::document::Document;
use std::io::Read; use std::io::Read;
use {Error, Fighter, Parser}; use {Error, Fighter};
use parser::Parser;
pub struct Ninjas { pub struct Ninjas {
cache: LruCache<String, Ninja>, cache: LruCache<String, Ninja>,

65
src/parser.rs Normal file
View File

@ -0,0 +1,65 @@
use select::document::Document;
use select::predicate::Class;
use std::collections::BTreeMap;
use Error;
pub struct Parser {
pub fighter_type: &'static str,
pub document: Document
}
impl Parser {
pub fn parse_name (&self) -> Result<String, Error> {
let name_class = format!("{}-name", self.fighter_type);
self.document.find(Class(&name_class[..])).first()
.ok_or_else(|| Error::Parse(format!("could not find {} element", name_class))).map(|result| result.text())
}
pub fn parse_skills (&self) -> Result<Skills, Error> {
let skill_class = format!("{}-skill", self.fighter_type);
let score_class = format!("{}-score", self.fighter_type);
let map: Result<BTreeMap<String, Skill>, Error> = self.document.find(Class(&skill_class[..])).iter().map(|node| {
let skill = node.text();
let score = node.parent().ok_or_else(|| Error::Parse(format!("could not get {} parent element", skill_class)))?
.find(Class(&score_class[..])).first()
.ok_or_else(|| Error::Parse(format!("could not get {} element", score_class)))?
.text();
Result::Ok((skill, Skill { score: score }))
}).collect();
Result::Ok(Skills {
map: map?
})
}
}
pub struct Skills {
map: BTreeMap<String, Skill>
}
impl Skills {
pub fn get (&self, key: &str) -> Result<&Skill, Error> {
self.map.get(key).ok_or_else(|| Error::Parse(format!("could not get {} value", key)))
}
}
pub struct Skill {
score: String
}
impl Skill {
pub fn as_number (&self) -> Result<u8, ::std::num::ParseIntError> {
self.score.parse::<u8>()
}
pub fn as_weapons (&self) -> Vec<String> {
if self.score == "None" {
return vec![];
}
self.score.split(", ").into_iter().map(String::from).collect()
}
}

View File

@ -4,7 +4,8 @@ use select::document::Document;
use std::io::Read; use std::io::Read;
use {Error, Fighter, Parser}; use {Error, Fighter};
use parser::Parser;
pub struct Pirates { pub struct Pirates {
cache: LruCache<String, Pirate>, cache: LruCache<String, Pirate>,