thunderstore_api/apis/
mod.rs

1////////////////////////////////////////////////////////////////////////////////
2// This Source Code Form is subject to the terms of the Mozilla Public         /
3// License, v. 2.0. If a copy of the MPL was not distributed with this         /
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.                   /
5////////////////////////////////////////////////////////////////////////////////
6
7use std::error;
8use std::fmt;
9
10#[derive(Debug, Clone)]
11pub struct ResponseContent<T> {
12    pub status: reqwest::StatusCode,
13    pub content: String,
14    pub entity: Option<T>,
15}
16
17#[derive(Debug)]
18pub enum Error<T> {
19    Reqwest(reqwest::Error),
20    Serde(serde_json::Error),
21    Io(std::io::Error),
22    ResponseError(ResponseContent<T>),
23}
24
25impl<T> fmt::Display for Error<T> {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        let (module, e) = match self {
28            Error::Reqwest(e) => ("reqwest", e.to_string()),
29            Error::Serde(e) => ("serde", e.to_string()),
30            Error::Io(e) => ("IO", e.to_string()),
31            Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
32        };
33        write!(f, "error in {}: {}", module, e)
34    }
35}
36
37impl<T: fmt::Debug> error::Error for Error<T> {
38    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
39        Some(match self {
40            Error::Reqwest(e) => e,
41            Error::Serde(e) => e,
42            Error::Io(e) => e,
43            Error::ResponseError(_) => return None,
44        })
45    }
46}
47
48impl<T> From<reqwest::Error> for Error<T> {
49    fn from(e: reqwest::Error) -> Self {
50        Error::Reqwest(e)
51    }
52}
53
54impl<T> From<serde_json::Error> for Error<T> {
55    fn from(e: serde_json::Error) -> Self {
56        Error::Serde(e)
57    }
58}
59
60impl<T> From<std::io::Error> for Error<T> {
61    fn from(e: std::io::Error) -> Self {
62        Error::Io(e)
63    }
64}
65
66pub fn urlencode<T: AsRef<str>>(s: T) -> String {
67    ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
68}
69
70pub mod v1;
71pub mod v2;
72
73pub mod configuration;