Skip to main content

actionqueue_core/ids/
attempt_id.rs

1//! Per-attempt identifier for execution lineage tracking.
2
3use std::fmt::{Display, Formatter};
4use std::str::FromStr;
5
6use uuid::Uuid;
7
8/// A unique identifier for an attempt within a run.
9///
10/// Each run may have multiple attempts (for retries). The AttemptId
11/// uniquely identifies each attempt.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct AttemptId(Uuid);
15
16impl AttemptId {
17    /// Creates a new random AttemptId.
18    pub fn new() -> Self {
19        AttemptId(Uuid::new_v4())
20    }
21
22    /// Creates an AttemptId from a UUID.
23    pub fn from_uuid(uuid: Uuid) -> Self {
24        AttemptId(uuid)
25    }
26
27    /// Returns the inner UUID.
28    pub fn as_uuid(&self) -> &Uuid {
29        &self.0
30    }
31}
32
33impl Default for AttemptId {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39impl FromStr for AttemptId {
40    type Err = uuid::Error;
41
42    fn from_str(s: &str) -> Result<Self, Self::Err> {
43        Uuid::from_str(s).map(AttemptId)
44    }
45}
46
47impl Display for AttemptId {
48    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49        write!(f, "{}", self.0)
50    }
51}