proxima-primitives 0.1.0

The pipe algebra itself: the Pipe trait — an async In -> Result<Out, Err> that becomes a transform, source, sink or observe by choice of types — plus its three tier siblings (SendPipe, UnpinPipe, UnpinSendPipe), Handler, SourcePipe, the payload types Carry, ResponseStream and RequestStream (buffered bodies are plain Bytes), HeaderList, the sans-IO byte-stream traits, runtime-agnostic concurrency primitives, and the Replay/GenericStream transport seam. Everything else in proxima composes what is defined here.
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use bytes::Bytes;
use core::future::Future;

use crate::pipe::SendPipe;
pub use crate::pipe::capabilities::{CheckOutcome, Checkable};
use crate::pipe::primitives::Pipe;

use crate::pipe::handler::{PipeHandle, ThreadLocalPipeHandle};
use crate::pipe::request::{Request, Response};
use proxima_core::ProximaError;

// proxima-config's schema module (Schema/SchemaRegistry/ValidationError) is
// std-only, and the HTTP `Checkable` path uses serde_json; the whole
// schema-backed validate surface is std-tier. The generic `Validate<Inner, Op>`
// over `Checkable<Op>` is alloc-tier.
#[cfg(feature = "std")]
use crate::pipe::handler::into_handle;
#[cfg(feature = "std")]
use crate::pipe::pipe_factory::PipeFactory;
#[cfg(feature = "std")]
use alloc::sync::Arc;
#[cfg(feature = "std")]
use proxima_config::schema::{Schema, SchemaRegistry, ValidationError};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use serde_json::{Value, json};
#[cfg(feature = "std")]
use std::pin::Pin;

#[cfg(feature = "std")]
const METRIC_REJECTED: &str = "proxima.validate.rejected_total";
#[cfg(feature = "std")]
const METRIC_ADMITTED: &str = "proxima.validate.admitted_total";

// ── main struct ───────────────────────────────────────────────────────────────

/// Predicate/schema-gated admission middleware. Generic over the inner pipe AND
/// the check op. The std default op is the HTTP `ValidateOp` (proxima-config's
/// schema module); under no_std+alloc that type is absent, so the default
/// falls back to `()` and callers name their own `Checkable` op explicitly.
#[cfg(feature = "std")]
pub struct Validate<Inner = PipeHandle, Op = ValidateOp> {
    pub inner: Inner,
    op: Op,
}

#[cfg(not(feature = "std"))]
pub struct Validate<Inner = PipeHandle, Op = ()> {
    pub inner: Inner,
    op: Op,
}

impl<Inner, Op> Validate<Inner, Op> {
    #[must_use]
    pub fn new(inner: Inner, op: Op) -> Self {
        Self { inner, op }
    }
}

impl<Inner, Op> SendPipe for Validate<Inner, Op>
where
    Inner: SendPipe + Clone,
    Inner::In: Checkable<Op, Out = Inner::Out> + Send,
    Inner::Out: Send,
    Inner::Err: From<ProximaError> + Send,
    Op: Clone + Send + Sync + 'static,
{
    type In = Inner::In;
    type Out = Inner::Out;
    type Err = Inner::Err;

    fn call(
        &self,
        input: Inner::In,
    ) -> impl Future<Output = Result<Inner::Out, Inner::Err>> + Send {
        let op = self.op.clone();
        let inner = self.inner.clone();
        async move {
            match input.check(&op).await.map_err(Inner::Err::from)? {
                CheckOutcome::Pass(admitted) => SendPipe::call(&inner, admitted).await,
                CheckOutcome::Reject(rejection) => Ok(rejection),
            }
        }
    }
}

