ffxiv-types/src/roles.rs

63 lines
1.3 KiB
Rust
Raw Normal View History

2018-03-29 17:20:26 +00:00
//! Job role types
use errors::UnknownVariant;
2018-03-29 00:44:30 +00:00
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;
2018-03-29 17:20:26 +00:00
/// The roles available in the game.
///
2018-03-29 18:36:38 +00:00
/// Each [`Job`] has a role attached to it.
///
/// [`Job`]: ::jobs::Job
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2018-03-31 21:11:03 +00:00
#[cfg_attr(feature = "with_serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "with_serde", serde(rename_all = "snake_case"))]
2018-03-29 00:44:30 +00:00
pub enum Role {
Dps,
Healer,
Tank,
}
impl Role {
2018-03-29 18:41:25 +00:00
#[cfg(feature = "all_const")]
2018-03-29 00:44:30 +00:00
pub const ALL: [Role; 3] = [Role::Dps, Role::Healer, Role::Tank];
pub fn as_str(&self) -> &'static str {
match *self {
Role::Dps => "Dps",
Role::Healer => "Healer",
Role::Tank => "Tank",
}
}
pub fn name(&self) -> &'static str {
2018-03-29 00:44:30 +00:00
match *self {
Role::Dps => "DPS",
Role::Healer => "Healer",
Role::Tank => "Tank",
}
}
}
impl FromStr for Role {
type Err = UnknownVariant;
2018-03-29 00:44:30 +00:00
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(UnknownVariant("Role", s.into()))
2018-03-29 00:44:30 +00:00
};
Ok(role)
}
}
impl Display for Role {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "{}", self.name())
2018-03-29 00:44:30 +00:00
}
}