Path: blob/main/crates/bevy_remote/src/schemas/open_rpc.rs
6849 views
//! Module with trimmed down `OpenRPC` document structs.1//! It tries to follow this standard: <https://spec.open-rpc.org>2use bevy_platform::collections::HashMap;3use bevy_utils::default;4use serde::{Deserialize, Serialize};56use crate::RemoteMethods;78use super::json_schema::JsonSchemaBevyType;910/// Represents an `OpenRPC` document as defined by the `OpenRPC` specification.11#[derive(Debug, Serialize, Deserialize)]12#[serde(rename_all = "camelCase")]13pub struct OpenRpcDocument {14/// The version of the `OpenRPC` specification being used.15pub openrpc: String,16/// Informational metadata about the document.17pub info: InfoObject,18/// List of RPC methods defined in the document.19pub methods: Vec<MethodObject>,20/// Optional list of server objects that provide the API endpoint details.21pub servers: Option<Vec<ServerObject>>,22}2324/// Contains metadata information about the `OpenRPC` document.25#[derive(Serialize, Deserialize, Debug)]26#[serde(rename_all = "camelCase")]27pub struct InfoObject {28/// The title of the API or document.29pub title: String,30/// The version of the API.31pub version: String,32/// An optional description providing additional details about the API.33#[serde(skip_serializing_if = "Option::is_none")]34pub description: Option<String>,35/// A collection of custom extension fields.36#[serde(flatten)]37pub extensions: HashMap<String, serde_json::Value>,38}3940impl Default for InfoObject {41fn default() -> Self {42Self {43title: "Bevy Remote Protocol".to_owned(),44version: env!("CARGO_PKG_VERSION").to_owned(),45description: None,46extensions: Default::default(),47}48}49}5051/// Describes a server hosting the API as specified in the `OpenRPC` document.52#[derive(Serialize, Deserialize, Debug, Default)]53#[serde(rename_all = "camelCase")]54pub struct ServerObject {55/// The name of the server.56pub name: String,57/// The URL endpoint of the server.58pub url: String,59/// An optional description of the server.60#[serde(skip_serializing_if = "Option::is_none")]61pub description: Option<String>,62/// Additional custom extension fields.63#[serde(flatten)]64pub extensions: HashMap<String, serde_json::Value>,65}6667/// Represents an RPC method in the `OpenRPC` document.68#[derive(Serialize, Deserialize, Debug, Default)]69#[serde(rename_all = "camelCase")]70pub struct MethodObject {71#[expect(72clippy::doc_markdown,73reason = "In this case, we are referring to a string, so using quotes instead of backticks makes sense."74)]75/// The method name (e.g., "world.get_components")76pub name: String,77/// An optional short summary of the method.78#[serde(skip_serializing_if = "Option::is_none")]79pub summary: Option<String>,80/// An optional detailed description of the method.81#[serde(skip_serializing_if = "Option::is_none")]82pub description: Option<String>,83/// Parameters for the RPC method84#[serde(default)]85pub params: Vec<Parameter>,86// /// The expected result of the method87// #[serde(skip_serializing_if = "Option::is_none")]88// pub result: Option<Parameter>,89/// Additional custom extension fields.90#[serde(flatten)]91pub extensions: HashMap<String, serde_json::Value>,92}9394/// Represents an RPC method parameter in the `OpenRPC` document.95#[derive(Serialize, Deserialize, Debug)]96#[serde(rename_all = "camelCase")]97pub struct Parameter {98/// Parameter name99pub name: String,100/// Parameter description101#[serde(skip_serializing_if = "Option::is_none")]102pub description: Option<String>,103/// JSON schema describing the parameter104pub schema: JsonSchemaBevyType,105/// Additional custom extension fields.106#[serde(flatten)]107pub extensions: HashMap<String, serde_json::Value>,108}109110impl From<&RemoteMethods> for Vec<MethodObject> {111fn from(value: &RemoteMethods) -> Self {112value113.methods()114.iter()115.map(|e| MethodObject {116name: e.to_owned(),117..default()118})119.collect()120}121}122123124