refactor: use error type instead of unit

This commit is contained in:
Anna 2018-03-29 09:11:19 -04:00
parent 60884483ec
commit 1d408d443b
6 changed files with 25 additions and 8 deletions

View File

@ -1,3 +1,5 @@
use errors::UnknownVariant;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;
@ -34,7 +36,7 @@ impl DataCenter {
}
impl FromStr for DataCenter {
type Err = ();
type Err = UnknownVariant;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let data_center = match s.to_lowercase().as_str() {
@ -44,7 +46,7 @@ impl FromStr for DataCenter {
"gaia" => DataCenter::Gaia,
"mana" => DataCenter::Mana,
"primal" => DataCenter::Primal,
_ => return Err(())
_ => return Err(UnknownVariant("DataCenter", s.into()))
};
Ok(data_center)

10
src/errors.rs Normal file
View File

@ -0,0 +1,10 @@
use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(Debug)]
pub struct UnknownVariant(pub &'static str, pub String);
impl Display for UnknownVariant {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "unknown variant {} for type {}", self.1, self.0)
}
}

View File

@ -1,3 +1,4 @@
use errors::UnknownVariant;
use roles::Role;
use std::fmt::{Display, Formatter, Result as FmtResult};
@ -97,7 +98,7 @@ impl Job {
}
impl FromStr for Job {
type Err = ();
type Err = UnknownVariant;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let job = match s.to_lowercase().as_str() {
@ -119,7 +120,7 @@ impl FromStr for Job {
"paladin" | "pld" => Job::Paladin,
"warrior" | "war" => Job::Warrior,
_ => return Err(())
_ => return Err(UnknownVariant("Job", s.into()))
};
Ok(job)

View File

@ -1,4 +1,5 @@
pub mod data_centers;
pub mod errors;
pub mod jobs;
pub mod roles;
pub mod worlds;

View File

@ -1,3 +1,5 @@
use errors::UnknownVariant;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;
@ -21,14 +23,14 @@ impl Role {
}
impl FromStr for Role {
type Err = ();
type Err = UnknownVariant;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let role = match s.to_lowercase().as_str() {
"dps" => Role::Dps,
"healer" => Role::Healer,
"tank" => Role::Tank,
_ => return Err(())
_ => return Err(UnknownVariant("Role", s.into()))
};
Ok(role)

View File

@ -1,4 +1,5 @@
use data_centers::DataCenter;
use errors::UnknownVariant;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;
@ -322,7 +323,7 @@ impl World {
}
impl FromStr for World {
type Err = ();
type Err = UnknownVariant;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let world = match s.to_lowercase().as_str() {
@ -398,7 +399,7 @@ impl FromStr for World {
"malboro" => World::Malboro,
"ultros" => World::Ultros,
_ => return Err(())
_ => return Err(UnknownVariant("World", s.into()))
};
Ok(world)