Skip to main content

actionqueue_core/ids/
run_id.rs

1//! Stable run identity and idempotency key for external side effects.
2
3use std::fmt::{Display, Formatter};
4use std::str::FromStr;
5
6use uuid::Uuid;
7
8/// Unique identifier for a run instance.
9///
10/// `RunId` derives `Ord`/`PartialOrd` because the engine's selection algorithm
11/// uses RunId ordering as a deterministic tie-breaker when priority and creation
12/// time are equal. `TaskId` and `AttemptId` do not need ordering semantics.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub struct RunId(Uuid);
16
17impl RunId {
18    /// Creates a new random RunId.
19    pub fn new() -> Self {
20        RunId(Uuid::new_v4())
21    }
22
23    /// Creates a RunId from a UUID.
24    pub fn from_uuid(uuid: Uuid) -> Self {
25        RunId(uuid)
26    }
27
28    /// Returns the inner UUID.
29    pub fn as_uuid(&self) -> &Uuid {
30        &self.0
31    }
32}
33
34impl Default for RunId {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl FromStr for RunId {
41    type Err = uuid::Error;
42
43    fn from_str(s: &str) -> Result<Self, Self::Err> {
44        Uuid::from_str(s).map(RunId)
45    }
46}
47
48impl Display for RunId {
49    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50        write!(f, "{}", self.0)
51    }
52}
53
54// The `Ord` and `PartialOrd` traits are derived automatically from the `Ord` derive on the struct,
55// which uses the underlying `Uuid`'s byte-level ordering for deterministic comparison.