Skip to main content

ferrum_types/
ids.rs

1//! Identifier types for Ferrum entities
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5use uuid::Uuid;
6
7/// Token identifier used across the inference pipeline.
8///
9/// This is a thin newtype wrapper around `u32` so that we retain the ergonomic
10/// `.0` access pattern while being able to provide convenience constructors the
11/// rest of the refactor relies on (e.g. `TokenId::new`, `TokenId::MAX`).
12#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
13pub struct TokenId(pub u32);
14
15impl TokenId {
16    pub const MAX: TokenId = TokenId(u32::MAX);
17
18    pub const fn new(value: u32) -> Self {
19        Self(value)
20    }
21
22    pub const fn get(self) -> u32 {
23        self.0
24    }
25}
26
27impl From<u32> for TokenId {
28    fn from(value: u32) -> Self {
29        TokenId(value)
30    }
31}
32
33impl From<TokenId> for u32 {
34    fn from(value: TokenId) -> Self {
35        value.0
36    }
37}
38
39impl From<usize> for TokenId {
40    fn from(value: usize) -> Self {
41        TokenId(value as u32)
42    }
43}
44
45impl From<TokenId> for usize {
46    fn from(value: TokenId) -> Self {
47        value.0 as usize
48    }
49}
50
51impl fmt::Display for TokenId {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        write!(f, "{}", self.0)
54    }
55}
56
57/// Request identifier
58#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
59pub struct RequestId(pub Uuid);
60
61impl RequestId {
62    pub fn new() -> Self {
63        Self(Uuid::new_v4())
64    }
65}
66
67impl Default for RequestId {
68    fn default() -> Self {
69        Self::new()
70    }
71}
72
73impl fmt::Display for RequestId {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        write!(f, "{}", self.0)
76    }
77}
78
79/// Batch identifier
80#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
81pub struct BatchId(pub Uuid);
82
83impl BatchId {
84    pub fn new() -> Self {
85        Self(Uuid::new_v4())
86    }
87}
88
89impl Default for BatchId {
90    fn default() -> Self {
91        Self::new()
92    }
93}
94
95impl fmt::Display for BatchId {
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        write!(f, "{}", self.0)
98    }
99}
100
101/// Model identifier
102#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
103pub struct ModelId(pub String);
104
105impl ModelId {
106    pub fn new(id: impl Into<String>) -> Self {
107        Self(id.into())
108    }
109
110    pub fn as_str(&self) -> &str {
111        &self.0
112    }
113}
114
115impl fmt::Display for ModelId {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        write!(f, "{}", self.0)
118    }
119}
120
121impl From<String> for ModelId {
122    fn from(id: String) -> Self {
123        Self(id)
124    }
125}
126
127impl From<&str> for ModelId {
128    fn from(id: &str) -> Self {
129        Self(id.to_string())
130    }
131}
132
133/// Session identifier for stateful interactions
134#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
135pub struct SessionId(pub Uuid);
136
137impl SessionId {
138    pub fn new() -> Self {
139        Self(Uuid::new_v4())
140    }
141}
142
143impl Default for SessionId {
144    fn default() -> Self {
145        Self::new()
146    }
147}
148
149/// Task identifier for execution tasks
150#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
151pub struct TaskId(pub Uuid);
152
153impl TaskId {
154    pub fn new() -> Self {
155        Self(Uuid::new_v4())
156    }
157}
158
159impl Default for TaskId {
160    fn default() -> Self {
161        Self::new()
162    }
163}
164
165/// Client identifier for multi-tenancy
166#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
167pub struct ClientId(pub String);
168
169impl ClientId {
170    pub fn new(id: impl Into<String>) -> Self {
171        Self(id.into())
172    }
173
174    pub fn as_str(&self) -> &str {
175        &self.0
176    }
177}
178
179impl fmt::Display for ClientId {
180    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181        write!(f, "{}", self.0)
182    }
183}
184
185impl From<String> for ClientId {
186    fn from(id: String) -> Self {
187        Self(id)
188    }
189}
190
191impl From<&str> for ClientId {
192    fn from(id: &str) -> Self {
193        Self(id.to_string())
194    }
195}