netbat 0.8.2

Thin sync-first server/network boundary exposure layer for syncbat.
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use std::error::Error;
use std::fmt;

use crate::transport::CALL_VERB;

/// Stable crate-layer rule for docs, diagnostics, and tests.
pub const LAYER_RULE: &str = "nb exposes, sb dispatches, bp records";

/// Maximum bytes accepted for a boundary route path.
pub const MAX_ROUTE_PATH_BYTES: usize = 512;

/// Boundary route validation failure.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum RouteValidationError {
    /// Operation name failed boundary validation.
    InvalidOperationName {
        /// Invalid operation name.
        name: String,
        /// Stable validation message.
        message: &'static str,
    },
    /// Boundary path failed validation.
    InvalidPath {
        /// Invalid boundary path.
        path: String,
        /// Stable validation message.
        message: &'static str,
    },
    /// Boundary method label failed validation.
    InvalidMethod {
        /// Invalid method label.
        method: String,
        /// Stable validation message.
        message: &'static str,
    },
    /// Wrapped syncbat module descriptor failed validation.
    InvalidModule(syncbat::RegisterValidationError),
    /// Two mounted routes would expose the same method/path pair.
    DuplicateRoute {
        /// Boundary method label.
        method: &'static str,
        /// Boundary path.
        path: String,
    },
}

impl fmt::Display for RouteValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidOperationName { name, message } => {
                write!(
                    f,
                    "operation name `{name}` is invalid for a route: {message}"
                )
            }
            Self::InvalidPath { path, message } => {
                write!(f, "route path `{path}` is invalid: {message}")
            }
            Self::InvalidMethod { method, message } => {
                write!(f, "route method `{method}` is invalid: {message}")
            }
            Self::InvalidModule(error) => write!(f, "module is invalid for exposure: {error}"),
            Self::DuplicateRoute { method, path } => {
                write!(f, "duplicate boundary route {method} {path}")
            }
        }
    }
}

impl Error for RouteValidationError {}

impl From<syncbat::RegisterValidationError> for RouteValidationError {
    fn from(error: syncbat::RegisterValidationError) -> Self {
        Self::InvalidModule(error)
    }
}

/// A syncbat operation exposed at a server/network boundary.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Endpoint {
    operation_name: String,
    path: String,
}

impl Endpoint {
    /// Create an endpoint for an operation and boundary path.
    ///
    /// # Errors
    /// Returns [`RouteValidationError`] when the operation name or path is not
    /// valid for a server boundary route.
    pub fn new(
        operation_name: impl Into<String>,
        path: impl Into<String>,
    ) -> Result<Self, RouteValidationError> {
        let operation_name = operation_name.into();
        let path = path.into();
        validate_route_operation_name(&operation_name)?;
        validate_route_path(&path)?;
        Ok(Self {
            operation_name,
            path,
        })
    }

    /// Stable syncbat operation name exposed by this endpoint.
    #[must_use]
    pub fn operation_name(&self) -> &str {
        &self.operation_name
    }

    /// Boundary path associated with this endpoint.
    #[must_use]
    pub fn path(&self) -> &str {
        &self.path
    }
}

/// A mounted boundary route.
///
/// A route maps boundary metadata to a syncbat operation name. It is not a
/// dispatcher and carries no transport server implementation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Route {
    method: &'static str,
    endpoint: Endpoint,
}

impl Route {
    /// Create a route with a stable method label and endpoint.
    ///
    /// # Errors
    /// Returns [`RouteValidationError`] when the method label is not valid for
    /// a stable boundary route.
    pub fn new(method: &'static str, endpoint: Endpoint) -> Result<Self, RouteValidationError> {
        validate_route_method(method)?;
        Ok(Self { method, endpoint })
    }

    /// Stable method label for the boundary route.
    #[must_use]
    pub fn method(&self) -> &'static str {
        self.method
    }

    /// Endpoint exposed by this route.
    #[must_use]
    pub fn endpoint(&self) -> &Endpoint {
        &self.endpoint
    }

    /// Stable syncbat operation name exposed by this route.
    #[must_use]
    pub fn operation_name(&self) -> &str {
        self.endpoint.operation_name()
    }

    /// Boundary path associated with this route.
    #[must_use]
    pub fn path(&self) -> &str {
        self.endpoint.path()
    }
}

/// Server-facing wrapper for a data-oriented syncbat module.
///
/// `ServerModule` owns the syncbat module descriptor so it can be mounted into
/// a [`syncbat::CoreBuilder`] later by the caller. It only derives route
/// metadata from operation descriptors.
pub struct ServerModule {
    module: syncbat::Module,
    routes: Vec<Route>,
}

