speedrun_api/
auth.rs

1use std::fmt::Debug;
2
3use http::{HeaderMap, HeaderValue};
4use thiserror::Error;
5
6/// Authentication error
7#[derive(Debug, Error)]
8#[non_exhaustive]
9pub enum AuthError {
10    /// Invalid header value
11    #[error("Invalid header value: {0}")]
12    HeaderValue(#[from] http::header::InvalidHeaderValue),
13}
14
15#[derive(Clone)]
16pub(crate) struct Auth {
17    pub(crate) token: Option<String>,
18}
19
20impl Auth {
21    pub fn set_auth_header<'a>(
22        &self,
23        headers: &'a mut HeaderMap<HeaderValue>,
24    ) -> Result<&'a mut HeaderMap<HeaderValue>, AuthError> {
25        if let Some(ref api_key) = self.token {
26            let mut val = HeaderValue::from_str(api_key)?;
27            val.set_sensitive(true);
28            headers.insert("X-API-Key", val);
29        }
30
31        Ok(headers)
32    }
33}
34
35impl Debug for Auth {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        if self.token.is_some() {
38            write!(f, "Yes")
39        } else {
40            write!(f, "No")
41        }
42    }
43}