wasmflow_invocation/
invocation.rs

1use 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/// A complete invocation request.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[must_use]
13pub struct Invocation {
14  /// The entity that originated the request.
15  pub origin: Entity,
16  /// The target of the invocation.
17  pub target: Entity,
18  /// The payload.
19  pub payload: TransportMap,
20  /// The invocation id.
21  pub id: Uuid,
22  /// The transaction id, to map together a string of invocations.
23  pub tx_id: Uuid,
24  /// Inherent data associated with the transaction.
25  pub inherent: Option<InherentData>,
26  /// Configuration associated with the invocation.
27  pub config: Option<wasmflow_transport::Serialized>,
28}
29
30impl Invocation {
31  /// Creates an invocation with a new transaction id.
32  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  /// Creates an invocation with a new transaction id.
48  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  /// Creates an invocation with a specific transaction id, to correlate a chain of.
64  /// invocations.
65  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  /// Creates an invocation with a Test origin.
86  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  /// Get the seed associated with an invocation if it exists.
108  #[must_use]
109  pub fn seed(&self) -> Option<u64> {
110    self.inherent.map(|i| i.seed)
111  }
112
113  /// Get the timestamp associated with an invocation if it exists.
114  #[must_use]
115  pub fn timestamp(&self) -> Option<u64> {
116    self.inherent.map(|i| i.timestamp)
117  }
118
119  /// Utility function to get the target [Entity] URL.
120  #[must_use]
121  pub fn target_url(&self) -> String {
122    self.target.url()
123  }
124
125  /// Utility function to get the origin [Entity] URL.
126  #[must_use]
127  pub fn origin_url(&self) -> String {
128    self.origin.url()
129  }
130}
131
132#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
133/// Data inherent to an invocation. Meant to be supplied by a runtime, not a user.
134#[must_use]
135pub struct InherentData {
136  /// The seed to associate with an invocation.
137  pub seed: u64,
138  /// The timestamp to associate with an invocation.
139  pub timestamp: u64,
140}
141
142impl InherentData {
143  /// Constructor for [InherentData]
144  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 {}