impl ServerModule {
    /// Wrap a syncbat module and expose each operation under `base_path`.
    ///
    /// Paths are formed as `{base_path}/{operation_name}` with a single slash
    /// between the base and the operation name.
    ///
    /// # Errors
    /// Returns [`RouteValidationError`] when the module descriptor or derived
    /// route metadata fails boundary validation.
    pub fn expose(
        module: syncbat::Module,
        base_path: impl AsRef<str>,
    ) -> Result<Self, RouteValidationError> {
        module.validate()?;
        let base_path = normalize_base_path(base_path.as_ref());
        validate_base_path(&base_path)?;
        let mut routes = Vec::with_capacity(module.operation_count());
        for (name, _) in module.operations() {
            let endpoint = Endpoint::new(name, format!("{base_path}/{name}"))?;
            routes.push(Route::new(CALL_VERB, endpoint)?);
        }

        Ok(Self { module, routes })
    }

    /// Wrapped syncbat module descriptor.
    #[must_use]
    pub fn module(&self) -> &syncbat::Module {
        &self.module
    }

    /// Stable module name.
    #[must_use]
    pub fn name(&self) -> &str {
        self.module.name()
    }

    /// Exposed routes derived from the module operation descriptors.
    #[must_use]
    pub fn routes(&self) -> &[Route] {
        &self.routes
    }

    /// Number of exposed operations.
    #[must_use]
    pub fn operation_count(&self) -> usize {
        self.module.operation_count()
    }

    /// Consume the wrapper and return the syncbat module descriptor.
    #[must_use]
    pub fn into_module(self) -> syncbat::Module {
        self.module
    }
}

/// Minimal server-boundary registry.
///
/// `Server` stores exposed modules and route metadata. Transport helpers in
/// this crate dispatch only by calling [`syncbat::Core`] APIs; the server
/// registry itself stays metadata-only.
#[derive(Default)]
pub struct Server {
    modules: Vec<ServerModule>,
}

impl Server {
    /// Create an empty server-boundary registry.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Mount server-facing module metadata.
    ///
    /// # Errors
    /// Returns [`RouteValidationError::DuplicateRoute`] if a mounted module
    /// already exposes the same method/path pair.
    pub fn mount(&mut self, module: ServerModule) -> Result<&mut Self, RouteValidationError> {
        for route in module.routes() {
            if self.routes().any(|existing| {
                existing.method() == route.method() && existing.path() == route.path()
            }) {
                return Err(RouteValidationError::DuplicateRoute {
                    method: route.method(),
                    path: route.path().to_owned(),
                });
            }
        }
        self.modules.push(module);
        Ok(self)
    }

    /// Mounted server-facing modules.
    #[must_use]
    pub fn modules(&self) -> &[ServerModule] {
        &self.modules
    }

    /// Iterate all exposed routes in mount order.
    pub fn routes(&self) -> impl Iterator<Item = &Route> {
        self.modules.iter().flat_map(|module| module.routes())
    }

    /// Build an introspection report over mounted module metadata.
    #[must_use]
    pub fn introspect(&self) -> Introspection {
        introspect_modules(&self.modules)
    }
}

/// Introspection report for exposed boundary metadata.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Introspection {
    /// Number of exposed modules.
    pub module_count: usize,
    /// Number of exposed operations.
    pub operation_count: usize,
    /// Number of exposed routes.
    pub route_count: usize,
    /// Human-readable layer rule.
    pub layer_rule: &'static str,
}

/// Build an introspection report over server-facing module metadata.
#[must_use]
pub fn introspect_modules(modules: &[ServerModule]) -> Introspection {
    let operation_count = modules
        .iter()
        .map(ServerModule::operation_count)
        .sum::<usize>();
    let route_count = modules
        .iter()
        .map(|module| module.routes().len())
        .sum::<usize>();

    Introspection {
        module_count: modules.len(),
        operation_count,
        route_count,
        layer_rule: LAYER_RULE,
    }
}

/// Borrowed health check over a syncbat core's mounted operation descriptors.
///
/// This report is descriptor-only. It does not invoke handlers or claim
/// transport readiness.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CoreHealth {
    /// Operation names present in the borrowed syncbat core.
    pub mounted_operations: Vec<String>,
    /// Operation names absent from the borrowed syncbat core.
    pub missing_operations: Vec<String>,
    /// Human-readable layer rule.
    pub layer_rule: &'static str,
}

