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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::fmt::Display;

use serde::{Deserialize, Serialize};

pub mod authenticated;
pub mod game_mechanics;
pub mod guild;
pub mod items;
pub mod maps;
pub mod misc;
pub mod pvp;
pub mod tradingpost;
pub mod wvw;

#[derive(Hash, Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum Language {
    En,
    Fr,
    De,
    Es,
    Zh,
}

impl Display for Language {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        std::fmt::Debug::fmt(self, f)
    }
}

impl Language {
    pub fn as_str(&self) -> &'static str {
        match self {
            Language::En => "en",
            Language::Fr => "fr",
            Language::De => "de",
            Language::Es => "es",
            Language::Zh => "zh",
        }
    }
}

impl From<&str> for Language {
    fn from(v: &str) -> Self {
        match v {
            "en" => Language::En,
            "fr" => Language::Fr,
            "de" => Language::De,
            "es" => Language::Es,
            "zh" => Language::Zh,
            _ => Language::En,
        }
    }
}

pub type TimeStamp = String;

#[derive(Hash, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct ErrorResponse {
    pub text: String,
}

pub trait Endpoint: Sized {
    /// whether this endpoint requires authentication
    const AUTHENTICATED: bool;

    /// whether this endpoint supports the language parameter
    const LOCALE: bool;

    /// endpoint url in the format `v2/account`
    /// ### Remarks
    /// Among other things, this URL is used to fetch ids.
    /// `v2/characters/My Character/core` still requires `v2/characters` to be
    /// set here. For special cases like characters, override the fetch url
    /// of single items here: [EndpointWithId::format_url]
    const URL: &'static str;

    /// version of the endpoint to request
    const VERSION: &'static str;
}

pub trait EndpointWithId: Endpoint {
    type IdType: Display;

    fn format_id(id: &Self::IdType) -> String {
        urlencoding::encode(&id.to_string()).into_owned()
    }

    fn format_url(id: &str) -> String {
        format!("{}/{}", Self::URL, id)
    }
}

pub trait FixedEndpoint: Endpoint {}

pub trait BulkEndpoint: EndpointWithId {
    /// whether this endpoint supports `ids=all`
    const ALL: bool;

    fn id(&self) -> &Self::IdType;
}

pub trait PagedEndpoint: Endpoint {}

impl<T: BulkEndpoint> PagedEndpoint for T {}