1use std::convert::Infallible;
2
3use axum_core::{
4 extract::FromRequestParts,
5 response::{IntoResponse, IntoResponseParts, Response, ResponseParts},
6};
7use http::request::Parts;
8
9use crate::{Cookie, CookieJar};
10
11impl<S> FromRequestParts<S> for CookieJar
12where
13 S: Send + Sync,
14{
15 type Rejection = Infallible;
16
17 async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
18 Ok(CookieJar::from_headers(&parts.headers))
19 }
20}
21
22impl IntoResponseParts for CookieJar {
23 type Error = Infallible;
24
25 fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
26 self.write_cookies(res.headers_mut());
27 Ok(res)
28 }
29}
30
31impl IntoResponse for CookieJar {
32 fn into_response(self) -> Response {
33 (self, ()).into_response()
34 }
35}
36
37impl IntoResponseParts for Cookie {
38 type Error = Infallible;
39
40 fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
41 if let Some(cookie) = self
42 .serialize_encoded()
43 .ok()
44 .and_then(|string| string.parse().ok())
45 {
46 res.headers_mut().insert("set-cookie", cookie);
47 }
48 Ok(res)
49 }
50}
51
52impl IntoResponse for Cookie {
53 fn into_response(self) -> Response {
54 (self, ()).into_response()
55 }
56}