Skip to main content

sark_core/route/
table.rs

1//! Static route-table contracts shared by protocol adapters.
2
3use sark_http::{method, payload::response::dispatch};
4
5use crate::{
6    decode, manifold,
7    request::path,
8    route::{self, guard},
9};
10
11/// One protocol-neutral route lookup.
12pub struct Key<'a> {
13    method: method::Known,
14    target: &'a [u8],
15    protocol: Option<&'a [u8]>,
16}
17
18impl<'a> Key<'a> {
19    pub const fn new(method: method::Known, target: &'a [u8], protocol: Option<&'a [u8]>) -> Self {
20        Self {
21            method,
22            target,
23            protocol,
24        }
25    }
26
27    #[doc(hidden)]
28    pub const fn into_parts(self) -> (method::Known, &'a [u8], Option<&'a [u8]>) {
29        (self.method, self.target, self.protocol)
30    }
31}
32
33/// A synchronous handler usable by the portable router.
34pub trait Sync<S>: route::Spec<Kind = dispatch::Sync> + manifold::Route<S> {}
35
36impl<S, R> Sync<S> for R where R: route::Spec<Kind = dispatch::Sync> + manifold::Route<S> {}
37
38/// Generated identity for one route inside exactly one table.
39pub trait Slot<S>: guard::Slot<S> {
40    type Set;
41    type Route: route::Spec;
42
43    const INDEX: u32;
44}
45
46/// Direct leaf operation. Implementations are monomorphized at the selected
47/// generated slot; no erased callback crosses this boundary.
48pub trait Visit<S, Set> {
49    type Output;
50
51    fn visit<'a, P>(
52        self,
53        target: path::Target<'a>,
54        path_end: usize,
55        captures: <<<P::Route as route::Spec>::Request as route::Schema>::RawParams as route::RawParams>::Captures,
56    ) -> Self::Output
57    where
58        P: Slot<S, Set = Set>,
59        P::Route: Sync<S>;
60}
61
62/// Route-type operation selected by a trusted generated route index.
63pub trait Each<S, Set> {
64    type Output;
65
66    fn each<P>(self) -> Self::Output
67    where
68        P: Slot<S, Set = Set>,
69        P::Route: Sync<S>;
70}
71
72/// A state-borrowing decoder backed by one generated route table.
73pub trait Router: decode::Decode {
74    type State;
75    type Table: route::raw::Table<Self::State>;
76
77    fn state(&self) -> &Self::State;
78}