|
| 1 | +//! Built-in API module. |
| 2 | +//! |
| 3 | +//! This module exposes read-only, deterministic API helpers for built-in plugins. |
| 4 | +//! It is intended to be used by HTTP servers, CLIs, and embedded hosts. |
| 5 | +//! |
| 6 | +//! Design constraints: |
| 7 | +//! - No network or filesystem I/O. |
| 8 | +//! - Pure functions over in-memory data. |
| 9 | +//! - Deterministic output suitable for hashing and caching. |
| 10 | +
|
| 11 | +#![cfg(feature = "builtin")] |
| 12 | + |
| 13 | +use serde::{Deserialize, Serialize}; |
| 14 | +use serde_json::Value; |
| 15 | + |
| 16 | +use crate::builtin::spec::{builtin_specs}; |
| 17 | +use crate::builtin::spec::link_graph::{build_link_graph, link_graph_to_json}; |
| 18 | +use crate::spec::PluginSpec; |
| 19 | + |
| 20 | +/// Top-level API response wrapper. |
| 21 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 22 | +pub struct ApiResponse<T> { |
| 23 | + pub ok: bool, |
| 24 | + pub data: T, |
| 25 | +} |
| 26 | + |
| 27 | +/// Return all built-in plugin specs. |
| 28 | +pub fn get_builtin_specs() -> ApiResponse<Vec<PluginSpec>> { |
| 29 | + ApiResponse { |
| 30 | + ok: true, |
| 31 | + data: builtin_specs(), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Return built-in specs rendered as a link graph. |
| 36 | +pub fn get_builtin_link_graph() -> ApiResponse<Value> { |
| 37 | + let specs = builtin_specs(); |
| 38 | + let graph = build_link_graph(&specs); |
| 39 | + ApiResponse { |
| 40 | + ok: true, |
| 41 | + data: link_graph_to_json(&graph), |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Return a single built-in plugin spec by id. |
| 46 | +pub fn get_builtin_spec_by_id(id: &str) -> ApiResponse<Option<PluginSpec>> { |
| 47 | + let spec = builtin_specs().into_iter().find(|s| s.id == id); |
| 48 | + ApiResponse { ok: true, data: spec } |
| 49 | +} |
| 50 | + |
| 51 | +/// Health check for embedded usage. |
| 52 | +pub fn health() -> ApiResponse<&'static str> { |
| 53 | + ApiResponse { |
| 54 | + ok: true, |
| 55 | + data: "ok", |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use super::*; |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn specs_endpoint_returns_data() { |
| 65 | + let resp = get_builtin_specs(); |
| 66 | + assert!(resp.ok); |
| 67 | + assert!(!resp.data.is_empty()); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn graph_endpoint_returns_nodes() { |
| 72 | + let resp = get_builtin_link_graph(); |
| 73 | + assert!(resp.ok); |
| 74 | + assert!(resp.data.get("nodes").is_some()); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn lookup_by_id() { |
| 79 | + let resp = get_builtin_spec_by_id("builtin.repo"); |
| 80 | + assert!(resp.ok); |
| 81 | + assert!(resp.data.is_some()); |
| 82 | + } |
| 83 | +} |
0 commit comments