impl<Op> Pipe for Validate<ThreadLocalPipeHandle, Op>
where
    Request<Bytes>: Checkable<Op, Out = Response<Bytes>>,
    Op: Clone + Send + Sync + 'static,
{
    type In = Request<Bytes>;
    type Out = Response<Bytes>;
    type Err = ProximaError;

    fn call(
        &self,
        input: Request<Bytes>,
    ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> {
        let op = self.op.clone();
        let inner = self.inner.clone();
        async move {
            match input.check(&op).await? {
                CheckOutcome::Pass(admitted) => Pipe::call(&inner, admitted).await,
                CheckOutcome::Reject(rejection) => Ok(rejection),
            }
        }
    }
}

// ── HTTP op type + Checkable impl ─────────────────────────────────────────────

#[cfg(feature = "std")]
#[derive(Clone)]
pub struct ValidateOp {
    pub schema: Schema,
    pub schemas: Arc<SchemaRegistry>,
}

#[cfg(feature = "std")]
impl ValidateOp {
    #[must_use]
    pub fn new(schema: Schema, schemas: Arc<SchemaRegistry>) -> Self {
        Self { schema, schemas }
    }
}

#[cfg(feature = "std")]
impl Checkable<ValidateOp> for Request<Bytes> {
    type Out = Response<Bytes>;

    fn check(
        self,
        op: &ValidateOp,
    ) -> impl Future<Output = Result<CheckOutcome<Self, Response<Bytes>>, ProximaError>> + Send
    {
        let schema = op.schema.clone();
        let schemas = op.schemas.clone();
        async move {
            let telemetry = self.context.telemetry.clone();
            let labels = self.context.metric_labels(&[]);
            let (request, buffered) = self.body_bytes().await?;
            let Request::<Bytes> {
                method,
                path,
                query,
                metadata,
                payload: _,
                stream: _,
                context,
            } = request;
            if buffered.is_empty()
                && matches!(
                    method,
                    crate::pipe::method::Method::Get
                        | crate::pipe::method::Method::Head
                        | crate::pipe::method::Method::Delete
                )
            {
                telemetry.counter_inc(METRIC_ADMITTED, &labels, 1);
                let forwarded = Request {
                    method,
                    path,
                    query,
                    metadata,
                    payload: buffered,
                    stream: None,
                    context,
                };
                return Ok(CheckOutcome::Pass(forwarded));
            }
            let value: Value = if buffered.is_empty() {
                Value::Null
            } else {
                serde_json::from_slice(&buffered).map_err(|err| {
                    ProximaError::Body(format!("validate: request payload is not JSON: {err}"))
                })?
            };
            if let Err(err) = schema.validate(&value, schemas.as_ref()) {
                telemetry.counter_inc(METRIC_REJECTED, &labels, 1);
                return Ok(CheckOutcome::Reject(rejected_response(&err)));
            }
            telemetry.counter_inc(METRIC_ADMITTED, &labels, 1);
            let forwarded = Request {
                method,
                path,
                query,
                metadata,
                payload: buffered,
                stream: None,
                context,
            };
            Ok(CheckOutcome::Pass(forwarded))
        }
    }
}

#[cfg(feature = "std")]
fn rejected_response(err: &ValidationError) -> Response<Bytes> {
    let body = json!({
        "error": "validation_failed",
        "path": err.path_string(),
        "message": err.message,
    });
    Response::new(400)
        .with_header("content-type", "application/json")
        .with_body(body.to_string())
}

// ── factory + constructors ────────────────────────────────────────────────────

#[cfg(feature = "std")]
impl Validate<PipeHandle, ValidateOp> {
    pub fn from_schema(inner: PipeHandle, schema: Schema, schemas: Arc<SchemaRegistry>) -> Self {
        Self::new(inner, ValidateOp::new(schema, schemas))
    }

    pub fn from_spec(
        inner: PipeHandle,
        value: &Value,
        schemas: &Arc<SchemaRegistry>,
    ) -> Result<Self, ProximaError> {
        let config: ValidateConfig = serde_json::from_value(value.clone())
            .map_err(|err| ProximaError::Config(format!("validate config: {err}")))?;
        config.from_config(inner, schemas)
    }
}

/// A schema reference: either a name resolved against the [`SchemaRegistry`], or
/// an inline [`Schema`] definition. Untagged so a JSON string deserialises to
/// [`SchemaRef::Named`] and a JSON object to [`SchemaRef::Inline`] — matching the
/// historical hand-parser's string-then-object precedence.
#[cfg(feature = "std")]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SchemaRef {
    Named(String),
    Inline(Schema),
}

/// Typed config surface for the `validate` middleware. The [`SchemaRegistry`] is
/// runtime state supplied at materialisation (not serialisable data), mirroring
/// how `callback` resolves its function from a registry.
#[cfg(feature = "std")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidateConfig {
    pub schema: SchemaRef,
}

#[cfg(feature = "std")]
impl ValidateConfig {
    /// Fluent constructor referencing a named schema.
    #[must_use]
    pub fn named(name: impl Into<String>) -> Self {
        Self {
            schema: SchemaRef::Named(name.into()),
        }
    }

    /// Fluent constructor carrying an inline schema.
    #[must_use]
    pub fn inline(schema: Schema) -> Self {
        Self {
            schema: SchemaRef::Inline(schema),
        }
    }

    /// Materialise the validate middleware around `inner`, resolving a named
    /// schema against `schemas`.
    pub fn from_config(
        self,
        inner: PipeHandle,
        schemas: &Arc<SchemaRegistry>,
    ) -> Result<Validate<PipeHandle, ValidateOp>, ProximaError> {
        let schema = match self.schema {
            SchemaRef::Named(name) => schemas.get(&name).ok_or_else(|| {
                ProximaError::Config(format!(
                    "validate middleware references unknown schema `{name}`"
                ))
            })?,
            SchemaRef::Inline(schema) => schema,
        };
        Ok(Validate::new(
            inner,
            ValidateOp::new(schema, schemas.clone()),
        ))
    }
}

