Skip to main content

mongreldb_protocol/
lib.rs

1//! MongrelDB versioned protocol messages and service definitions (spec
2//! section 6.7, Stage 1D).
3//!
4//! Every protocol adapter (native RPC, HTTP/JSON, Kit, MySQL wire) converts
5//! into the canonical request model defined here. All network formats carry a
6//! versioned envelope and fail closed on incompatible versions (spec 4.10).
7//!
8//! - [`request`]: the canonical request model every adapter converts into
9//!   (S1D-001).
10//! - [`envelope`]: the versioned, checksummed wire envelope with fail-closed
11//!   decode (spec section 4.10).
12//! - [`services`]: the seven service traits adapters dispatch against
13//!   (S1D-003).
14//! - [`session`]: the server-side session model (S1D-004).
15//! - [`prepared`]: the prepared-statement binding record and its
16//!   invalidation check (S1D-005).
17
18pub mod envelope;
19pub mod native_transport;
20pub mod prepared;
21pub mod request;
22pub mod services;
23pub mod session;
24
25/// Generated Protobuf messages and tonic client/server stubs for the native
26/// HTTP/2 transport.
27pub mod native {
28    tonic::include_proto!("mongreldb.v1");
29}
30
31pub const NATIVE_API_MAJOR: u32 = 1;
32pub const NATIVE_API_MINOR: u32 = 1;
33
34/// Fail closed when a native request omits its version or uses another major
35/// protocol generation. Newer minor versions remain forward-compatible
36/// through Protobuf unknown-field handling.
37pub fn validate_native_context(
38    context: Option<&native::RequestContext>,
39) -> Result<(), tonic::Status> {
40    let version = context
41        .and_then(|context| context.version.as_ref())
42        .ok_or_else(|| tonic::Status::invalid_argument("native API version is required"))?;
43    if version.major != NATIVE_API_MAJOR {
44        return Err(tonic::Status::failed_precondition(format!(
45            "unsupported native API major {}; supported major is {}",
46            version.major, NATIVE_API_MAJOR
47        )));
48    }
49    Ok(())
50}
51
52#[cfg(test)]
53pub(crate) mod test_support;