selium-atlas-protocol 0.2.3

Selium module for indexing and discovering resources with URIs
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Selium URI parsing and Flatbuffers conversion.
//!
//! Selium uses `sel://` URIs to identify tenants, applications, services, and endpoints.
//!
//! # Examples
//! ```
//! use selium_atlas_protocol::uri::{Uri, UriError};
//!
//! fn main() -> Result<(), UriError> {
//!     let uri = Uri::parse("sel://tenant/app.app/service:ep")?;
//!     assert_eq!(uri.tenant(), "tenant");
//!     assert_eq!(uri.application(), Some("app.app"));
//!     assert_eq!(uri.service(), Some("service"));
//!     assert_eq!(uri.endpoint(), Some("ep"));
//!     Ok(())
//! }
//! ```

use std::hash::{Hash, Hasher};
use std::{fmt, fmt::Write as _};

use flatbuffers::{self, FlatBufferBuilder, WIPOffset};
use thiserror::Error;
use url::{ParseError, Url};

use crate::fbs::atlas::uri as fb;

type Result<T, E = UriError> = std::result::Result<T, E>;

/// Parsed Selium URI.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Uri {
    tenant: String,
    path: Option<Vec<String>>,
    application: Option<String>,
    service: Option<String>,
    endpoint: Option<String>,
}

/// Errors returned when parsing a Selium URI.
#[derive(Error, Debug)]
pub enum UriError {
    /// Parsing the URI string failed.
    #[error("Unable to parse string into URI")]
    BadUri(#[from] ParseError),
    /// The URL scheme was not `sel`.
    #[error("URI must use Selium scheme: \"sel://\"")]
    InvalidScheme,
    /// A tenant component was not present.
    #[error("URI did not contain a tenant")]
    MissingTenant,
    /// The path segments could not be interpreted as a Selium URI.
    #[error("URI path segments invalid")]
    InvalidPath,
}

/// Errors returned when decoding a Flatbuffers `Uri`.
#[derive(Error, Debug)]
pub enum UriFlatbufferError {
    /// Flatbuffers verification failed.
    #[error("Flatbuffer verification failed: {0:?}")]
    Invalid(flatbuffers::InvalidFlatbuffer),
}

impl From<flatbuffers::InvalidFlatbuffer> for UriFlatbufferError {
    fn from(value: flatbuffers::InvalidFlatbuffer) -> Self {
        Self::Invalid(value)
    }
}

impl Uri {
    /// Parse a Selium URI from a string or an existing `Url`.
    pub fn parse<U>(uri: U) -> Result<Self>
    where
        U: TryInto<Url, Error = ParseError>,
    {
        let url = uri.try_into().map_err(UriError::BadUri)?;
        Self::parse_url(url)
    }

    /// Parse a Selium URI from a pre-parsed `Url`.
    pub fn parse_url(uri: Url) -> Result<Self> {
        if uri.scheme() != "sel" {
            return Err(UriError::InvalidScheme);
        }

        let mut this = Self {
            tenant: uri.domain().ok_or(UriError::MissingTenant)?.into(),
            path: None,
            application: None,
            service: None,
            endpoint: None,
        };

        let has_trailing_slash = uri.path().ends_with('/');

        if let Some(segments_iter) = uri.path_segments() {
            let segments: Vec<String> = segments_iter
                .filter(|segment| !segment.is_empty())
                .map(|segment| segment.to_string())
                .collect();

            if segments.is_empty() {
                return Ok(this);
            }

            populate_segments(&segments, has_trailing_slash, &mut this)?;
        }

        Ok(this)
    }

    /// Tenant extracted from the URI.
    pub fn tenant(&self) -> &str {
        &self.tenant
    }

    /// Optional path segments following the tenant.
    pub fn path(&self) -> Option<&[String]> {
        self.path.as_deref()
    }

    /// Optional application name.
    pub fn application(&self) -> Option<&str> {
        self.application.as_deref()
    }

    /// Optional service name.
    pub fn service(&self) -> Option<&str> {
        self.service.as_deref()
    }

    /// Optional endpoint name.
    pub fn endpoint(&self) -> Option<&str> {
        self.endpoint.as_deref()
    }

