mecha10-core 0.6.2

Mecha10 core runtime and foundational types
Documentation
//! Built-in JSON Schema definitions for Mecha10 configuration files.

use serde_json::Value;

// ============================================================================
// Schema Definitions
// ============================================================================

/// Get the built-in JSON Schema for mecha10.json project configuration
pub fn get_project_schema() -> Value {
    serde_json::json!({
        "$schema": "http://json-schema.org/draft-07/schema#",
        "$id": "https://mecha10.dev/schemas/project.schema.json",
        "title": "Mecha10 Project Configuration",
        "description": "Configuration file for Mecha10 robotics projects",
        "type": "object",
        "required": ["name", "version"],
        "properties": {
            "$schema": {
                "type": "string",
                "description": "JSON Schema reference"
            },
            "name": {
                "type": "string",
                "pattern": "^[a-z0-9][a-z0-9-]*$",
                "minLength": 1,
                "maxLength": 64,
                "description": "Project name (lowercase, alphanumeric, hyphens)"
            },
            "version": {
                "type": "string",
                "pattern": "^\\d+\\.\\d+\\.\\d+(-.+)?$",
                "description": "Semantic version (e.g., '1.0.0' or '1.0.0-beta')"
            },
            "description": {
                "type": "string",
                "maxLength": 500,
                "description": "Project description"
            },
            "author": {
                "type": "string",
                "maxLength": 100,
                "description": "Project author"
            },
            "license": {
                "type": "string",
                "maxLength": 50,
                "description": "License identifier (e.g., MIT, Apache-2.0)"
            },
            "robot": {
                "type": "object",
                "required": ["id", "type"],
                "properties": {
                    "id": {
                        "type": "string",
                        "pattern": "^[a-zA-Z0-9_\\-${}]+$",
                        "description": "Robot identifier (can use ${ROBOT_ID} env var)"
                    },
                    "type": {
                        "type": "string",
                        "enum": ["service", "delivery", "industrial", "mobile", "manipulator", "custom"],
                        "description": "Robot type classification"
                    },
                    "platform": {
                        "type": "string",
                        "description": "Hardware platform (e.g., differential-drive, mecanum, ackermann)"
                    }
                },
                "description": "Robot hardware configuration"
            },
            "nodes": {
                "type": "object",
                "properties": {
                    "drivers": {
                        "type": "array",
                        "items": { "$ref": "#/definitions/node" },
                        "description": "Hardware driver nodes"
                    },
                    "standard": {
                        "type": "array",
                        "items": { "$ref": "#/definitions/standardNode" },
                        "description": "Standard/pre-built nodes from packages"
                    },
                    "custom": {
                        "type": "array",
                        "items": { "$ref": "#/definitions/node" },
                        "description": "Custom application nodes"
                    }
                },
                "description": "Node definitions"
            },
            "run_targets": {
                "type": "object",
                "patternProperties": {
                    "^[a-z][a-z0-9_-]*$": {
                        "$ref": "#/definitions/runTarget"
                    }
                },
                "description": "Execution targets (robot, remote, dev, etc.)"
            },
            "behaviors": {
                "type": "object",
                "properties": {
                    "default": {
                        "type": "string",
                        "description": "Default behavior tree file"
                    }
                },
                "additionalProperties": {
                    "type": "string"
                },
                "description": "Behavior tree configurations"
            },
            "simulation": {
                "type": "object",
                "properties": {
                    "enabled": {
                        "type": "boolean",
                        "description": "Enable simulation mode"
                    },
                    "environment": {
                        "type": "string",
                        "description": "Path to simulation environment file"
                    }
                },
                "description": "Simulation settings"
            },
            "scripts": {
                "type": "object",
                "patternProperties": {
                    "^[a-z][a-z0-9:_-]*$": {
                        "type": "string"
                    }
                },
                "description": "Script shortcuts (npm scripts-like)"
            }
        },
        "definitions": {
            "node": {
                "type": "object",
                "required": ["name", "path"],
                "properties": {
                    "name": {
                        "type": "string",
                        "pattern": "^[a-z][a-z0-9_]*$",
                        "description": "Node name (snake_case)"
                    },
                    "path": {
                        "type": "string",
                        "description": "Path to node source code"
                    },
                    "config": {
                        "type": "string",
                        "description": "Path to node configuration file"
                    },
                    "description": {
                        "type": "string",
                        "maxLength": 200,
                        "description": "Node description"
                    },
                    "run_target": {
                        "type": "string",
                        "description": "Where to run this node (robot, remote, etc.)"
                    },
                    "enabled": {
                        "type": "boolean",
                        "default": true,
                        "description": "Whether node is enabled"
                    },
                    "auto_restart": {
                        "type": "boolean",
                        "default": true,
                        "description": "Auto-restart on crash"
                    },
                    "depends_on": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        },
                        "description": "Node dependencies (other nodes that must start first)"
                    }
                }
            },
            "standardNode": {
                "type": "object",
                "required": ["package", "binary"],
                "properties": {
                    "package": {
                        "type": "string",
                        "pattern": "^@[a-z0-9-]+/[a-z0-9/-]+$",
                        "description": "Package identifier (e.g., @mecha10/drivers/camera-fake)"
                    },
                    "binary": {
                        "type": "string",
                        "description": "Binary name within package"
                    },
                    "config": {
                        "type": "string",
                        "description": "Path to configuration file"
                    }
                }
            },
            "runTarget": {
                "type": "object",
                "properties": {
                    "description": {
                        "type": "string",
                        "maxLength": 200,
                        "description": "Target description"
                    },
                    "includes": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        },
                        "description": "Node groups to include"
                    },
                    "resources": {
                        "type": "object",
                        "properties": {
                            "cpu_limit": {
                                "type": "string",
                                "pattern": "^\\d+(\\.\\d+)?$",
                                "description": "CPU limit (e.g., '2.0')"
                            },
                            "memory_limit": {
                                "type": "string",
                                "pattern": "^\\d+(K|M|G|T)?$",
                                "description": "Memory limit (e.g., '512M', '16G')"
                            },
                            "gpu": {
                                "oneOf": [
                                    { "type": "boolean" },
                                    {
                                        "type": "object",
                                        "properties": {
                                            "enabled": { "type": "boolean" },
                                            "vendor": {
                                                "type": "string",
                                                "enum": ["nvidia", "amd", "intel"]
                                            },
                                            "count": {
                                                "type": "integer",
                                                "minimum": 1
                                            },
                                            "optional": { "type": "boolean" }
                                        }
                                    }
                                ],
                                "description": "GPU requirements"
                            },
                            "storage_type": {
                                "type": "string",
                                "enum": ["local", "ssd", "hdd", "any"],
                                "description": "Storage type requirement"
                            },
                            "storage_size_gb": {
                                "type": "integer",
                                "minimum": 1,
                                "description": "Storage size in GB"
                            },
                            "model_cache": {
                                "type": "string",
                                "description": "Path to model cache directory"
                            },
                            "devices": {
                                "type": "array",
                                "items": {
                                    "type": "string",
                                    "pattern": "^/dev/.+$"
                                },
                                "description": "Required device files (e.g., /dev/video0)"
                            }
                        },
                        "description": "Resource requirements"
                    }
                }
            }
        }
    })
}

