riot_api/models/routing/
region_routing.rs

1use std::fmt::{Display, Formatter};
2use serde::Deserialize;
3use crate::models::client::{
4    ConversionError, configuration::Routable
5};
6
7#[derive(Debug, Deserialize, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
8pub enum RegionRouting {
9    AMERICAS,
10    ASIA,
11    EUROPE,
12    SEA,
13}
14
15impl Routable for RegionRouting {
16    fn base_url(&self) -> String {
17        format!("https://{}.api.riotgames.com", self)
18    }
19}
20
21impl TryFrom<&str> for RegionRouting {
22    type Error = ConversionError;
23
24    fn try_from(value: &str) -> Result<Self, Self::Error> {
25        match value.to_lowercase().as_str() {
26            "americas" => Ok(Self::AMERICAS),
27            "asia"     => Ok(Self::ASIA),
28            "europe"   => Ok(Self::EUROPE),
29            "sea"      => Ok(Self::SEA),
30
31            _ => Err(Self::Error::InvalidStringError),
32        }
33    }
34}
35impl TryFrom<String> for RegionRouting {
36    type Error = ConversionError;
37
38    fn try_from(value: String) -> Result<Self, Self::Error> {
39        value.as_str().try_into()
40    }
41}
42
43impl<'l> Into<&'l str> for RegionRouting {
44    fn into(self) -> &'l str {
45        match self {
46            Self::AMERICAS => "AMERICAS",
47            Self::ASIA     => "ASIA",
48            Self::EUROPE   => "EUROPE",
49            Self::SEA      => "SEA",
50        }
51    }
52}
53impl Into<String> for RegionRouting {
54    fn into(self) -> String {
55        match self {
56            Self::AMERICAS => "AMERICAS",
57            Self::ASIA     => "ASIA",
58            Self::EUROPE   => "EUROPE",
59            Self::SEA      => "SEA",
60        }.to_string()
61    }
62}
63
64impl Display for RegionRouting {
65    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
66        write!(f, "{}", Into::<String>::into(*self))
67    }
68}