    /// Tests whether one URI has a broader scope than another.
    ///
    /// Examples:
    /// - `sel://a/b.app/service` contains `sel://a/b.app/service:endpoint`
    /// - `sel://a` contains `sel://a`
    /// - `sel://a/b` does not contain `sel://a`
    pub fn contains(&self, other: &Self) -> bool {
        if self.tenant != other.tenant {
            return false;
        }

        if let Some(self_path) = &self.path {
            let Some(other_path) = &other.path else {
                return false;
            };

            if other_path.len() < self_path.len() {
                return false;
            }

            if !self_path
                .iter()
                .zip(other_path.iter())
                .all(|(self_segment, other_segment)| self_segment == other_segment)
            {
                return false;
            }
        }

        match (&self.application, &other.application) {
            (Some(self_app), Some(other_app)) if self_app != other_app => return false,
            (Some(_), None) => return false,
            _ => {}
        }

        match (&self.service, &other.service) {
            (Some(self_service), Some(other_service)) if self_service != other_service => {
                return false;
            }
            (Some(_), None) => return false,
            _ => {}
        }

        match (&self.endpoint, &other.endpoint) {
            (Some(self_endpoint), Some(other_endpoint)) if self_endpoint != other_endpoint => {
                return false;
            }
            (Some(_), None) => return false,
            _ => {}
        }

        true
    }

    /// Serialises this URI into a Flatbuffer byte vector.
    pub fn to_flatbuffer_bytes(&self) -> Vec<u8> {
        let mut builder = FlatBufferBuilder::new();
        let offset = self.write_flatbuffer(&mut builder);
        builder.finish(offset, None);
        builder.finished_data().to_vec()
    }

    /// Deserialises a URI from a Flatbuffer payload.
    pub fn from_flatbuffer_bytes(bytes: &[u8]) -> Result<Self, UriFlatbufferError> {
        let fb_uri = fb::root_as_uri(bytes)?;
        Ok(Self::from_flatbuffer_table(fb_uri))
    }

    pub(crate) fn write_flatbuffer<'bldr, A: flatbuffers::Allocator + 'bldr>(
        &self,
        builder: &mut FlatBufferBuilder<'bldr, A>,
    ) -> WIPOffset<fb::Uri<'bldr>> {
        let tenant = builder.create_string(&self.tenant);
        let path = self.path.as_ref().map(|segments| {
            let offsets: Vec<_> = segments
                .iter()
                .map(|segment| builder.create_string(segment))
                .collect();
            builder.create_vector(&offsets)
        });
        let application = self
            .application
            .as_ref()
            .map(|value| builder.create_string(value));
        let service = self
            .service
            .as_ref()
            .map(|value| builder.create_string(value));
        let endpoint = self
            .endpoint
            .as_ref()
            .map(|value| builder.create_string(value));

        fb::Uri::create(
            builder,
            &fb::UriArgs {
                tenant: Some(tenant),
                path,
                application,
                service,
                endpoint,
            },
        )
    }

    pub(crate) fn from_flatbuffer_table(table: fb::Uri<'_>) -> Self {
        let path = table.path().map(|vector| {
            vector
                .iter()
                .map(|segment| segment.to_string())
                .collect::<Vec<_>>()
        });

        Self {
            tenant: table.tenant().to_string(),
            path,
            application: table.application().map(|value| value.to_string()),
            service: table.service().map(|value| value.to_string()),
            endpoint: table.endpoint().map(|value| value.to_string()),
        }
    }
}

impl fmt::Display for Uri {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("sel://")?;
        f.write_str(&self.tenant)?;

        if let Some(path) = &self.path {
            for segment in path {
                f.write_char('/')?;
                f.write_str(segment)?;
            }
        }

        if let Some(application) = &self.application {
            f.write_char('/')?;
            f.write_str(application)?;
        }

        if let Some(service) = &self.service {
            f.write_char('/')?;
            f.write_str(service)?;
        }

        if let Some(endpoint) = &self.endpoint {
            f.write_char(':')?;
            f.write_str(endpoint)?;
        }

        Ok(())
    }
}

impl<'a> TryFrom<&'a str> for Uri {
    type Error = UriError;

    fn try_from(value: &'a str) -> std::result::Result<Self, Self::Error> {
        Uri::parse(value)
    }
}

impl TryFrom<Url> for Uri {
    type Error = UriError;

    fn try_from(value: Url) -> std::result::Result<Self, Self::Error> {
        Uri::parse_url(value)
    }
}

impl Hash for Uri {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.to_string().hash(state)
    }
}

fn parse_service_segment(segment: String) -> Result<(String, Option<String>)> {
    if let Some((service, endpoint)) = segment.split_once(':') {
        if service.is_empty() || endpoint.is_empty() {
            return Err(UriError::InvalidPath);
        }
        return Ok((service.to_string(), Some(endpoint.to_string())));
    }

    Ok((segment, None))
}

