Skip to main content

temporalio_protos/protos/
task_token.rs

1use base64::{Engine, prelude::BASE64_STANDARD};
2use std::{
3    borrow::Borrow,
4    fmt::{Debug, Display, Formatter},
5};
6
7static LOCAL_ACT_TASK_TOKEN_PREFIX: &[u8] = b"local_act_";
8
9#[derive(
10    Hash,
11    Eq,
12    PartialEq,
13    Clone,
14    derive_more::From,
15    derive_more::Into,
16    serde::Serialize,
17    serde::Deserialize,
18)]
19/// Type-safe wrapper for task token bytes
20pub struct TaskToken(Vec<u8>);
21
22impl TaskToken {
23    /// Consumes this token and returns its underlying bytes.
24    pub fn into_inner(self) -> Vec<u8> {
25        self.0
26    }
27
28    /// Task tokens for local activities are always prefixed with a special sigil so they can
29    /// be identified easily
30    pub fn new_local_activity_token(unique_data: impl IntoIterator<Item = u8>) -> Self {
31        let mut bytes = LOCAL_ACT_TASK_TOKEN_PREFIX.to_vec();
32        bytes.extend(unique_data);
33        TaskToken(bytes)
34    }
35
36    /// Returns true if the task token is for a local activity
37    pub fn is_local_activity_task(&self) -> bool {
38        self.0.starts_with(LOCAL_ACT_TASK_TOKEN_PREFIX)
39    }
40}
41
42impl Display for TaskToken {
43    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44        f.write_str(&format_task_token(&self.0))
45    }
46}
47
48impl Debug for TaskToken {
49    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50        f.write_str(&format!("TaskToken({})", format_task_token(&self.0)))
51    }
52}
53
54impl Borrow<[u8]> for TaskToken {
55    fn borrow(&self) -> &[u8] {
56        self.0.as_slice()
57    }
58}
59
60pub(crate) fn format_task_token(tt: &[u8]) -> String {
61    BASE64_STANDARD.encode(tt)
62}