use serde::{Deserialize, Serialize};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Sector {
#[serde(alias = "tech")]
Technology,
#[serde(alias = "financials", alias = "financial")]
FinancialServices,
ConsumerCyclical,
#[serde(alias = "communication")]
CommunicationServices,
#[serde(alias = "health")]
Healthcare,
#[serde(alias = "industrial")]
Industrials,
ConsumerDefensive,
Energy,
#[serde(alias = "materials")]
BasicMaterials,
#[serde(alias = "realestate")]
RealEstate,
#[serde(alias = "utility")]
Utilities,
}
impl Sector {
pub fn as_api_path(&self) -> &'static str {
match self {
Sector::Technology => "technology",
Sector::FinancialServices => "financial-services",
Sector::ConsumerCyclical => "consumer-cyclical",
Sector::CommunicationServices => "communication-services",
Sector::Healthcare => "healthcare",
Sector::Industrials => "industrials",
Sector::ConsumerDefensive => "consumer-defensive",
Sector::Energy => "energy",
Sector::BasicMaterials => "basic-materials",
Sector::RealEstate => "real-estate",
Sector::Utilities => "utilities",
}
}
pub fn display_name(&self) -> &'static str {
match self {
Sector::Technology => "Technology",
Sector::FinancialServices => "Financial Services",
Sector::ConsumerCyclical => "Consumer Cyclical",
Sector::CommunicationServices => "Communication Services",
Sector::Healthcare => "Healthcare",
Sector::Industrials => "Industrials",
Sector::ConsumerDefensive => "Consumer Defensive",
Sector::Energy => "Energy",
Sector::BasicMaterials => "Basic Materials",
Sector::RealEstate => "Real Estate",
Sector::Utilities => "Utilities",
}
}
pub fn spdr_etf(&self) -> &'static str {
match self {
Sector::Technology => "XLK",
Sector::FinancialServices => "XLF",
Sector::ConsumerCyclical => "XLY",
Sector::CommunicationServices => "XLC",
Sector::Healthcare => "XLV",
Sector::Industrials => "XLI",
Sector::ConsumerDefensive => "XLP",
Sector::Energy => "XLE",
Sector::BasicMaterials => "XLB",
Sector::RealEstate => "XLRE",
Sector::Utilities => "XLU",
}
}
pub fn valid_types() -> &'static str {
"technology, financial-services, consumer-cyclical, communication-services, \
healthcare, industrials, consumer-defensive, energy, basic-materials, \
real-estate, utilities"
}
pub fn all() -> &'static [Sector] {
&[
Sector::Technology,
Sector::FinancialServices,
Sector::ConsumerCyclical,
Sector::CommunicationServices,
Sector::Healthcare,
Sector::Industrials,
Sector::ConsumerDefensive,
Sector::Energy,
Sector::BasicMaterials,
Sector::RealEstate,
Sector::Utilities,
]
}
}
impl std::str::FromStr for Sector {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().replace('_', "-").as_str() {
"technology" | "tech" => Ok(Sector::Technology),
"financial-services" | "financials" | "financial" => Ok(Sector::FinancialServices),
"consumer-cyclical" => Ok(Sector::ConsumerCyclical),
"communication-services" | "communication" => Ok(Sector::CommunicationServices),
"healthcare" | "health" => Ok(Sector::Healthcare),
"industrials" | "industrial" => Ok(Sector::Industrials),
"consumer-defensive" => Ok(Sector::ConsumerDefensive),
"energy" => Ok(Sector::Energy),
"basic-materials" | "materials" => Ok(Sector::BasicMaterials),
"real-estate" | "realestate" => Ok(Sector::RealEstate),
"utilities" | "utility" => Ok(Sector::Utilities),
_ => Err(()),
}
}
}
impl std::fmt::Display for Sector {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.display_name())
}
}
impl From<Sector> for String {
fn from(v: Sector) -> Self {
v.display_name().to_string()
}
}