1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Job role types

use errors::UnknownVariant;

use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;

/// The roles available in the game.
///
/// Each [`Job`] has a role attached to it.
///
/// [`Job`]: ::jobs::Job
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "with_serde", derive(Serialize, Deserialize))]
pub enum Role {
  Dps,
  Healer,
  Tank,
}

impl Role {
  #[cfg(feature = "all_const")]
  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 {
    match *self {
      Role::Dps => "DPS",
      Role::Healer => "Healer",
      Role::Tank => "Tank",
    }
  }
}

impl FromStr for Role {
  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(UnknownVariant("Role", s.into()))
    };

    Ok(role)
  }
}

impl Display for Role {
  fn fmt(&self, f: &mut Formatter) -> FmtResult {
    write!(f, "{}", self.name())
  }
}