Skip to main content

aether_ndk/
schema.rs

1//! JSON schema generation for registered nodes.
2//! Used by the CLI and manifest system.
3
4use serde::{Deserialize, Serialize};
5use crate::{node::NodeRegistry, ParamDef};
6
7#[derive(Debug, Serialize, Deserialize)]
8pub struct NodeSchema {
9    pub type_name: String,
10    pub params: Vec<ParamSchema>,
11}
12
13#[derive(Debug, Serialize, Deserialize)]
14pub struct ParamSchema {
15    pub name: String,
16    pub min: f32,
17    pub max: f32,
18    pub default: f32,
19}
20
21impl From<&ParamDef> for ParamSchema {
22    fn from(d: &ParamDef) -> Self {
23        Self {
24            name: d.name.to_string(),
25            min: d.min,
26            max: d.max,
27            default: d.default,
28        }
29    }
30}
31
32/// Generate JSON schema for all registered nodes.
33pub fn generate_schema(registry: &NodeRegistry) -> Vec<NodeSchema> {
34    registry
35        .list()
36        .into_iter()
37        .map(|name| NodeSchema {
38            type_name: name.to_string(),
39            params: registry
40                .param_defs(name)
41                .unwrap_or(&[])
42                .iter()
43                .map(ParamSchema::from)
44                .collect(),
45        })
46        .collect()
47}
48
49/// Serialize schema to pretty JSON.
50pub fn schema_to_json(registry: &NodeRegistry) -> String {
51    let schema = generate_schema(registry);
52    serde_json::to_string_pretty(&schema).unwrap_or_default()
53}