wasmflow_invocation/
invocation.rs1use serde::de::DeserializeOwned;
2use serde::{Deserialize, Serialize};
3use uuid::Uuid;
4use wasmflow_entity::Entity;
5use wasmflow_packet::PacketMap;
6use wasmflow_transport::TransportMap;
7
8use crate::error::Error;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12#[must_use]
13pub struct Invocation {
14 pub origin: Entity,
16 pub target: Entity,
18 pub payload: TransportMap,
20 pub id: Uuid,
22 pub tx_id: Uuid,
24 pub inherent: Option<InherentData>,
26 pub config: Option<wasmflow_transport::Serialized>,
28}
29
30impl Invocation {
31 pub fn new(origin: Entity, target: Entity, payload: TransportMap, inherent: Option<InherentData>) -> Invocation {
33 let tx_id = get_uuid();
34 let invocation_id = get_uuid();
35
36 Invocation {
37 origin,
38 target,
39 payload,
40 id: invocation_id,
41 tx_id,
42 inherent,
43 config: None,
44 }
45 }
46
47 pub fn into_v1_parts<C>(self) -> Result<(wasmflow_packet::v1::PacketMap, Option<C>), Error>
49 where
50 C: std::fmt::Debug + DeserializeOwned,
51 {
52 let config = match self.config {
53 Some(v) => Some(
54 v.deserialize()
55 .map_err(|e| Error::IncomingPayload(format!("could not deserialize config: {}", e)))?,
56 ),
57 None => None,
58 };
59
60 Ok((self.payload.into_v1_map(), config))
61 }
62
63 pub fn next(
66 tx_id: Uuid,
67 origin: Entity,
68 target: Entity,
69 payload: TransportMap,
70 inherent: Option<InherentData>,
71 ) -> Invocation {
72 let invocation_id = get_uuid();
73
74 Invocation {
75 origin,
76 target,
77 payload,
78 id: invocation_id,
79 tx_id,
80 inherent,
81 config: None,
82 }
83 }
84
85 pub fn new_test(
87 msg: &str,
88 target: Entity,
89 payload: impl Into<PacketMap>,
90 inherent: Option<InherentData>,
91 ) -> Invocation {
92 let payload = payload.into();
93 let tx_id = get_uuid();
94 let invocation_id = get_uuid();
95
96 Invocation {
97 origin: Entity::test(msg),
98 target,
99 payload: payload.into(),
100 id: invocation_id,
101 tx_id,
102 inherent,
103 config: None,
104 }
105 }
106
107 #[must_use]
109 pub fn seed(&self) -> Option<u64> {
110 self.inherent.map(|i| i.seed)
111 }
112
113 #[must_use]
115 pub fn timestamp(&self) -> Option<u64> {
116 self.inherent.map(|i| i.timestamp)
117 }
118
119 #[must_use]
121 pub fn target_url(&self) -> String {
122 self.target.url()
123 }
124
125 #[must_use]
127 pub fn origin_url(&self) -> String {
128 self.origin.url()
129 }
130}
131
132#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
133#[must_use]
135pub struct InherentData {
136 pub seed: u64,
138 pub timestamp: u64,
140}
141
142impl InherentData {
143 pub fn new(seed: u64, timestamp: u64) -> Self {
145 Self { seed, timestamp }
146 }
147}
148
149pub(crate) fn get_uuid() -> Uuid {
150 Uuid::new_v4()
151}
152
153#[cfg(test)]
154mod tests {}