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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
macro_rules! poll_req {
    ($ty:ident => $ret:ty) => {
        impl ::std::future::Future for $ty<'_> {
            type Output = $crate::OsuResult<$ret>;

            fn poll(
                mut self: ::std::pin::Pin<&mut Self>,
                cx: &mut ::std::task::Context<'_>,
            ) -> ::std::task::Poll<Self::Output> {
                match self.fut {
                    Some(ref mut fut) => fut.as_mut().poll(cx),
                    None => {
                        let fut = self.start();

                        self.fut.get_or_insert(fut).as_mut().poll(cx)
                    }
                }
            }
        }
    };
}

mod beatmap;
mod comments;
mod forum;
mod matches;
mod news;
mod ranking;
mod replay;
mod seasonal_backgrounds;
mod user;
mod wiki;

pub use beatmap::*;
pub use comments::*;
pub use forum::*;
pub use matches::*;
pub use news::*;
pub use ranking::*;
pub use replay::*;
pub use seasonal_backgrounds::*;
pub use user::*;
pub use wiki::*;

use crate::{routing::Route, OsuResult};

use hyper::Method;
use std::{
    borrow::Cow,
    fmt::{Display, Formatter, Result, Write},
    future::Future,
    pin::Pin,
};

type Pending<'a, T> = Pin<Box<dyn Future<Output = OsuResult<T>> + Send + Sync + 'a>>;

#[derive(Debug)]
pub(crate) struct Request {
    pub query: Query,
    pub method: Method,
    pub path: Cow<'static, str>,
    pub body: Body,
}

impl Request {
    fn new(route: Route) -> Self {
        Self::with_query_and_body(route, Query::default(), Body::default())
    }

    fn with_query(route: Route, query: Query) -> Self {
        Self::with_query_and_body(route, query, Body::default())
    }

    fn with_body(route: Route, body: Body) -> Self {
        Self::with_query_and_body(route, Query::default(), body)
    }

    fn with_query_and_body(route: Route, query: Query, body: Body) -> Self {
        let (method, path) = route.into_parts();

        Self {
            query,
            method,
            path,
            body,
        }
    }
}

#[derive(Debug, Default)]
pub(crate) struct Body {
    inner: String,
}

impl Body {
    fn push_prefix(&mut self) {
        if self.inner.is_empty() {
            self.inner.push('{');
        } else {
            self.inner.push(',');
        }
    }

    fn push_key(&mut self, key: &str) {
        self.push_prefix();
        self.inner.push('"');
        self.inner.push_str(key);
        self.inner.push_str("\":");
    }

    pub(crate) fn push_with_quotes(&mut self, key: &str, value: impl Display) {
        self.push_key(key);
        let _ = write!(self.inner, r#""{value}""#);
    }

    pub(crate) fn push_without_quotes(&mut self, key: &str, value: impl Display) {
        self.push_key(key);
        let _ = write!(self.inner, "{value}");
    }

    pub(crate) fn into_bytes(mut self) -> Vec<u8> {
        if !self.inner.is_empty() {
            self.inner.push('}');
        }

        self.inner.into_bytes()
    }
}

#[derive(Debug, Default)]
pub(crate) struct Query {
    query: String,
}

impl Query {
    pub(crate) fn new() -> Self {
        Self::default()
    }

    pub(crate) fn push(&mut self, key: &str, value: impl Display) {
        self.query.push_str(key);
        self.query.push('=');
        let _ = write!(self.query, "{}", value);
        self.query.push('&');
    }
}

impl Display for Query {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        if self.query.is_empty() {
            return Ok(());
        }

        f.write_char('?')?;
        f.write_str(&self.query[..self.query.len() - 1])
    }
}