Skip to main content

behest_core/
id.rs

1//! Strongly typed identifiers used across provider APIs.
2
3use std::fmt;
4
5use serde::{Deserialize, Serialize};
6
7/// Stable logical identifier for a provider implementation.
8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct ProviderId(String);
11
12impl ProviderId {
13    /// Creates a provider identifier from any string-like value.
14    #[must_use]
15    pub fn new(value: impl Into<String>) -> Self {
16        Self(value.into())
17    }
18
19    /// Returns the identifier as a string slice.
20    #[must_use]
21    pub fn as_str(&self) -> &str {
22        &self.0
23    }
24
25    /// Returns `true` when the identifier has no characters.
26    #[must_use]
27    pub fn is_empty(&self) -> bool {
28        self.0.is_empty()
29    }
30}
31
32impl AsRef<str> for ProviderId {
33    fn as_ref(&self) -> &str {
34        self.as_str()
35    }
36}
37
38impl From<String> for ProviderId {
39    fn from(value: String) -> Self {
40        Self::new(value)
41    }
42}
43
44impl From<&str> for ProviderId {
45    fn from(value: &str) -> Self {
46        Self::new(value)
47    }
48}
49
50impl fmt::Display for ProviderId {
51    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
52        self.0.fmt(formatter)
53    }
54}
55
56/// Provider-specific model name.
57#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
58#[serde(transparent)]
59pub struct ModelName(String);
60
61impl ModelName {
62    /// Creates a model name from any string-like value.
63    #[must_use]
64    pub fn new(value: impl Into<String>) -> Self {
65        Self(value.into())
66    }
67
68    /// Returns the model name as a string slice.
69    #[must_use]
70    pub fn as_str(&self) -> &str {
71        &self.0
72    }
73
74    /// Returns `true` when the model name has no characters.
75    #[must_use]
76    pub fn is_empty(&self) -> bool {
77        self.0.is_empty()
78    }
79}
80
81impl AsRef<str> for ModelName {
82    fn as_ref(&self) -> &str {
83        self.as_str()
84    }
85}
86
87impl From<String> for ModelName {
88    fn from(value: String) -> Self {
89        Self::new(value)
90    }
91}
92
93impl From<&str> for ModelName {
94    fn from(value: &str) -> Self {
95        Self::new(value)
96    }
97}
98
99impl fmt::Display for ModelName {
100    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
101        self.0.fmt(formatter)
102    }
103}
104
105/// Unique identifier for a single agent run invocation.
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
107pub struct RunId(uuid::Uuid);
108
109impl RunId {
110    /// Creates a new random run identifier.
111    #[must_use]
112    pub fn new() -> Self {
113        Self(uuid::Uuid::new_v4())
114    }
115
116    /// Creates a run identifier from an existing UUID.
117    #[must_use]
118    pub fn from_uuid(uuid: uuid::Uuid) -> Self {
119        Self(uuid)
120    }
121
122    /// Returns the underlying UUID.
123    #[must_use]
124    pub fn as_uuid(&self) -> &uuid::Uuid {
125        &self.0
126    }
127}
128
129impl Default for RunId {
130    fn default() -> Self {
131        Self::new()
132    }
133}
134
135impl fmt::Display for RunId {
136    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137        write!(f, "{}", self.0)
138    }
139}