Skip to main content

actionqueue_core/ids/
task_id.rs

1//! Unique identifier for a durable task specification.
2
3use std::fmt::{Display, Formatter};
4use std::str::FromStr;
5
6use uuid::Uuid;
7
8/// A unique identifier for a task definition.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct TaskId(Uuid);
12
13impl TaskId {
14    /// Creates a new random TaskId.
15    pub fn new() -> Self {
16        TaskId(Uuid::new_v4())
17    }
18
19    /// Creates a TaskId from a UUID.
20    pub fn from_uuid(uuid: Uuid) -> Self {
21        TaskId(uuid)
22    }
23
24    /// Returns the inner UUID.
25    pub fn as_uuid(&self) -> &Uuid {
26        &self.0
27    }
28
29    /// Returns whether this identifier is the nil UUID.
30    pub fn is_nil(&self) -> bool {
31        self.0.is_nil()
32    }
33}
34
35impl Default for TaskId {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl FromStr for TaskId {
42    type Err = uuid::Error;
43
44    fn from_str(s: &str) -> Result<Self, Self::Err> {
45        Uuid::from_str(s).map(TaskId)
46    }
47}
48
49impl Display for TaskId {
50    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
51        write!(f, "{}", self.0)
52    }
53}