blueprint_core/extract/
job_call_parts.rs

1use super::{FromJobCall, FromJobCallParts, rejection::*};
2use crate::JobCall;
3use crate::job::call::Parts;
4use crate::metadata::{MetadataMap, MetadataValue};
5use alloc::string::String;
6use bytes::{Bytes, BytesMut};
7use core::convert::Infallible;
8
9impl<Ctx> FromJobCall<Ctx> for JobCall
10where
11    Ctx: Send + Sync,
12{
13    type Rejection = Infallible;
14
15    async fn from_job_call(call: JobCall, _: &Ctx) -> Result<Self, Self::Rejection> {
16        Ok(call)
17    }
18}
19
20impl<Ctx> FromJobCallParts<Ctx> for MetadataMap<MetadataValue>
21where
22    Ctx: Send + Sync,
23{
24    type Rejection = Infallible;
25
26    async fn from_job_call_parts(parts: &mut Parts, _: &Ctx) -> Result<Self, Self::Rejection> {
27        let metadata = parts.metadata.clone();
28
29        Ok(metadata)
30    }
31}
32
33impl<Ctx> FromJobCall<Ctx> for BytesMut
34where
35    Ctx: Send + Sync,
36{
37    type Rejection = Infallible;
38
39    async fn from_job_call(call: JobCall, _: &Ctx) -> Result<Self, Self::Rejection> {
40        let (_, body) = call.into_parts();
41        Ok(body.into())
42    }
43}
44
45impl<Ctx> FromJobCall<Ctx> for Bytes
46where
47    Ctx: Send + Sync,
48{
49    type Rejection = Infallible;
50
51    async fn from_job_call(call: JobCall, _: &Ctx) -> Result<Self, Self::Rejection> {
52        let (_, body) = call.into_parts();
53
54        Ok(body)
55    }
56}
57
58impl<Ctx> FromJobCall<Ctx> for String
59where
60    Ctx: Send + Sync,
61{
62    type Rejection = InvalidUtf8;
63
64    async fn from_job_call(call: JobCall, ctx: &Ctx) -> Result<Self, Self::Rejection> {
65        let bytes = Bytes::from_job_call(call, ctx)
66            .await
67            .map_err(InvalidUtf8::from_err)?;
68
69        let string = String::from_utf8(bytes.into()).map_err(InvalidUtf8::from_err)?;
70
71        Ok(string)
72    }
73}
74
75impl<Ctx> FromJobCallParts<Ctx> for Parts
76where
77    Ctx: Send + Sync,
78{
79    type Rejection = Infallible;
80
81    async fn from_job_call_parts(
82        parts: &mut Parts,
83        _context: &Ctx,
84    ) -> Result<Self, Self::Rejection> {
85        Ok(parts.clone())
86    }
87}