/// Get the JSON Schema for NodeConfig (runtime node configuration)
pub fn get_node_config_schema() -> Value {
    serde_json::json!({
        "$schema": "http://json-schema.org/draft-07/schema#",
        "title": "Mecha10 Node Configuration",
        "description": "Runtime configuration for individual nodes",
        "type": "object",
        "required": ["id", "executable"],
        "properties": {
            "id": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9_-]*$",
                "description": "Unique node identifier"
            },
            "name": {
                "type": "string",
                "maxLength": 100,
                "description": "Human-readable name"
            },
            "executable": {
                "type": "string",
                "minLength": 1,
                "description": "Path to executable"
            },
            "args": {
                "type": "array",
                "items": { "type": "string" },
                "description": "Command-line arguments"
            },
            "env": {
                "type": "object",
                "patternProperties": {
                    "^[A-Z_][A-Z0-9_]*$": {
                        "type": "string"
                    }
                },
                "description": "Environment variables"
            },
            "mode": {
                "type": "string",
                "enum": ["local", "remote", "simulation"],
                "default": "local",
                "description": "Execution mode"
            },
            "auto_restart": {
                "type": "boolean",
                "default": true,
                "description": "Auto-restart on crash"
            },
            "health_check_interval_s": {
                "type": "integer",
                "minimum": 1,
                "maximum": 300,
                "default": 5,
                "description": "Health check interval in seconds"
            },
            "publishes": {
                "type": "array",
                "items": {
                    "type": "string",
                    "pattern": "^/[a-z0-9/_-]+$"
                },
                "description": "Topics this node publishes to"
            },
            "subscribes": {
                "type": "array",
                "items": {
                    "type": "string",
                    "pattern": "^/[a-z0-9/_-]+$"
                },
                "description": "Topics this node subscribes to"
            },
            "depends_on": {
                "type": "array",
                "items": {
                    "type": "string"
                },
                "description": "Node dependencies"
            }
        }
    })
}