impl CoreHealth {
    /// Return true when every inspected operation name is mounted.
    #[must_use]
    pub fn is_healthy(&self) -> bool {
        self.missing_operations.is_empty()
    }
}

/// Inspect whether named operations are mounted in a borrowed syncbat core.
///
/// This is a boundary health/introspection helper only; syncbat remains the
/// owner of dispatch and batpak remains the owner of durable records.
#[must_use]
pub fn inspect_core_operations<I, S>(core: &syncbat::Core, operation_names: I) -> CoreHealth
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let mut mounted_operations = Vec::new();
    let mut missing_operations = Vec::new();

    for name in operation_names {
        let name = name.as_ref();
        if core.contains_operation(name) {
            mounted_operations.push(name.to_owned());
        } else {
            missing_operations.push(name.to_owned());
        }
    }

    CoreHealth {
        mounted_operations,
        missing_operations,
        layer_rule: LAYER_RULE,
    }
}

fn normalize_base_path(base_path: &str) -> String {
    let trimmed = base_path.trim_matches('/');
    if trimmed.is_empty() {
        String::new()
    } else {
        format!("/{trimmed}")
    }
}

fn validate_base_path(path: &str) -> Result<(), RouteValidationError> {
    if path.is_empty() {
        return Ok(());
    }
    validate_route_path(path)
}

fn validate_route_method(method: &str) -> Result<(), RouteValidationError> {
    if method.is_empty() {
        return Err(RouteValidationError::InvalidMethod {
            method: method.to_owned(),
            message: "empty",
        });
    }
    if method
        .bytes()
        .any(|byte| !matches!(byte, b'A'..=b'Z' | b'0'..=b'9' | b'_' | b'-'))
    {
        return Err(RouteValidationError::InvalidMethod {
            method: method.to_owned(),
            message: "expected ASCII uppercase letters, digits, '_' or '-'",
        });
    }
    Ok(())
}

fn validate_route_operation_name(name: &str) -> Result<(), RouteValidationError> {
    // Boundary check defers to the syncbat operation-name newtype so the
    // route layer never re-parses the grammar.
    syncbat::OperationName::new(name)
        .map(|_| ())
        .map_err(|error| {
            let message: &'static str = match error {
                syncbat::OperationNameError::Empty => "empty",
                syncbat::OperationNameError::TooLong { .. } => "too long",
                syncbat::OperationNameError::LeadingOrTrailingDot
                | syncbat::OperationNameError::ConsecutiveDots => {
                    "dot-separated tokens must be non-empty"
                }
                syncbat::OperationNameError::IllegalCharacter { .. } => {
                    "expected ASCII letters, digits, '.', '_' or '-'"
                }
                // `OperationNameError` is `#[non_exhaustive]`; any variant added
                // post-1.0 surfaces under a generic message until this layer
                // learns a more specific one.
                _ => "invalid operation name",
            };
            RouteValidationError::InvalidOperationName {
                name: name.to_owned(),
                message,
            }
        })
}

fn validate_route_path(path: &str) -> Result<(), RouteValidationError> {
    if path.is_empty() {
        return Err(RouteValidationError::InvalidPath {
            path: path.to_owned(),
            message: "empty",
        });
    }
    if path.len() > MAX_ROUTE_PATH_BYTES {
        return Err(RouteValidationError::InvalidPath {
            path: path.to_owned(),
            message: "too long",
        });
    }
    if !path.starts_with('/') {
        return Err(RouteValidationError::InvalidPath {
            path: path.to_owned(),
            message: "must start with '/'",
        });
    }
    if path == "/" {
        return Err(RouteValidationError::InvalidPath {
            path: path.to_owned(),
            message: "must include at least one segment",
        });
    }
    if path.len() > 1 && path.ends_with('/') {
        return Err(RouteValidationError::InvalidPath {
            path: path.to_owned(),
            message: "must not end with '/'",
        });
    }
    if path.contains("//") {
        return Err(RouteValidationError::InvalidPath {
            path: path.to_owned(),
            message: "empty path segments are not allowed",
        });
    }
    for segment in path.split('/').skip(1) {
        if segment == "." || segment == ".." {
            return Err(RouteValidationError::InvalidPath {
                path: path.to_owned(),
                message: "relative path segments are not allowed",
            });
        }
        if segment.bytes().any(
            |byte| !matches!(byte, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'.' | b'_' | b'-'),
        ) {
            return Err(RouteValidationError::InvalidPath {
                path: path.to_owned(),
                message: "expected ASCII letters, digits, '/', '.', '_' or '-'",
            });
        }
    }
    Ok(())
}