Skip to main content

soothfast_spec/
lib.rs

1//! Soothfast spec engine, working in both directions.
2//!
3//! **Checking.** Anything that declares an external surface — an OpenAPI
4//! spec, AsyncAPI channels, a GraphQL SDL schema, MCP tool schemas — parses
5//! into a common [`DeclaredOp`] set ([`providers`]) and reconciles against
6//! `#[route]` annotations with one mechanism ([`reconcile`]). The spec is
7//! hand-authored and the code proves it, which is the only order that works
8//! for a contract someone else serves.
9//!
10//! **Generating.** The same four dialects are also emitted *from* the code:
11//! [`schema`] turns rustdoc JSON into JSON Schema, and one of [`openapi`],
12//! [`asyncapi`], [`graphql`] or [`mcp`] renders a document from it. A
13//! generated spec cannot disagree with the code, so what is left worth
14//! gating is whether it broke a consumer — [`compat`], through each
15//! dialect's own diff.
16//!
17//! [`SpecKind`] names the dialect in both directions, and [`dialect`] is
18//! where the generation half dispatches on it.
19
20pub mod asyncapi;
21pub mod compat;
22pub mod dialect;
23pub mod graphql;
24pub mod mcp;
25pub mod openapi;
26pub mod probe;
27pub mod proto;
28pub mod providers;
29pub mod reconcile;
30pub mod router;
31pub mod schema;
32pub mod serialize;
33pub mod specified;
34
35/// One operation a spec declares.
36#[derive(Debug, Clone, PartialEq)]
37pub struct DeclaredOp {
38    /// Identity: operationId / GraphQL field / MCP tool / channel+verb.
39    pub operation: String,
40    /// HTTP verb, or QUERY/MUTATION/SUBSCRIPTION/PUBLISH/SUBSCRIBE/TOOL.
41    pub method: String,
42    /// Route path / field / tool / channel.
43    pub path: String,
44}
45
46/// One `#[soothfast::route]` annotation from the code.
47#[derive(Debug, Clone)]
48pub struct RouteDecl {
49    pub id: String,
50    pub operation: String,
51    pub method: String,
52    pub path: String,
53}
54
55/// Spec dialects soothfast reads and writes.
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum SpecKind {
58    OpenApi,
59    AsyncApi,
60    GraphQl,
61    McpTools,
62}
63
64/// Sniff the dialect from filename + content.
65pub fn sniff_kind(filename: &str, text: &str) -> Option<SpecKind> {
66    if filename.ends_with(".graphql") || filename.ends_with(".sdl") {
67        return Some(SpecKind::GraphQl);
68    }
69    let head: String = text.chars().take(2000).collect();
70    if head.contains("asyncapi") {
71        Some(SpecKind::AsyncApi)
72    } else if head.contains("openapi") || head.contains("swagger") {
73        Some(SpecKind::OpenApi)
74    } else if head.contains("\"tools\"") {
75        Some(SpecKind::McpTools)
76    } else if head.contains("type Query") || head.contains("type Mutation") {
77        Some(SpecKind::GraphQl)
78    } else {
79        None
80    }
81}