Skip to main content

temporalio_common/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(pub Vec<u8>);
21
22impl TaskToken {
23    /// Task tokens for local activities are always prefixed with a special sigil so they can
24    /// be identified easily
25    pub fn new_local_activity_token(unique_data: impl IntoIterator<Item = u8>) -> Self {
26        let mut bytes = LOCAL_ACT_TASK_TOKEN_PREFIX.to_vec();
27        bytes.extend(unique_data);
28        TaskToken(bytes)
29    }
30
31    /// Returns true if the task token is for a local activity
32    pub fn is_local_activity_task(&self) -> bool {
33        self.0.starts_with(LOCAL_ACT_TASK_TOKEN_PREFIX)
34    }
35}
36
37impl Display for TaskToken {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        f.write_str(&format_task_token(&self.0))
40    }
41}
42
43impl Debug for TaskToken {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        f.write_str(&format!("TaskToken({})", format_task_token(&self.0)))
46    }
47}
48
49impl Borrow<[u8]> for TaskToken {
50    fn borrow(&self) -> &[u8] {
51        self.0.as_slice()
52    }
53}
54
55pub(crate) fn format_task_token(tt: &[u8]) -> String {
56    BASE64_STANDARD.encode(tt)
57}