Skip to main content

harn_hostlib/
schemas.rs

1//! Embedded JSON Schemas for every hostlib host method.
2//!
3//! Schemas live at `schemas/<module>/<method>.{request,response}.json` and
4//! are baked into the crate at compile time via `include_str!`. They're the
5//! source of truth for hostlib request/response compatibility: the schema
6//! files ship with the crate (see the `include` field in `Cargo.toml`),
7//! and consumers fetch them through this module.
8//!
9//! Schemas use JSON Schema draft 2020-12.
10
11mod validation;
12
13pub(crate) use validation::{validate_request_args, validate_response};
14
15/// Direction of a schema (request body vs. response body).
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum SchemaKind {
18    /// Schema for the *input* of a host method.
19    Request,
20    /// Schema for the *output* of a host method.
21    Response,
22}
23
24// The build script derives the catalog from the directory. Embedders use it
25// for drift checks, schema export, and live contract validation.
26include!(concat!(env!("OUT_DIR"), "/hostlib_schemas.rs"));
27
28/// Look up a single schema as raw JSON text.
29pub fn lookup(module: &str, method: &str, kind: SchemaKind) -> Option<&'static str> {
30    SCHEMAS
31        .iter()
32        .find(|(m, mt, k, _)| *m == module && *mt == method && *k == kind)
33        .map(|(_, _, _, body)| *body)
34}