1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
pub mod annotated;
pub mod maybe_error;
pub type LeaseId = i64;
/// Default namespace if user does not provide one
const DEFAULT_NAMESPACE: &str = "NS";
const DEFAULT_COMPONENT: &str = "C";
const DEFAULT_ENDPOINT: &str = "E";
/// How we identify a namespace/component/endpoint URL.
/// Technically the '://' is not part of the scheme but it eliminates several string
/// concatenations.
pub const ENDPOINT_SCHEME: &str = "dyn://";
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct Component {
pub name: String,
pub namespace: String,
}
/// Represents an endpoint with a namespace, component, and name.
///
/// An [EndpointId] is defined by a three-part string separated by `/` or a '.':
/// - **namespace**
/// - **component**
/// - **name**
///
/// Example format: `"namespace/component/endpoint"`
///
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
pub struct EndpointId {
pub namespace: String,
pub component: String,
pub name: String,
}
impl fmt::Display for EndpointId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}/{}", self.namespace, self.component, self.name)
}
}
impl PartialEq<Vec<&str>> for EndpointId {
fn eq(&self, other: &Vec<&str>) -> bool {
if other.len() != 3 {
return false;
}
self.namespace == other[0] && self.component == other[1] && self.name == other[2]
}
}
impl PartialEq<[&str; 3]> for EndpointId {
fn eq(&self, other: &[&str; 3]) -> bool {
self.namespace == other[0] && self.component == other[1] && self.name == other[2]
}
}
impl PartialEq<EndpointId> for [&str; 3] {
fn eq(&self, other: &EndpointId) -> bool {
other == self
}
}
impl PartialEq<EndpointId> for Vec<&str> {
fn eq(&self, other: &EndpointId) -> bool {
other == self
}
}
impl Default for EndpointId {
fn default() -> Self {
EndpointId {
namespace: DEFAULT_NAMESPACE.to_string(),
component: DEFAULT_COMPONENT.to_string(),
name: DEFAULT_ENDPOINT.to_string(),
}
}
}
impl From<&str> for EndpointId {
/// Creates an [EndpointId] from a string.
///
/// # Arguments
/// - `path`: A string in the format `"namespace/component/endpoint"`.
///
/// The first two parts become the first two elements of the vector.
/// The third and subsequent parts are joined with '_' and become the third element.
/// Default values are used for missing parts.
///
/// # Examples:
/// - "component" -> ["DEFAULT_NAMESPACE", "component", "DEFAULT_ENDPOINT"]
/// - "namespace.component" -> ["namespace", "component", "DEFAULT_ENDPOINT"]
/// - "namespace.component.endpoint" -> ["namespace", "component", "endpoint"]
/// - "namespace/component" -> ["namespace", "component", "DEFAULT_ENDPOINT"]
/// - "namespace.component.endpoint.other.parts" -> ["namespace", "component", "endpoint_other_parts"]
///
/// # Examples
/// ```
/// use dynamo_runtime::protocols::EndpointId;
///
/// let endpoint = EndpointId::from("namespace/component/endpoint");
/// assert_eq!(endpoint.namespace, "namespace");
/// assert_eq!(endpoint.component, "component");
/// assert_eq!(endpoint.name, "endpoint");
/// ```
fn from(s: &str) -> Self {
let input = s.strip_prefix(ENDPOINT_SCHEME).unwrap_or(s);
// Split the input string on either '.' or '/'
let mut parts = input
.trim_matches([' ', '/', '.'])
.split(['.', '/'])
.filter(|x| !x.is_empty());
// Extract the first three potential components.
let p1 = parts.next();
let p2 = parts.next();
let p3 = parts.next();
let namespace;
let component;
let name;
match (p1, p2, p3) {
(None, _, _) => {
// 0 elements: all fields remain empty.
// Should this be an error?
namespace = DEFAULT_NAMESPACE.to_string();
component = DEFAULT_COMPONENT.to_string();
name = DEFAULT_ENDPOINT.to_string();
}
(Some(c), None, _) => {
namespace = DEFAULT_NAMESPACE.to_string();
component = c.to_string();
name = DEFAULT_ENDPOINT.to_string();
}
(Some(ns), Some(c), None) => {
// 2 elements: namespace, component
namespace = ns.to_string();
component = c.to_string();
name = DEFAULT_ENDPOINT.to_string();
}
(Some(ns), Some(c), Some(ep)) => {
namespace = ns.to_string();
component = c.to_string();
// For the 'name' field, we need to handle 'n' and any remaining parts.
// Instead of collecting into a Vec and then joining, we can build the string directly.
let mut endpoint_buf = String::from(ep); // Start with the third part
for part in parts {
// 'parts' iterator continues from where p3 left off
endpoint_buf.push('_');
endpoint_buf.push_str(part);
}
name = endpoint_buf;
}
}
EndpointId {
namespace,
component,
name,
}
}
}
impl FromStr for EndpointId {
type Err = core::convert::Infallible;
/// Parses an `EndpointId` from a string using the standard Rust `.parse::<T>()` pattern.
///
/// This is implemented in terms of [`From<&str>`].
///
/// # Errors
/// Does not fail
///
/// # Examples
/// ```
/// use std::str::FromStr;
/// use dynamo_runtime::protocols::EndpointId;
///
/// let endpoint: EndpointId = "namespace/component/endpoint".parse().unwrap();
/// assert_eq!(endpoint.namespace, "namespace");
/// assert_eq!(endpoint.component, "component");
/// assert_eq!(endpoint.name, "endpoint");
/// let endpoint: EndpointId = "dyn://namespace/component/endpoint".parse().unwrap();
/// // same as above
/// assert_eq!(endpoint.name, "endpoint");
/// ```
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(EndpointId::from(s))
}
}
impl EndpointId {
/// As a String like dyn://dynamo.internal.worker
pub fn as_url(&self) -> String {
format!(
"{ENDPOINT_SCHEME}{}.{}.{}",
self.namespace, self.component, self.name
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::convert::TryFrom;
use std::str::FromStr;
#[test]
fn test_valid_endpoint_from() {
let input = "namespace1/component1/endpoint1";
let endpoint = EndpointId::from(input);
assert_eq!(endpoint.namespace, "namespace1");
assert_eq!(endpoint.component, "component1");
assert_eq!(endpoint.name, "endpoint1");
}
#[test]
fn test_valid_endpoint_from_str() {
let input = "namespace2/component2/endpoint2";
let endpoint = EndpointId::from_str(input).unwrap();
assert_eq!(endpoint.namespace, "namespace2");
assert_eq!(endpoint.component, "component2");
assert_eq!(endpoint.name, "endpoint2");
}
#[test]
fn test_valid_endpoint_parse() {
let input = "namespace3/component3/endpoint3";
let endpoint: EndpointId = input.parse().unwrap();
assert_eq!(endpoint.namespace, "namespace3");
assert_eq!(endpoint.component, "component3");
assert_eq!(endpoint.name, "endpoint3");
}
#[test]
fn test_endpoint_from() {
let result = EndpointId::from("component");
assert_eq!(
result,
vec![DEFAULT_NAMESPACE, "component", DEFAULT_ENDPOINT]
);
}
#[test]
fn test_namespace_component_endpoint() {
let result = EndpointId::from("namespace.component.endpoint");
assert_eq!(result, vec!["namespace", "component", "endpoint"]);
}
#[test]
fn test_forward_slash_separator() {
let result = EndpointId::from("namespace/component");
assert_eq!(result, vec!["namespace", "component", DEFAULT_ENDPOINT]);
}
#[test]
fn test_multiple_parts() {
let result = EndpointId::from("namespace.component.endpoint.other.parts");
assert_eq!(
result,
vec!["namespace", "component", "endpoint_other_parts"]
);
}
#[test]
fn test_mixed_separators() {
// Do it the .into way for variety and documentation
let result: EndpointId = "namespace/component.endpoint".into();
assert_eq!(result, vec!["namespace", "component", "endpoint"]);
}
#[test]
fn test_empty_string() {
let result = EndpointId::from("");
assert_eq!(
result,
vec![DEFAULT_NAMESPACE, DEFAULT_COMPONENT, DEFAULT_ENDPOINT]
);
// White space is equivalent to an empty string
let result = EndpointId::from(" ");
assert_eq!(
result,
vec![DEFAULT_NAMESPACE, DEFAULT_COMPONENT, DEFAULT_ENDPOINT]
);
}
#[test]
fn test_parse_with_scheme_and_url_roundtrip() {
let input = "dyn://ns/cp/ep";
let endpoint: EndpointId = input.parse().unwrap();
assert_eq!(endpoint, vec!["ns", "cp", "ep"]);
assert_eq!(endpoint.as_url(), "dyn://ns.cp.ep");
}
}