Skip to main content

endpoint_validator/parser/
models.rs

1//! Types for reading `services.json`, the machine-readable endpoint description
2//! written by `endpoint-gen`.
3//!
4//! The schema types are **not** redefined here. They are re-exported from
5//! [`endpoint_libs::model`], which is where `endpoint-gen` gets them from when it
6//! writes the file. This crate previously kept hand-copied duplicates, which
7//! silently rotted: `EnumVariant.comment` was renamed to `description` upstream
8//! and the copy here never followed, so every current `services.json` failed to
9//! parse with `missing field 'comment'`. The vendored `Type` had also drifted
10//! badly — it still listed `Date`, `Int`, `BigInt`, `Numeric`, `Inet` and
11//! `DataTable`, none of which exist upstream, and was missing `UInt32`, `Int32`,
12//! `Int64`, `Float64`, `NanoId`, `IpAddr` and `StructTable`.
13//!
14//! Anything describing the wire schema belongs upstream. Only this tool's own
15//! config types are defined here.
16
17use std::collections::HashMap;
18
19use serde::Deserialize;
20
21pub use endpoint_libs::model::{EndpointSchema, EnumVariant, Field, Service, Type};
22
23/// The whole of `services.json`.
24///
25/// `enums` and `structs` deserialize as bare [`Type`]s because that is literally
26/// what they are: serde's external tagging renders `Type::Enum` as
27/// `{"Enum": {...}}` and `Type::Struct` as `{"Struct": {...}}`, exactly as the
28/// file contains them.
29///
30/// Note the file holds only `frontend_facing` endpoints — `endpoint-gen`'s
31/// deliberate choice, so this tool sees the public surface and nothing else.
32#[derive(Debug, Deserialize)]
33pub struct Services {
34    #[serde(default)]
35    pub enums: Vec<Type>,
36    pub services: Vec<Service>,
37    #[serde(default)]
38    pub structs: Vec<Type>,
39}
40
41/// One endpoint, flattened into what the driver needs to issue a call.
42#[derive(Debug, Clone)]
43pub struct EndpointMetadata {
44    pub service_name: String,
45    pub method_id: u32,
46    pub params: Vec<ParameterMetadata>,
47    pub is_stream: bool,
48}
49
50#[derive(Debug, Clone)]
51pub struct ParameterMetadata {
52    pub name: String,
53    pub ty: Type,
54}
55
56/// `config.toml` — preset parameter values, keyed by endpoint.
57#[derive(Debug, Deserialize)]
58pub struct Config {
59    #[serde(flatten)]
60    pub endpoints: HashMap<String, EndpointData>,
61}
62
63#[derive(Debug, Deserialize)]
64pub struct EndpointData {
65    pub name: String,
66    #[serde(default)]
67    pub params: HashMap<String, ParamValue>,
68}
69
70#[derive(Debug, Deserialize)]
71#[serde(untagged)]
72pub enum ParamValue {
73    String(String),
74    Number(i64),
75    Bool(bool),
76    Object(HashMap<String, ParamValue>),
77    Array(Vec<ParamValue>),
78}