summaryrefslogtreecommitdiffstats
path: root/src/helpers/log.rs
blob: 2b6783ae78b32f9666eba879170a7a1c24255094 (plain)
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
use serde::{Deserialize, Serialize};

/// Serializable form of reqwest's Status type.
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct Status {
    /// The numerical representation of the status
    pub code: u16,
    /// it's canonical reason.
    pub message: Option<&'static str>,
}

impl Status {
    /// New from reqwest's Status type (which is more useful but not
    /// serializable).
    pub fn new(status: reqwest::StatusCode) -> Self {
        Self {
            code: status.as_u16(),
            message: status.canonical_reason(),
        }
    }
}

impl From<&reqwest::Response> for Status {
    fn from(value: &reqwest::Response) -> Self {
        Self::new(value.status())
    }
}

/// Helper for logging request headers
#[derive(Debug)]
pub struct Headers<'h>(pub &'h reqwest::header::HeaderMap);

impl<'h> Serialize for Headers<'h> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.collect_map(
            self.0
                .iter()
                .map(|(k, v)| (format!("{k:?}"), format!("{v:?}"))),
        )
    }
}

impl<'h> From<&'h reqwest::Response> for Headers<'h> {
    fn from(value: &'h reqwest::Response) -> Self {
        Headers(value.headers())
    }
}