blueprint_tangle_extra/extract/
call_id.rs1use blueprint_core::{
2 __composite_rejection as composite_rejection, __define_rejection as define_rejection,
3};
4use blueprint_core::{FromJobCallParts, job::call::Parts as JobCallParts};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct CallId(pub u64);
9
10impl CallId {
11 pub const METADATA_KEY: &'static str = "X-TANGLE-CALL-ID";
12}
13
14blueprint_core::__impl_deref!(CallId: u64);
15blueprint_core::__impl_from!(u64, CallId);
16
17define_rejection! {
18 #[body = "No CallId found in the metadata"]
19 pub struct MissingCallId;
21}
22
23define_rejection! {
24 #[body = "The call id in the metadata is not a valid integer"]
25 pub struct InvalidCallId;
27}
28
29composite_rejection! {
30 pub enum CallIdRejection {
35 MissingCallId,
36 InvalidCallId,
37 }
38}
39
40impl TryFrom<&mut JobCallParts> for CallId {
41 type Error = CallIdRejection;
42
43 fn try_from(parts: &mut JobCallParts) -> Result<Self, Self::Error> {
44 let call_id_raw = parts
45 .metadata
46 .get(Self::METADATA_KEY)
47 .ok_or(MissingCallId)?;
48 let call_id = call_id_raw.try_into().map_err(|_| InvalidCallId)?;
49 Ok(CallId(call_id))
50 }
51}
52
53impl<Ctx> FromJobCallParts<Ctx> for CallId
54where
55 Ctx: Send + Sync,
56{
57 type Rejection = CallIdRejection;
58
59 async fn from_job_call_parts(
60 parts: &mut JobCallParts,
61 _: &Ctx,
62 ) -> Result<Self, Self::Rejection> {
63 CallId::try_from(parts)
64 }
65}