#[cfg(feature = "std")]
pub struct ValidateFactory {
    schemas: Arc<SchemaRegistry>,
}

#[cfg(feature = "std")]
impl ValidateFactory {
    #[must_use]
    pub fn new(schemas: Arc<SchemaRegistry>) -> Self {
        Self { schemas }
    }
}

#[cfg(feature = "std")]
impl PipeFactory for ValidateFactory {
    fn name(&self) -> &str {
        "validate"
    }

    fn build(
        &self,
        spec: &Value,
        inner: Option<PipeHandle>,
    ) -> Pin<Box<dyn Future<Output = Result<PipeHandle, ProximaError>> + Send + '_>> {
        let spec = spec.clone();
        Box::pin(async move {
            let inner = inner
                .ok_or_else(|| ProximaError::Config("validate requires an inner pipe".into()))?;
            let validate = Validate::from_spec(inner, &spec, &self.schemas)?;
            Ok(into_handle(validate))
        })
    }
}

// `#[proxima::test]` pulls in the `proxima` dev-dependency, which the
// loom build keeps out of the graph (see
// `[target.'cfg(not(loom))'.dev-dependencies]` in Cargo.toml); these
// tests are unrelated to the Notify/watch loom protocol.
#[cfg(all(test, not(loom)))]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use std::future::Future;

    use super::*;
    use crate::pipe::handler::into_handle;
    use crate::pipe::request::Request;
    use crate::pipe::telemetry_surface::{NoopTelemetry, TelemetryHandle};
    use proxima_config::schema::{FieldFlags, StructField};

    fn echo_pipe() -> PipeHandle {
        struct EchoPipe;
        impl SendPipe for EchoPipe {
            type In = Request<Bytes>;
            type Out = Response<Bytes>;
            type Err = ProximaError;

            fn call(
                &self,
                request: Request<Bytes>,
            ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> + Send {
                async move {
                    let (_, body) = request.body_bytes().await?;
                    Ok(Response::new(200).with_body(body))
                }
            }
        }
        into_handle(EchoPipe)
    }

    fn user_schema() -> Schema {
        Schema::Struct {
            name: "User".into(),
            fields: vec![StructField {
                name: "name".into(),
                schema: Schema::String {
                    pattern: None,
                    format: None,
                    min_len: Some(1),
                    max_len: None,
                },
                flags: FieldFlags::default(),
            }],
        }
    }

    fn noop_telemetry() -> TelemetryHandle {
        NoopTelemetry::handle()
    }

    // principle-4 parity: the fluent builder and the config value must lower to
    // identical ValidateOp state (the resolved schema).
    #[test]
    fn parity_fluent_builder_and_config_value_match() {
        let schemas = Arc::new(SchemaRegistry::new());
        let schema_json = serde_json::to_value(user_schema()).expect("schema json");

        let from_value: ValidateConfig =
            serde_json::from_value(json!({ "schema": schema_json })).expect("from_value");
        let from_value = from_value
            .from_config(echo_pipe(), &schemas)
            .expect("from_config value");

        let from_builder = ValidateConfig::inline(user_schema())
            .from_config(echo_pipe(), &schemas)
            .expect("from_config builder");

        assert_eq!(
            serde_json::to_value(&from_value.op.schema).expect("ser value"),
            serde_json::to_value(&from_builder.op.schema).expect("ser builder"),
        );
    }

    #[proxima::test]
    async fn admits_valid_body() {
        let schemas = Arc::new(SchemaRegistry::new());
        let wrapped = Validate::from_schema(echo_pipe(), user_schema(), schemas);
        let telemetry = noop_telemetry();
        let request = Request::builder()
            .method("POST")
            .path("/")
            .body(bytes::Bytes::from_static(br#"{"name":"brian"}"#))
            .header("content-type", "application/json")
            .telemetry(telemetry)
            .build()
            .expect("builder");
        let response = SendPipe::call(&wrapped, request).await.expect("call");
        assert_eq!(response.status, 200);
    }

    #[proxima::test]
    async fn rejects_missing_required_field() {
        let schemas = Arc::new(SchemaRegistry::new());
        let wrapped = Validate::from_schema(echo_pipe(), user_schema(), schemas);
        let telemetry = noop_telemetry();
        let request = Request::builder()
            .method("POST")
            .path("/")
            .body(bytes::Bytes::from_static(b"{}"))
            .header("content-type", "application/json")
            .telemetry(telemetry)
            .build()
            .expect("builder");
        let response = SendPipe::call(&wrapped, request).await.expect("call");
        assert_eq!(response.status, 400);
        let body = response.collect_body().await.expect("body");
        let parsed: Value = serde_json::from_slice(&body).expect("json");
        assert_eq!(parsed["error"], "validation_failed");
        assert!(
            parsed["message"]
                .as_str()
                .unwrap_or("")
                .contains("missing required field `name`")
        );
    }

    #[proxima::test]
    async fn resolves_named_schema_from_registry() {
        let schemas = Arc::new(SchemaRegistry::new());
        schemas.register("User", user_schema()).expect("register");
        let wrapped = Validate::from_spec(echo_pipe(), &json!({"schema": "User"}), &schemas)
            .expect("from spec");
        let telemetry = noop_telemetry();
        let request = Request::builder()
            .method("POST")
            .path("/")
            .body(bytes::Bytes::from_static(br#"{"name":"brian"}"#))
            .header("content-type", "application/json")
            .telemetry(telemetry)
            .build()
            .expect("builder");
        let response = SendPipe::call(&wrapped, request).await.expect("call");
        assert_eq!(response.status, 200);
    }

    #[proxima::test]
    async fn empty_body_get_short_circuits_validation() {
        let schemas = Arc::new(SchemaRegistry::new());
        let wrapped = Validate::from_schema(echo_pipe(), user_schema(), schemas);
        let telemetry = noop_telemetry();
        let request = Request::builder()
            .method("GET")
            .path("/todos")
            .telemetry(telemetry)
            .build()
            .expect("builder");
        let response = SendPipe::call(&wrapped, request).await.expect("call");
        assert_eq!(
            response.status, 200,
            "GET with empty body must skip validate"
        );
    }

    #[proxima::test]
    async fn empty_body_delete_short_circuits_validation() {
        let schemas = Arc::new(SchemaRegistry::new());
        let wrapped = Validate::from_schema(echo_pipe(), user_schema(), schemas);
        let telemetry = noop_telemetry();
        let request = Request::builder()
            .method("DELETE")
            .path("/todos/abc")
            .telemetry(telemetry)
            .build()
            .expect("builder");
        let response = SendPipe::call(&wrapped, request).await.expect("call");
        assert_eq!(
            response.status, 200,
            "DELETE with empty body must skip validate"
        );
    }

    #[proxima::test]
    async fn from_spec_errors_on_unknown_named_schema() {
        let schemas = Arc::new(SchemaRegistry::new());
        let outcome =
            Validate::from_spec(echo_pipe(), &json!({"schema": "MissingShape"}), &schemas);
        assert!(matches!(outcome, Err(ProximaError::Config(_))));
    }

    #[derive(Clone, PartialEq, Debug)]
    struct EventMsg {
        kind: &'static str,
        value: u64,
    }

    #[derive(Clone)]
    struct EventOp {
        allowed_kind: &'static str,
    }

    impl Checkable<EventOp> for EventMsg {
        type Out = EventMsg;

        fn check(
            self,
            op: &EventOp,
        ) -> impl Future<Output = Result<CheckOutcome<Self, Self::Out>, ProximaError>> + Send
        {
            let allowed = op.allowed_kind;
            async move {
                if self.kind == allowed {
                    Ok(CheckOutcome::Pass(self))
                } else {
                    Ok(CheckOutcome::Reject(EventMsg {
                        kind: "rejected",
                        value: 0,
                    }))
                }
            }
        }
    }

    #[derive(Clone)]
    struct EventSink;

    impl SendPipe for EventSink {
        type In = EventMsg;
        type Out = EventMsg;
        type Err = ProximaError;

        fn call(
            &self,
            input: EventMsg,
        ) -> impl Future<Output = Result<EventMsg, ProximaError>> + Send {
            async move {
                Ok(EventMsg {
                    kind: "processed",
                    value: input.value + 1,
                })
            }
        }
    }

    #[proxima::test]
    async fn validate_is_generic_over_a_non_http_payload() {
        let stack = Validate::new(
            EventSink,
            EventOp {
                allowed_kind: "ping",
            },
        );

        let admitted = SendPipe::call(
            &stack,
            EventMsg {
                kind: "ping",
                value: 10,
            },
        )
        .await
        .expect("admitted call");
        assert_eq!(
            admitted,
            EventMsg {
                kind: "processed",
                value: 11
            }
        );

        let rejected = SendPipe::call(
            &stack,
            EventMsg {
                kind: "unknown",
                value: 99,
            },
        )
        .await
        .expect("rejected call");
        assert_eq!(
            rejected,
            EventMsg {
                kind: "rejected",
                value: 0
            }
        );
    }
}