actionqueue_core/ids/
task_id.rs1use std::fmt::{Display, Formatter};
4use std::str::FromStr;
5
6use uuid::Uuid;
7
8#[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 pub fn new() -> Self {
16 TaskId(Uuid::new_v4())
17 }
18
19 pub fn from_uuid(uuid: Uuid) -> Self {
21 TaskId(uuid)
22 }
23
24 pub fn as_uuid(&self) -> &Uuid {
26 &self.0
27 }
28
29 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}