-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractors.rs
More file actions
54 lines (43 loc) · 1.58 KB
/
extractors.rs
File metadata and controls
54 lines (43 loc) · 1.58 KB
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
use std::collections::HashMap;
use axum::extract::FromRequestParts;
use axum::http::StatusCode;
use crate::db::DbPool;
use crate::html::utils;
use crate::server::AppState;
/// Extracts browser defaults from the `sp_defaults` cookie. Never rejects.
pub struct BrowserDefaults(pub HashMap<String, String>);
impl FromRequestParts<AppState> for BrowserDefaults {
type Rejection = std::convert::Infallible;
async fn from_request_parts(
parts: &mut axum::http::request::Parts,
_state: &AppState,
) -> Result<Self, Self::Rejection> {
let map =
crate::middleware::cookie::extract_cookie_value(&parts.headers, utils::DEFAULTS_COOKIE)
.map(|v| utils::parse_defaults_cookie(&v))
.unwrap_or_default();
Ok(BrowserDefaults(map))
}
}
/// Axum extractor for HTML handlers -- clones the read pool from state.
pub struct ReadPool(pub DbPool);
impl FromRequestParts<AppState> for ReadPool {
type Rejection = axum::response::Response;
async fn from_request_parts(
_parts: &mut axum::http::request::Parts,
state: &AppState,
) -> Result<Self, Self::Rejection> {
Ok(ReadPool(state.pool.clone()))
}
}
/// Same thing but for API routes -- just returns a status code on error, no HTML.
pub struct ApiReadPool(pub DbPool);
impl FromRequestParts<AppState> for ApiReadPool {
type Rejection = StatusCode;
async fn from_request_parts(
_parts: &mut axum::http::request::Parts,
state: &AppState,
) -> Result<Self, Self::Rejection> {
Ok(ApiReadPool(state.pool.clone()))
}
}