Skip to main content

obzenflow_core/id/
journal_id.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// SPDX-FileCopyrightText: 2025-2026 ObzenFlow Contributors
3// https://obzenflow.dev
4
5//! Journal identifier type
6
7use serde::{Deserialize, Serialize};
8use std::fmt;
9use ulid::Ulid;
10
11/// Identifies a specific journal instance
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
13pub struct JournalId(Ulid);
14
15impl JournalId {
16    /// Create a new JournalId
17    pub fn new() -> Self {
18        JournalId(Ulid::new())
19    }
20
21    /// Create from an existing Ulid
22    pub fn from_ulid(ulid: Ulid) -> Self {
23        JournalId(ulid)
24    }
25
26    /// Get the inner ULID
27    pub fn as_ulid(&self) -> &Ulid {
28        &self.0
29    }
30
31    /// Convert to Ulid
32    pub fn into_ulid(self) -> Ulid {
33        self.0
34    }
35}
36
37impl Default for JournalId {
38    fn default() -> Self {
39        Self::new()
40    }
41}
42
43impl fmt::Display for JournalId {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        write!(f, "journal_{}", self.0)
46    }
47}
48
49impl From<Ulid> for JournalId {
50    fn from(ulid: Ulid) -> Self {
51        JournalId(ulid)
52    }
53}
54
55impl From<JournalId> for Ulid {
56    fn from(id: JournalId) -> Self {
57        id.0
58    }
59}
60
61impl AsRef<Ulid> for JournalId {
62    fn as_ref(&self) -> &Ulid {
63        &self.0
64    }
65}