diff --git a/src/data_centers.rs b/src/data_centers.rs index 8a72ee4..cf2b643 100644 --- a/src/data_centers.rs +++ b/src/data_centers.rs @@ -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 { 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) diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..4d6a8a9 --- /dev/null +++ b/src/errors.rs @@ -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) + } +} diff --git a/src/jobs.rs b/src/jobs.rs index c316e9d..5f6b9cc 100644 --- a/src/jobs.rs +++ b/src/jobs.rs @@ -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 { 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) diff --git a/src/lib.rs b/src/lib.rs index 87e7f14..aacfd66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ pub mod data_centers; +pub mod errors; pub mod jobs; pub mod roles; pub mod worlds; diff --git a/src/roles.rs b/src/roles.rs index ed135cd..c85bc0b 100644 --- a/src/roles.rs +++ b/src/roles.rs @@ -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 { 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) diff --git a/src/worlds.rs b/src/worlds.rs index ba23516..1e07c64 100644 --- a/src/worlds.rs +++ b/src/worlds.rs @@ -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 { 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)