fn populate_segments(segments: &[String], has_trailing_slash: bool, uri: &mut Uri) -> Result<()> {
    let mut cutoff = segments.len();

    if cutoff == 0 {
        return Ok(());
    }

    // Service (and optional endpoint) exist only when there is no trailing slash and a `.app`
    // segment immediately precedes the tail.
    if !has_trailing_slash && cutoff >= 2 && segments[cutoff - 2].ends_with(".app") {
        let raw_service = segments[cutoff - 1].clone();
        let (service, endpoint) = parse_service_segment(raw_service)?;
        uri.service = Some(service);
        uri.endpoint = endpoint;
        cutoff -= 1;
    }

    if cutoff > 0 && segments[cutoff - 1].ends_with(".app") {
        uri.application = Some(segments[cutoff - 1].clone());
        cutoff -= 1;
    }

    if cutoff > 0 {
        uri.path = Some(segments[..cutoff].to_vec());
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_tenant_only() {
        let uri = Uri::parse("sel://tenant").expect("parse");
        assert_eq!(uri.tenant, "tenant");
        assert!(uri.path.is_none());
        assert!(uri.application.is_none());
        assert!(uri.service.is_none());
        assert!(uri.endpoint.is_none());
    }

    #[test]
    fn parses_application_without_service() {
        let uri = Uri::parse("sel://tenant/app.app/").expect("parse app");
        assert_eq!(uri.tenant, "tenant");
        assert_eq!(uri.application.as_deref(), Some("app.app"));
        assert!(uri.service.is_none());
        assert!(uri.endpoint.is_none());
        assert!(uri.path.is_none());
    }

    #[test]
    fn parses_path_application_service_and_endpoint() {
        let uri = Uri::parse("sel://tenant/team/payments/orders.app/runner:rpc").expect("parse");
        assert_eq!(uri.tenant, "tenant");
        assert_eq!(
            uri.path,
            Some(vec!["team".to_string(), "payments".to_string()])
        );
        assert_eq!(uri.application.as_deref(), Some("orders.app"));
        assert_eq!(uri.service.as_deref(), Some("runner"));
        assert_eq!(uri.endpoint.as_deref(), Some("rpc"));
    }

    #[test]
    fn parses_service_without_endpoint() {
        let uri = Uri::parse("sel://tenant/app.app/service").expect("parse");
        assert_eq!(uri.application.as_deref(), Some("app.app"));
        assert_eq!(uri.service.as_deref(), Some("service"));
        assert!(uri.endpoint.is_none());
    }

    #[test]
    fn treats_segments_without_app_suffix_as_path() {
        let uri = Uri::parse("sel://tenant/team/service").expect("parse path");
        assert_eq!(
            uri.path,
            Some(vec!["team".to_string(), "service".to_string()])
        );
        assert!(uri.application.is_none());
        assert!(uri.service.is_none());
        assert!(uri.endpoint.is_none());
    }

    #[test]
    fn flatbuffer_roundtrip() {
        let uri =
            Uri::parse("sel://tenant/team/payments/orders.app/runner:rpc").expect("parse uri");
        let bytes = uri.to_flatbuffer_bytes();
        let decoded = Uri::from_flatbuffer_bytes(&bytes).expect("decode uri");

        assert_eq!(decoded.tenant(), uri.tenant());
        assert_eq!(decoded.application(), uri.application());
        assert_eq!(decoded.service(), uri.service());
        assert_eq!(decoded.endpoint(), uri.endpoint());
        assert_eq!(
            decoded.path().map(|segments| segments.to_vec()),
            uri.path().map(|segments| segments.to_vec())
        );
    }

    #[test]
    fn contains_endpoint_scope() {
        let broader = Uri::parse("sel://tenant/app.app/service").expect("parse uri");
        let narrower = Uri::parse("sel://tenant/app.app/service:endpoint").expect("parse uri");

        assert!(broader.contains(&narrower));
    }

    #[test]
    fn contains_equal_scope() {
        let uri = Uri::parse("sel://tenant").expect("parse uri");
        assert!(uri.contains(&uri));
    }

    #[test]
    fn contains_path_prefix() {
        let parent = Uri::parse("sel://tenant/team").expect("parse uri");
        let child = Uri::parse("sel://tenant/team/payments/orders.app/runner").expect("parse uri");

        assert!(parent.contains(&child));
    }

    #[test]
    fn does_not_contain_broader_scope() {
        let narrow = Uri::parse("sel://tenant/team").expect("parse uri");
        let broad = Uri::parse("sel://tenant").expect("parse uri");

        assert!(!narrow.contains(&broad));
    }

    #[test]
    fn contains_requires_matching_components() {
        let service_scope = Uri::parse("sel://tenant/app.app/service").expect("parse uri");
        let different_service = Uri::parse("sel://tenant/app.app/other").expect("parse uri");
        assert!(!service_scope.contains(&different_service));

        let endpoint_scope = Uri::parse("sel://tenant/app.app/service:rpc").expect("parse uri");
        let missing_endpoint = Uri::parse("sel://tenant/app.app/service").expect("parse uri");
        assert!(!endpoint_scope.contains(&missing_endpoint));

        let app_scope = Uri::parse("sel://tenant/app.app").expect("parse uri");
        let other_app = Uri::parse("sel://tenant/other.app").expect("parse uri");
        assert!(!app_scope.contains(&other_app));
    }
}